結果

問題 No.2262 Fractions
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-04-07 22:58:09
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 364 ms / 2,000 ms
コード長 2,818 bytes
コンパイル時間 1,593 ms
コンパイル使用メモリ 158,432 KB
実行使用メモリ 32,040 KB
最終ジャッジ日時 2023-09-04 20:08:30
合計ジャッジ時間 15,063 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 239 ms
16,720 KB
testcase_01 AC 90 ms
12,496 KB
testcase_02 AC 85 ms
12,428 KB
testcase_03 AC 86 ms
14,080 KB
testcase_04 AC 88 ms
12,544 KB
testcase_05 AC 83 ms
14,468 KB
testcase_06 AC 89 ms
12,496 KB
testcase_07 AC 86 ms
12,544 KB
testcase_08 AC 83 ms
13,876 KB
testcase_09 AC 87 ms
13,552 KB
testcase_10 AC 86 ms
12,496 KB
testcase_11 AC 227 ms
9,088 KB
testcase_12 AC 224 ms
9,096 KB
testcase_13 AC 227 ms
9,088 KB
testcase_14 AC 231 ms
9,068 KB
testcase_15 AC 225 ms
9,076 KB
testcase_16 AC 127 ms
9,072 KB
testcase_17 AC 124 ms
9,040 KB
testcase_18 AC 124 ms
9,136 KB
testcase_19 AC 335 ms
17,916 KB
testcase_20 AC 321 ms
17,932 KB
testcase_21 AC 340 ms
17,992 KB
testcase_22 AC 319 ms
17,848 KB
testcase_23 AC 278 ms
17,832 KB
testcase_24 AC 364 ms
31,604 KB
testcase_25 AC 353 ms
17,128 KB
testcase_26 AC 352 ms
31,768 KB
testcase_27 AC 362 ms
31,848 KB
testcase_28 AC 350 ms
32,040 KB
testcase_29 AC 352 ms
17,136 KB
testcase_30 AC 353 ms
17,668 KB
testcase_31 AC 361 ms
17,688 KB
testcase_32 AC 359 ms
17,124 KB
testcase_33 AC 352 ms
17,388 KB
testcase_34 AC 352 ms
17,184 KB
testcase_35 AC 362 ms
17,500 KB
testcase_36 AC 362 ms
17,176 KB
testcase_37 AC 25 ms
8,012 KB
testcase_38 AC 25 ms
7,968 KB
testcase_39 AC 345 ms
17,392 KB
testcase_40 AC 343 ms
17,428 KB
testcase_41 AC 347 ms
17,648 KB
testcase_42 AC 343 ms
16,948 KB
testcase_43 AC 343 ms
17,184 KB
testcase_44 AC 363 ms
17,176 KB
testcase_45 AC 354 ms
17,176 KB
権限があれば一括ダウンロードができます

ソースコード

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)); }


enum LIM = 3 * 10^^5 + 10;
int[] lpf, moe;

// 0 < p/q < 1, p/q <= t/N^2
long calc(long N, long t) {
  auto fs = new long[N + 1];
  foreach (q; 1 .. N + 1) {
    fs[q] = q * t / (N*N);
    chmin(fs[q], q - 1);
  }
  foreach (q; 1 .. N + 1) {
    fs[q] += fs[q - 1];
  }
  long ret;
  foreach (d; 1 .. N + 1) {
    if (moe[d] < 0) ret -= fs[N / d];
    if (moe[d] > 0) ret += fs[N / d];
  }
  return ret;
}

alias Frac = Tuple!(long, "p", long, "q");
Frac solve(long N, long K) {
  long lo = 0, hi = N*N;
  for (; lo + 1 < hi; ) {
    const mid = (lo + hi) / 2;
    ((calc(N, mid) >= K) ? hi : lo) = mid;
  }
  foreach (q; 1 .. N + 1) {
    const p = q * hi / (N*N);
    if (lo * q < p * (N*N)) {
      return Frac(p, q);
    }
  }
  assert(false);
}

void main() {
  lpf = iota(LIM).array;
  moe = new int[LIM];
  moe[1 .. $] = 1;
  foreach (p; 2 .. LIM) if (lpf[p] == p) {
    for (int n = p; n < LIM; n += p) {
      chmin(lpf[n], p);
      moe[n] = -moe[n];
      if (n / p % p == 0) moe[n] = 0;
    }
  }
  
  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const N = readLong;
        const K = readLong;
        
        Frac ans;
        const res = calc(N, N*N);
        if (K <= res) {
          ans = solve(N, K);
        } else if (K == res + 1) {
          ans = Frac(1, 1);
        } else if (K <= res + 1 + res) {
          ans = solve(N, (res + 1 + res) + 1 - K);
          swap(ans.p, ans.q);
        }
        
        if (ans.q) {
          writefln("%s/%s", ans.p, ans.q);
        } else {
          writeln(-1);
        }
      }
    }
  } catch (EOFException e) {
  }
}
0