結果

問題 No.125 悪の花弁
ユーザー 👑 hos.lyrichos.lyric
提出日時 2015-01-15 10:11:33
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 200 ms / 5,000 ms
コード長 2,727 bytes
コンパイル時間 1,622 ms
コンパイル使用メモリ 112,240 KB
実行使用メモリ 34,932 KB
最終ジャッジ日時 2023-09-02 19:40:00
合計ジャッジ時間 2,783 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
31,640 KB
testcase_01 AC 183 ms
33,580 KB
testcase_02 AC 197 ms
34,932 KB
testcase_03 AC 200 ms
34,424 KB
testcase_04 AC 180 ms
32,956 KB
testcase_05 AC 179 ms
32,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

//	Input
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }


immutable long MO = 10^^9 + 7;
immutable LIM = 10^^6 + 5;

long[] inv, fac, facInv;

int gcd(int a, int b) {
	return (b != 0) ? gcd(b, a % b) : a;
}

void main(string[] args) {
	inv = new long[LIM];
	inv[1] = 1;
	foreach (i; 2 .. LIM) {
		inv[i] = MO - MO / i * inv[cast(size_t)(MO % i)] % MO;
	}
	fac = new long[LIM];
	facInv = new long[LIM];
	fac[0] = facInv[0] = 1;
	foreach (i; 1 .. LIM) {
		fac[i] = fac[i - 1] * i % MO;
		facInv[i] = facInv[i - 1] * inv[i] % MO;
	}
	
	try {
	for (; ; ) {
		int K = readInt;
		int[] C = new int[K];
		foreach (k; 0 .. K) {
			C[k] = readInt;
		}
		
		const M = C.reduce!"a + b";
		int[] cnt = new int[M + 1];
		foreach (x; 0 .. M) {
			++cnt[gcd(x, M)];
		}
debug{
writeln("cnt = ",cnt[1..$]);
}
		long ans;
		foreach (d; 1 .. M + 1) if (cnt[d]) {
			bool ok = true;
			foreach (c; C) {
				ok = ok && (c % (M / d) == 0);
			}
			if (ok) {
				long tmp = fac[d];
				foreach (c; C) {
					(tmp *= facInv[c / (M / d)]) %= MO;
				}
debug{
writeln(d," ",cnt[d]," ",tmp);
}
				(ans += cnt[d] * tmp) %= MO;
			}
		}
		(ans *= inv[M]) %= MO;
		writeln(ans);
	}
	} catch (EOFException) {}
}
0