結果

問題 No.125 悪の花弁
ユーザー assy1028assy1028
提出日時 2016-12-19 17:25:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 234 ms / 5,000 ms
コード長 2,539 bytes
コンパイル時間 2,503 ms
コンパイル使用メモリ 99,136 KB
実行使用メモリ 26,664 KB
最終ジャッジ日時 2023-08-21 01:18:51
合計ジャッジ時間 2,675 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 150 ms
25,840 KB
testcase_01 AC 158 ms
26,664 KB
testcase_02 AC 228 ms
24,828 KB
testcase_03 AC 234 ms
24,568 KB
testcase_04 AC 219 ms
24,020 KB
testcase_05 AC 219 ms
23,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int k;
ll c[100100];
ll sum = 0;
ll d[1000100];

ll mod_pow(ll x, ll n) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = (res * x) % mod;
        x = (x * x) % mod;
        n >>= 1;
    }
    return res;
}

ll mod_inverse(ll x) {
    return mod_pow(x, mod-2);
}

const int max_n = 1000100;
ll fact[max_n];
ll fact_inv[max_n];
void calc_fact() {
    fact[0] = 1;
    for (ll i = 1; i < max_n; i++)
        fact[i] = (fact[i-1] * i) % mod;
    fact_inv[max_n-1] = mod_inverse(fact[max_n-1]);
    for (ll i = max_n-2; i >= 0; i--)
        fact_inv[i] = (fact_inv[i+1] * (i+1)) % mod;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a%b);
}

void calc(int t) {
    d[t] = fact[sum/t];
    for (int j = 0; j < k; j++) {
        if (c[j] % t) {
            d[t] = 0;
            break;
        } else {
            d[t] = d[t] * fact_inv[c[j]/t] % mod;
        }
    }
}

ll solve() {
    for (int i = 0; i < k; i++) sum += c[i];
    calc_fact();
    for (int i = 1; i * i <= sum; i++) {
        if (sum % i == 0) {
            calc(i);
            calc(sum/i);
        }
    }
    swap(d[0], d[1]);
    ll res = d[0];
    for (int i = 1; i < sum; i++) {
        res = (res + d[sum/gcd(sum, i)]) % mod;
    }
    return res * mod_inverse(sum) % mod;
}

void input() {
    cin >> k;
    for (int i = 0; i < k; i++) cin >> c[i];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}
0