結果
問題 | No.2262 Fractions |
ユーザー | 👑 hos.lyric |
提出日時 | 2023-04-07 22:32:15 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 829 ms / 2,000 ms |
コード長 | 3,416 bytes |
コンパイル時間 | 1,942 ms |
コンパイル使用メモリ | 164,220 KB |
実行使用メモリ | 16,392 KB |
最終ジャッジ日時 | 2024-06-22 17:43:56 |
合計ジャッジ時間 | 26,935 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 483 ms
11,872 KB |
testcase_01 | AC | 97 ms
6,812 KB |
testcase_02 | AC | 94 ms
6,944 KB |
testcase_03 | AC | 95 ms
6,944 KB |
testcase_04 | AC | 101 ms
6,944 KB |
testcase_05 | AC | 95 ms
6,940 KB |
testcase_06 | AC | 107 ms
6,944 KB |
testcase_07 | AC | 101 ms
6,944 KB |
testcase_08 | AC | 95 ms
6,944 KB |
testcase_09 | AC | 104 ms
6,940 KB |
testcase_10 | AC | 104 ms
6,940 KB |
testcase_11 | AC | 415 ms
6,940 KB |
testcase_12 | AC | 408 ms
6,944 KB |
testcase_13 | AC | 414 ms
6,944 KB |
testcase_14 | AC | 423 ms
6,940 KB |
testcase_15 | AC | 415 ms
6,940 KB |
testcase_16 | AC | 200 ms
6,944 KB |
testcase_17 | AC | 198 ms
6,944 KB |
testcase_18 | AC | 196 ms
6,940 KB |
testcase_19 | AC | 697 ms
13,260 KB |
testcase_20 | AC | 665 ms
13,080 KB |
testcase_21 | AC | 708 ms
13,120 KB |
testcase_22 | AC | 665 ms
13,772 KB |
testcase_23 | AC | 542 ms
13,680 KB |
testcase_24 | AC | 804 ms
13,204 KB |
testcase_25 | AC | 815 ms
12,072 KB |
testcase_26 | AC | 768 ms
13,396 KB |
testcase_27 | AC | 778 ms
13,400 KB |
testcase_28 | AC | 801 ms
12,892 KB |
testcase_29 | AC | 826 ms
12,272 KB |
testcase_30 | AC | 790 ms
13,556 KB |
testcase_31 | AC | 829 ms
11,952 KB |
testcase_32 | AC | 786 ms
13,516 KB |
testcase_33 | AC | 823 ms
12,560 KB |
testcase_34 | AC | 782 ms
13,544 KB |
testcase_35 | AC | 742 ms
12,720 KB |
testcase_36 | AC | 740 ms
12,964 KB |
testcase_37 | AC | 39 ms
6,940 KB |
testcase_38 | AC | 40 ms
6,940 KB |
testcase_39 | AC | 696 ms
16,392 KB |
testcase_40 | AC | 694 ms
16,344 KB |
testcase_41 | AC | 695 ms
16,384 KB |
testcase_42 | AC | 728 ms
16,296 KB |
testcase_43 | AC | 694 ms
16,368 KB |
testcase_44 | AC | 790 ms
13,548 KB |
testcase_45 | AC | 826 ms
13,260 KB |
コンパイルメッセージ
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!ulong.gcdImpl`
ソースコード
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)); } alias Frac = Tuple!(long, "p", long, "q"); int cmp(Frac a, Frac b) { const d = a.p * b.q - b.p * a.q; return (d < 0) ? -1 : (d > 0) ? +1 : 0; } void main() { try { for (; ; ) { const numCases = readInt; foreach (caseId; 0 .. numCases) { const N = readInt; const K = readLong; // p/q <= l/N long calc(int l) { auto fs = new long[N + 1]; foreach (d; 1 .. N + 1) { const n = N / d; if (d > 1 && N / (d - 1) == n) { fs[d] = fs[d - 1]; } else { foreach (q; 1 .. n + 1) { fs[d] += min(q - 1, cast(long)(q) * l / N); } } } foreach_reverse (d; 1 .. N + 1) { for (int e = 2 * d; e <= N; e += d) { fs[d] -= fs[e]; } } debug { writefln("calc %s = %s", l, fs[1]); } return fs[1]; } Frac solve(long k) { int lo = 0, hi = N; for (; lo + 1 < hi; ) { const mid = (lo + hi) / 2; ((calc(mid) >= k) ? hi : lo) = mid; } debug { writefln("solve %s: lo = %s, hi = %s", k, lo, hi); } k -= calc(lo); const a0 = Frac(lo, N); Frac[] bs; foreach (q; 1 .. N + 1) { const p = cast(long)(q) * hi / N; if (0 < p && p < q && gcd(p, q) == 1) { const b = Frac(p, q); if (cmp(a0, b) < 0) { bs ~= b; } } } bs.sort!((a, b) => (cmp(a, b) < 0)); debug { writeln("bs = ", bs); } return bs[k - 1]; } const num = calc(N); Frac ans; if (K <= num) { ans = solve(K); } else if (K == num + 1) { ans = Frac(1, 1); } else if (K <= num + 1 + num) { ans = solve((num + 1 + num) + 1 - K); swap(ans.p, ans.q); } if (ans.q) { writefln("%s/%s", ans.p, ans.q); } else { writeln(-1); } } } } catch (EOFException e) { } }