結果
| 問題 | No.1203 お菓子ゲーム |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-08-28 22:13:29 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 791 ms / 2,000 ms |
| コード長 | 3,767 bytes |
| 記録 | |
| コンパイル時間 | 764 ms |
| コンパイル使用メモリ | 119,320 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-22 08:26:43 |
| 合計ジャッジ時間 | 26,103 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 51 |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
enum L = 10L^^8;
long calc(const(long) X, const(long) Y) {
long ret;
/*
0 < j < i <= L
j = 2 k
X / Y = k / i
*/
if (Y > 2 * X) {
ret += L / Y;
}
/*
0 < j < i <= L
j = 2 k + 1
X / Y = (2 k (k + 1)) / (i (2 k + 1))
X > 0 ==> k > 0
*/
void check(long d) {
if (d >= 3 && d % 2 != 0) {
const k = (d - 1) / 2;
if ((2 * k * (k + 1)) % X == 0) {
const q = (2 * k * (k + 1)) / X;
if (Y / d <= L / q) {
const i = (Y / d) * q;
debug {
writefln("X/Y = %s/%s, d = %s, k = %s, i = %s", X, Y, d, k, i);
}
if (2 * k + 1 < i && i <= L) {
++ret;
}
}
}
}
}
for (long d = 1; d^^2 <= Y; ++d) {
if (Y % d == 0) {
check(d);
if (d != Y / d) {
check(Y / d);
}
}
}
return ret;
}
void main() {
debug {
enum lim = 10;
auto dp = new real[][](lim, lim);
foreach (i; 0 .. lim) foreach (j; 0 .. lim) {
if (i != j) {
dp[i][j] = 0.0L;
if (i > 0 && j > 0) {
if (i > j) {
dp[i][j] += (1.0L * (j - 1) / i) * dp[j - 1][j];
} else {
dp[i][j] += (1.0L * (i - 1) / j) * dp[i][i - 1];
dp[i][j] += (1.0L * (j - (i - 1)) / j);
}
}
}
}
foreach (i; 0 .. lim) {
writeln(i, ": ", dp[i]);
foreach (j; 1 .. i) {
real prob = 1.0L * (j^^2 / 2) / (i * j);
assert(abs(prob - dp[i][j]) <= 1e-12L);
}
if (i > 0) {
foreach (j; i + 1 .. lim) {
real prob = 1.0L - 1.0L * (i^^2 / 2) / (j * i);
assert(abs(prob - dp[i][j]) <= 1e-12L);
}
}
}
}
try {
for (; ; ) {
const numCases = readInt();
foreach (caseId; 0 .. numCases) {
const X = readLong();
const Y = readLong();
long ans;
ans += calc(X, Y);
ans += calc(Y - X, Y);
writeln(ans);
debug {
foreach (i; 1 .. 100) {
foreach (j; 1 .. i) {
if (j % 2 != 0) {
long p = j^^2 / 2;
long q = i * j;
const g = gcd(p, q);
p /= g;
q /= g;
if (p == X && q == Y) {
writeln("brt X/Y " , i, " ", j);
}
if (p == Y - X && q == Y) {
writeln("brt 1-X/Y " , i, " ", j);
}
}
}
}
}
}
}
} catch (EOFException e) {
}
}