結果

問題 No.117 組み合わせの数
ユーザー 👑 hos.lyrichos.lyric
提出日時 2015-01-06 22:00:07
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 3,680 bytes
コンパイル時間 9,106 ms
コンパイル使用メモリ 314,652 KB
実行使用メモリ 100,540 KB
最終ジャッジ日時 2024-06-12 01:58:59
合計ジャッジ時間 21,673 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
権限があれば一括ダウンロードができます

ソースコード

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 = 1_000_000_007;
immutable LIM = 2_000_005;

shared long[] inv, fac, facInv;

void prepare() {
	inv = new shared(long[LIM]);
	inv[1] = 1;
	foreach (i; 2 .. LIM) {
		inv[i] = MO - MO / i * inv[MO % i] % MO;
	}
	fac = new shared(long[LIM]);
	facInv = new shared(long[LIM]);
	fac[0] = 1;
	facInv[0] = 1;
	foreach (i; 1 .. LIM) {
		fac[i] = fac[i - 1] * i % MO;
		facInv[i] = facInv[i - 1] * inv[i] % MO;
	}
}

class Solver : core.thread.Thread {
	size_t caseId;
	this(size_t caseId) {
		this.caseId = caseId;
		super(&main);
	}
	void main() {
		try {
			run;
			stderr.writeln("DONE  Case #", caseId);
		} catch (Throwable e) {
			stderr.writeln("ERROR Case #", caseId, ": ", e.msg);
		} finally {
			stderr.flush;
		}
	}
	
	
	string S;
	long ans;
	
	void run() {
		auto ss = S.match(r"^([CPH])\((\d+),(\d+)\)$").captures;
debug{
writeln("ss = ",ss);
}
		int n = ss[2].to!int;
		int k = ss[3].to!int;
		switch (ss[1]) {
			case "C": {
				if (k <= n) {
					ans = fac[n] * facInv[k] % MO * facInv[n - k] % MO;
				} else {
					ans = 0;
				}
			} break;
			case "P": {
				if (k <= n) {
					ans = fac[n] * facInv[n - k] % MO;
				} else {
					ans = 0;
				}
			} break;
			case "H": {
				/*
					H(n, k) = (n (n + 1) ... (n + k - 1)) / k!
				*/
				if (n == 0) {
					ans = (k == 0) ? 1 : 0;
				} else {
					ans = fac[n + k - 1] * facInv[n - 1] % MO * facInv[k] % MO;
				}
			} break;
			default: assert(false);
		}
	}
	
	void input() {
		S = readToken;
	}
	
	void output() {
		writeln(ans);
		stdout.flush;
	}
}

void main(string[] args) {
	bool parallel = false;
parallel=true;
	foreach (arg; args) if (arg == "-p") parallel = true;
	prepare;
	auto solvers = new Solver[readInt];
	foreach (caseId, ref solver; solvers) solver = new Solver(caseId + 1);
	if (parallel) {
		foreach (solver; solvers) solver.input, solver.start;
		foreach (solver; solvers) solver.join, solver.output;
	} else {
		foreach (caseId, solver; solvers) {
			solver.input;
			solver.run;
			solver.output;
			stderr.writeln("DONE  Case #", caseId + 1);
			stderr.flush;
		}
	}
}
0