結果

問題 No.2262 Fractions
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-04-07 22:32:15
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 882 ms / 2,000 ms
コード長 3,416 bytes
コンパイル時間 1,735 ms
コンパイル使用メモリ 162,424 KB
実行使用メモリ 16,844 KB
最終ジャッジ日時 2023-09-04 20:07:07
合計ジャッジ時間 29,416 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 508 ms
12,304 KB
testcase_01 AC 105 ms
4,376 KB
testcase_02 AC 103 ms
4,380 KB
testcase_03 AC 105 ms
4,380 KB
testcase_04 AC 111 ms
4,380 KB
testcase_05 AC 104 ms
4,376 KB
testcase_06 AC 117 ms
4,380 KB
testcase_07 AC 111 ms
4,376 KB
testcase_08 AC 106 ms
4,380 KB
testcase_09 AC 116 ms
4,380 KB
testcase_10 AC 113 ms
4,380 KB
testcase_11 AC 454 ms
6,908 KB
testcase_12 AC 443 ms
6,844 KB
testcase_13 AC 453 ms
6,764 KB
testcase_14 AC 459 ms
6,832 KB
testcase_15 AC 450 ms
6,840 KB
testcase_16 AC 219 ms
6,836 KB
testcase_17 AC 217 ms
6,832 KB
testcase_18 AC 213 ms
6,880 KB
testcase_19 AC 740 ms
13,452 KB
testcase_20 AC 716 ms
13,516 KB
testcase_21 AC 757 ms
13,320 KB
testcase_22 AC 707 ms
13,908 KB
testcase_23 AC 578 ms
13,952 KB
testcase_24 AC 860 ms
12,240 KB
testcase_25 AC 872 ms
12,600 KB
testcase_26 AC 815 ms
13,736 KB
testcase_27 AC 819 ms
13,820 KB
testcase_28 AC 849 ms
12,816 KB
testcase_29 AC 882 ms
12,256 KB
testcase_30 AC 835 ms
13,756 KB
testcase_31 AC 879 ms
12,308 KB
testcase_32 AC 833 ms
13,756 KB
testcase_33 AC 872 ms
12,700 KB
testcase_34 AC 839 ms
13,924 KB
testcase_35 AC 793 ms
12,548 KB
testcase_36 AC 801 ms
13,764 KB
testcase_37 AC 42 ms
5,252 KB
testcase_38 AC 43 ms
5,892 KB
testcase_39 AC 735 ms
16,648 KB
testcase_40 AC 739 ms
16,828 KB
testcase_41 AC 739 ms
16,828 KB
testcase_42 AC 780 ms
16,692 KB
testcase_43 AC 744 ms
16,844 KB
testcase_44 AC 839 ms
13,816 KB
testcase_45 AC 872 ms
13,536 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/dmd2/linux/bin64/../../src/phobos/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!ulong.gcdImpl`

ソースコード

diff #

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