結果

問題 No.125 悪の花弁
ユーザー 👑 hos.lyric
提出日時 2015-01-15 10:11:33
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 218 ms / 5,000 ms
コード長 2,727 bytes
コンパイル時間 824 ms
コンパイル使用メモリ 123,776 KB
実行使用メモリ 32,364 KB
最終ジャッジ日時 2024-06-12 02:03:18
合計ジャッジ時間 2,886 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 6
権限があれば一括ダウンロードができます

ソースコード

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) {}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0