結果

問題 No.125 悪の花弁
ユーザー Yang33Yang33
提出日時 2018-05-01 19:10:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 181 ms / 5,000 ms
コード長 3,351 bytes
コンパイル時間 2,217 ms
コンパイル使用メモリ 184,140 KB
実行使用メモリ 27,040 KB
最終ジャッジ日時 2023-09-10 08:45:23
合計ジャッジ時間 3,284 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
19,840 KB
testcase_01 AC 181 ms
20,360 KB
testcase_02 AC 77 ms
27,040 KB
testcase_03 AC 80 ms
26,712 KB
testcase_04 AC 41 ms
26,540 KB
testcase_05 AC 41 ms
26,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/05/01  Problem: yukicoder 125  / Link: http://yukicoder.me/problems/no/125  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----

蟻本読んでた

----解説ここまで---- */

map<int, int>MapMoebius(int n) {
	map<int, int>res;
	vector<int>primes;

	for (int i = 2; i*i <= n; i++) {
		if (n%i == 0) {
			primes.push_back(i);
			while (n%i == 0)n /= i;
		}
	}
	if (n != 1)primes.push_back(n);

	int m = (int)primes.size();
	for (int i = 0; i < (1 << m); i++) {//logn個も無いので余裕?
		int mu = 1, d = 1;
		for (int j = 0; j < m; j++) {
			if (i >> j & 1) {
				mu *= -1;
				d *= primes[j];
			}
		}
		res[d] = mu;
	}
	return res;
}


long long modpow(long long a, long long b) {
	if (b == 0) return 1;
	return modpow(a * a % MOD, b / 2) * (b & 1 ? a : 1) % MOD;
}

long long modinv(long long a) {
	return modpow(a, MOD - 2);
}

vector<long long> fact, inv_fact;

void init_fact(int n) {
	fact.resize(n);
	fact[0] = 1;
	for (int i = 1; i < n; i++) {
		fact[i] = i * fact[i - 1] % MOD;
	}
	inv_fact.resize(n);
	inv_fact[n - 1] = modinv(fact[n - 1]);
	for (int i = n - 2; i >= 0; i--) {
		inv_fact[i] = (i + 1) * inv_fact[i + 1] % MOD;
	}
}
VI getdivisor(int n) {
	VI res;
	for (int i = 1; i*i <= n; i++) {
		if (n%i == 0) {
			res.push_back(i);
			if (n / i != i)res.push_back(n / i);
		}
	}
	SORT(res);
	return res;
}
LL N;

LL ans = 0LL;

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int K; cin >> K;
	VI c(K);
	FOR(i, 0, K) {
		cin >> c[i];
	}
	N = accumulate(ALL(c), 0);
	map<int, int>moebious = MapMoebius(N);
	init_fact(N + 1);
	VL unit_c(N + 1, 0);
	FOR(i, 1, N + 1) {
		if (N%i == 0) {
			int ok = 1;
			int d = N / i;//groupのサイズi,グループの個数d
			FOR(j, 0, K) {
				if (c[j] % d)ok = 0;
			}
			if (!ok)continue;
			LL comb = fact[i];
			FOR(j, 0, K) {
				comb *= inv_fact[c[j] / d];
				comb %= MOD;
			}
			unit_c[i] = comb;
		}
	}

	FOR(i, 1, N + 1) {
		if (N%i == 0) {
			int ok = 1;
			int d = N / i;//groupのサイズi,グループの個数d
			FOR(j, 0, K) {
				if (c[j] % d)ok = 0;
			}
			if (!ok)continue;
			VI divs = getdivisor(i);
			LL ret = 0;
			for (int div : divs) {
				ret += (unit_c[div] * moebious[i / div]) % MOD;
				(ret += MOD) %= MOD;
			}
			ans += ret*(d);
			ans %= MOD;
		}
	}
	ans *= modinv(N); // ans/=N
	ans %= MOD;
	cout << ans << "\n";

	return 0;
}
0