結果

問題 No.125 悪の花弁
ユーザー LayCurse
提出日時 2014-11-25 22:44:41
言語 C++11
(gcc 4.8.5)
結果
AC  
実行時間 168 ms / 5,000 ms
コード長 3,750 Byte
コンパイル時間 1,151 ms
使用メモリ 21,440 KB
最終ジャッジ日時 2020-05-03 07:47:36

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 117 ms
19,968 KB
testcase_01 AC 119 ms
20,352 KB
testcase_02 AC 168 ms
21,440 KB
testcase_03 AC 168 ms
21,440 KB
testcase_04 AC 166 ms
21,060 KB
testcase_05 AC 164 ms
21,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #
#include<bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define mygc(c) (c)=getchar_unlocked()
#define mypc(c) putchar_unlocked(c)

#define ll long long
#define ull unsigned ll

void reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
void reader(ll *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
int reader(char c[]){int i,s=0;for(;;){mygc(i);if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF) break;}c[s++]=i;for(;;){mygc(i);if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF) break;c[s++]=i;}c[s]='\0';return s;}
template <class T, class S> void reader(T *x, S *y){reader(x);reader(y);}
template <class T, class S, class U> void reader(T *x, S *y, U *z){reader(x);reader(y);reader(z);}
template <class T, class S, class U, class V> void reader(T *x, S *y, U *z, V *w){reader(x);reader(y);reader(z);reader(w);}

void writer(int x, char c){int s=0,m=0;char f[10];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(ll x, char c){int s=0,m=0;char f[20];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(const char c[]){int i;for(i=0;c[i]!='\0';i++)mypc(c[i]);}
void writer(const char x[], char c){int i;for(i=0;x[i]!='\0';i++)mypc(x[i]);mypc(c);}
template<class T> void writerLn(T x){writer(x,'\n');}
template<class T, class S> void writerLn(T x, S y){writer(x,' ');writer(y,'\n');}
template<class T, class S, class U> void writerLn(T x, S y, U z){writer(x,' ');writer(y,' ');writer(z,'\n');}
template<class T> void writerArr(T x[], int n){int i;if(!n){mypc('\n');return;}rep(i,n-1)writer(x[i],' ');writer(x[n-1],'\n');}

template<class T> T GCD(T a,T b){T r; while(a){r=b; b=a; a=r%a;} return b;}

void extended_euclid(ll x,ll y,ll *c,ll *a,ll *b){
  ll a0,a1,a2,b0,b1,b2,r0,r1,r2,q;
  r0=x; r1=y; a0=1; a1=0; b0=0; b1=1;
  while(r1>0){
    q=r0/r1; r2=r0%r1; a2=a0-q*a1; b2=b0-q*b1;
    r0=r1; r1=r2; a0=a1; a1=a2; b0=b1; b1=b2;
  }
  *c=r0; *a=a0; *b=b0;
}

ll get_inv(ll n, ll p){
  ll a,b,c;
  extended_euclid(n,p,&c,&a,&b);
  if(a<p) a+=p;
  return a%p;
}

#define MD 1000000007

int K, C[100001];

ll fact[1100000], fact_inv[1100000];
int cnt[1100000];

int solve(void){
  int i, j, k, g, sum = 0;
  ll res = 0, add;

  rep(i,K) sum += C[i];
  g = 0;
  rep(i,K) g = GCD(g, C[i]);

  rep(i,sum+1) cnt[i] = 0;
  rep(i,sum) cnt[sum/GCD(sum,i)]++;

  rep(k,sum+1) if(cnt[k] && g%k==0){
    add = cnt[k] * fact[sum/k] % MD;
    rep(i,K) (add *= fact_inv[C[i]/k]) %= MD;
    (res += add) %= MD;
  }
  (res *= get_inv(sum, MD)) %= MD;

  return res;
}

int solve_brute(void){
  int i, j, k, res = 0;
  set<string> s;
  string str, tmp, mn;

  rep(i,K) rep(j,C[i]) str += (char)('A'+i);
  do{
    tmp = mn = str;
    rep(i,tmp.size()){
      tmp = tmp.substr(1) + tmp[0];
      mn = min(mn, tmp);
    }
    if(s.count(mn)==0) res++, s.insert(mn);
  }while(next_permutation(str.begin(), str.end()));

  return res;
}

int main(){
  int i, j, k;
  int res, res1;

  fact[0] = 1;
  REP(i,1,1000100) fact[i] = fact[i-1] * i % MD;
  fact_inv[1000050] = get_inv(fact[1000050], MD);
  for(i=1000050-1;i>=0;i--) fact_inv[i] = fact_inv[i+1] * (i+1) % MD;

  reader(&K);
  rep(i,K) reader(C+i);

  res = solve();
  writerLn(res);
  
//  res1 = solve_brute();
//  writerLn(res,res1);
//  assert(res==res1);

  return 0;
}
0