結果

問題 No.125 悪の花弁
ユーザー antaanta
提出日時 2015-01-12 00:19:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 170 ms / 5,000 ms
コード長 5,243 bytes
コンパイル時間 1,082 ms
コンパイル使用メモリ 106,100 KB
実行使用メモリ 19,776 KB
最終ジャッジ日時 2024-06-22 04:11:15
合計ジャッジ時間 1,931 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
14,992 KB
testcase_01 AC 170 ms
15,488 KB
testcase_02 AC 73 ms
19,776 KB
testcase_03 AC 74 ms
19,748 KB
testcase_04 AC 37 ms
19,352 KB
testcase_05 AC 36 ms
19,492 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:164:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  164 |         scanf("%d", &K);
      |         ~~~~~^~~~~~~~~~
main.cpp:166:24: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  166 |         rep(i, K) scanf("%d", &C[i]);
      |                   ~~~~~^~~~~~~~~~~~~

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

template<int MOD>
struct ModInt {
	static const int Mod = MOD;
	unsigned x;
	ModInt(): x(0) { }
	ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }
	
	ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
	ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
	
	ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
	ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
	ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
	ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
	
	ModInt inverse() const {
		long long a = x, b = MOD, u = 1, v = 0;
		while(b) {
			long long t = a / b;
			a -= t * b; std::swap(a, b);
			u -= t * v; std::swap(u, v);
		}
		return ModInt(u);
	}
};
typedef ModInt<1000000007> mint;

vector<mint> fact, factinv;
void nCr_computeFactinv(int N) {
	N = min(N, mint::Mod - 1);
	fact.resize(N+1); factinv.resize(N+1);
	fact[0] = 1;
	rer(i, 1, N) fact[i] = fact[i-1] * i;
	factinv[N] = fact[N].inverse();
	for(int i = N; i >= 1; i --) factinv[i-1] = factinv[i] * i;
}

vector<bool> isprime;
vector<int> primes;
void sieve(int n){
	if((int)isprime.size() >= n+1) return;
	isprime.assign(n+1, true);
	isprime[0] = isprime[1] = false;
	int sqrtn = (int)(sqrt(n * 1.) + .5);
	for(int i = 2; i <= sqrtn; i ++) if(isprime[i]) {
		for(int j = i * i; j <= n; j += i)
			isprime[j] = false;
	}
	primes.clear();
	for(int i = 2; i <= n; i ++) if(isprime[i])
		primes.push_back(i);
}

vector<int> mobiusMu;
void calcMobiusMu() {
	int n = (int)isprime.size() - 1;
	mobiusMu.assign(n+1, 1);
	for(int i = 2; i <= n; i ++) if(isprime[i]) {
		if((ll)i * i <= n) {
			for(int j = i * i; j <= n; j += i * i)
				mobiusMu[j] = 0;
		}
		for(int j = i; j <= n; j += i)
			mobiusMu[j] *= -1;
	}
}

typedef int FactorsInt;
typedef vector<pair<FactorsInt,int> > Factors;

void primeFactors(FactorsInt x, Factors &out_v) {
	out_v.clear();
	int sqrtx = (int)(sqrt(x*1.) + 10.5);
	sieve(sqrtx);
	for(vector<int>::const_iterator p = primes.begin(); p != primes.end(); ++ p) {
		if(*p > sqrtx) break;
		if(x % *p == 0) {
			int t = 1;
			x /= *p;
			while(x % *p == 0) {
				t ++;
				x /= *p;
			}
			out_v.push_back(make_pair(*p, t));
		}
	}
	if(x != 1) out_v.push_back(make_pair(x, 1));
}

void getDivisors(FactorsInt x, vector<FactorsInt> &out_v) {
	Factors fs;
	primeFactors(x, fs);
	out_v.assign(1, 1);
	rep(i, fs.size()) {
		for(int j = (int)out_v.size()-1; j >= 0; j --) {
			FactorsInt x = out_v[j];
			rep(k, fs[i].second) {
				x *= fs[i].first;
				out_v.push_back(x);
			}
		}
	}
	sort(all(out_v));
}

vector<vi> allv;
void brute(vi &C, vi &v) {
	bool emp = true;
	rep(i, C.size()) if(C[i] > 0) {
		emp = false;
		-- C[i];
		v.push_back(i);
		brute(C, v);
		v.pop_back();
		++ C[i];
	}
	if(emp)
		allv.pb(v);
}

int main() {
	int K;
	scanf("%d", &K);
	vector<int> C(K);
	rep(i, K) scanf("%d", &C[i]);

	int N = accumulate(all(C), 0);
	nCr_computeFactinv(N);
	sieve(N);
	calcMobiusMu();
	vector<mint> cnts(N+1);
	rer(p, 1, N) if(N % p == 0) {
		bool ok = true;
		rep(i, K)
			ok &= C[i] % (N / p) == 0;
		if(!ok) continue;
		mint y = fact[p];
		rep(i, K)
			y *= factinv[C[i] / (N / p)];
		cnts[p] = y;
	}
	mint ans = 0;
	rer(p, 1, N) if(N % p == 0) {
		//ちょうどp個の回転で同じになるようなものを数える
		//= 最小periodがちょうどp
		//periodがpの約数であるのは数えられるので包除原理したい
		bool ok = true;
		rep(i, K)
			ok &= C[i] % (N / p) == 0;
		if(!ok) continue;
		vector<int> divs;
		getDivisors(p, divs);
		mint x = 0;
		each(d, divs)
			x += cnts[*d] * mobiusMu[p / *d];
		ans += x * (N / p);
	}
	ans /= N;
	printf("%d\n", ans.get());
	return 0;
}
0