結果

問題 No.125 悪の花弁
ユーザー 0w10w1
提出日時 2017-11-03 16:18:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 5,000 ms
コード長 1,297 bytes
コンパイル時間 2,059 ms
コンパイル使用メモリ 205,184 KB
実行使用メモリ 10,380 KB
最終ジャッジ日時 2023-08-14 16:36:15
合計ジャッジ時間 3,020 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
10,380 KB
testcase_01 AC 35 ms
10,088 KB
testcase_02 AC 36 ms
10,208 KB
testcase_03 AC 90 ms
10,100 KB
testcase_04 AC 9 ms
10,180 KB
testcase_05 AC 9 ms
10,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int MOD = int(1e9) + 7;
const int MAXK = 1 << 17;
const int MAXTOT = 1 << 20;

int inv_mod(int v, int m = MOD) {
  int r = 1;
  for (int p = m - 2; p; p >>= 1) {
    if (p & 1) r = 1LL * r * v % m;
    v = 1LL * v * v % m;
  }
  return r;
}

int fac[MAXTOT];

int K;
int C[MAXK];
int T;
int G;

int dp[MAXTOT];

signed main() {
  ios::sync_with_stdio(false);
  for (int i = fac[0] = 1; i < MAXTOT; ++i) {
    fac[i] = 1LL * fac[i - 1] * i % MOD;
  }
  cin >> K;
  for (int i = 0; i < K; ++i) {
    cin >> C[i];
    T += C[i];
    G = __gcd(G, C[i]);
  }
  vector<int> periods;
  for (int i = 1; i * i <= G; ++i) {
    if (G % i != 0) continue;
    periods.emplace_back(T / i);
    if (i * i == G) break;
    periods.emplace_back(T / (G / i));
  }
  sort(periods.begin(), periods.end());
  int ans = 0;
  for (int i = 0; i < periods.size(); ++i) {
    int p = periods[i];
    dp[p] = fac[p];
    for (int j = 0; j < K; ++j) {
      dp[p] = 1LL * dp[p] * inv_mod(fac[C[j] / (T / p)]) % MOD;
    }
    for (int j = 0; j < i; ++j) {
      int q = periods[j];
      if (p % q != 0) continue;
      (dp[p] -= dp[q]) %= MOD;
    }
    (ans += 1LL * dp[p] * inv_mod(p) % MOD) %= MOD;
  }
  if (ans < 0) ans += MOD;
  cout << ans << endl;
  return 0;
}
0