結果

問題 No.2262 Fractions
ユーザー 👑 chro_96chro_96
提出日時 2023-04-08 09:29:18
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1,565 ms / 2,000 ms
コード長 3,212 bytes
コンパイル時間 1,200 ms
コンパイル使用メモリ 31,476 KB
実行使用メモリ 32,160 KB
最終ジャッジ日時 2023-08-18 00:40:32
合計ジャッジ時間 50,795 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 969 ms
31,056 KB
testcase_01 AC 219 ms
31,180 KB
testcase_02 AC 229 ms
31,180 KB
testcase_03 AC 231 ms
31,184 KB
testcase_04 AC 257 ms
31,104 KB
testcase_05 AC 228 ms
31,108 KB
testcase_06 AC 263 ms
31,064 KB
testcase_07 AC 257 ms
31,116 KB
testcase_08 AC 232 ms
31,148 KB
testcase_09 AC 251 ms
31,152 KB
testcase_10 AC 250 ms
31,156 KB
testcase_11 AC 932 ms
31,204 KB
testcase_12 AC 909 ms
31,248 KB
testcase_13 AC 923 ms
31,064 KB
testcase_14 AC 927 ms
31,172 KB
testcase_15 AC 924 ms
31,160 KB
testcase_16 AC 486 ms
31,120 KB
testcase_17 AC 491 ms
31,152 KB
testcase_18 AC 486 ms
31,108 KB
testcase_19 AC 1,412 ms
31,824 KB
testcase_20 AC 1,341 ms
31,920 KB
testcase_21 AC 1,418 ms
31,560 KB
testcase_22 AC 1,322 ms
31,700 KB
testcase_23 AC 1,139 ms
31,212 KB
testcase_24 AC 1,541 ms
32,160 KB
testcase_25 AC 1,546 ms
31,068 KB
testcase_26 AC 1,516 ms
31,440 KB
testcase_27 AC 1,540 ms
31,532 KB
testcase_28 AC 1,503 ms
31,008 KB
testcase_29 AC 1,560 ms
32,160 KB
testcase_30 AC 1,562 ms
31,988 KB
testcase_31 AC 1,542 ms
31,148 KB
testcase_32 AC 1,546 ms
31,116 KB
testcase_33 AC 1,557 ms
31,152 KB
testcase_34 AC 1,559 ms
32,124 KB
testcase_35 AC 1,555 ms
31,444 KB
testcase_36 AC 1,548 ms
31,072 KB
testcase_37 AC 85 ms
31,076 KB
testcase_38 AC 81 ms
31,064 KB
testcase_39 AC 1,439 ms
31,676 KB
testcase_40 AC 1,437 ms
31,844 KB
testcase_41 AC 1,447 ms
31,844 KB
testcase_42 AC 1,448 ms
31,844 KB
testcase_43 AC 1,445 ms
31,764 KB
testcase_44 AC 1,565 ms
32,152 KB
testcase_45 AC 1,556 ms
31,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

int cmp_fr (const void *a, const void *b) {
  long long *a_ = (long long *)a;
  long long *b_ = (long long *)b;
  
  if (a_[0]*b_[1] < a_[1]*b_[0]) {
    return -1;
  }
  
  if (a_[0]*b_[1] > a_[1]*b_[0]) {
    return 1;
  }
  
  return 0;
}

int main () {
  int t = 0;
  
  int res = 0;
  
  long long ps[300001][10] = {};
  int pcnt[300001] = {};
  long long fr[300000][2] = {};
  
  for (int i = 2; i <= 300000; i++) {
    if (pcnt[i] <= 0) {
      ps[i][0] = (long long)i;
      pcnt[i] = 1;
      for (int p = 2; i*p <= 300000; p++) {
        ps[i*p][pcnt[i*p]] = i;
        pcnt[i*p]++;
      }
    }
  }
  
  res = scanf("%d", &t);
  while (t > 0) {
    long long n = 0LL;
    long long k = 0LL;
    long long rcnt = 1LL;
    res = scanf("%lld", &n);
    res = scanf("%lld", &k);
    rcnt = n*n;
    for (long long i = 1LL; i <= n; i += 1LL) {
      for (int j = 1; j < (1<<pcnt[(int)i]); j++) {
        int bcnt = 0;
        long long p = 1LL;
        for (int l = 0; l < pcnt[(int)i]; l++) {
          if ((j&(1<<l)) > 0) {
            p *= ps[(int)i][l];
            bcnt++;
          }
        }
        if (bcnt%2 == 1) {
          rcnt -= n/p;
        } else {
          rcnt += n/p;
        }
      }
    }
    if (rcnt < k) {
      printf("-1\n");
    } else {
      long long ans[2] = {};
      long long l = 0LL;
      long long r = (1LL<<19LL);
      long long div = 1LL;
      long long lcnt = 0LL;
      int frcnt = 0;
      int ucnt = 1;
      while (div <= n) {
        long long nxt = l+r;
        long long cnt = 0LL;
        if (nxt%2LL == 1LL) {
          div *= 2LL;
          l *= 2LL;
          r *= 2LL;
        } else {
          nxt /= 2LL;
        }
        for (long long i = 1LL; i <= n; i += 1LL) {
          long long d = (nxt*i)/div;
          long long diff = 0LL;
          if (d > n) {
            d = n;
          }
          for (int j = 1; j < (1<<pcnt[(int)i]); j++) {
            int bcnt = 0;
            long long p = 1LL;
            for (int l = 0; l < pcnt[(int)i]; l++) {
              if ((j&(1<<l)) > 0) {
                p *= ps[(int)i][l];
                bcnt++;
              }
            }
            if (bcnt%2 == 1) {
              diff += d/p;
            } else {
              diff -= d/p;
            }
          }
          cnt += d-diff;
        }
        if (cnt < k) {
          l = nxt;
          lcnt = cnt;
        } else {
          r = nxt;
          rcnt = cnt;
        }
      }
      for (long long i = 1LL; i <= n; i += 1LL) {
        long long d = (r*i)/div;
        if (d <= n && l*i < d*div) {
          fr[frcnt][0] = d;
          fr[frcnt][1] = i;
          frcnt++;
        }
      }
      qsort(fr, frcnt, sizeof(long long)*2, cmp_fr);
      for (int i = 1; i < frcnt; i++) {
        if (fr[ucnt-1][0]*fr[i][1] != fr[ucnt-1][1]*fr[i][0]) {
          fr[ucnt][0] = fr[i][0];
          fr[ucnt][1] = fr[i][1];
          ucnt++;
        } else if (fr[ucnt-1][0]%fr[i][0] == 0LL) {
          fr[ucnt-1][0] = fr[i][0];
          fr[ucnt-1][1] = fr[i][1];
        }
      }
      printf("%lld/%lld\n", fr[(int)(k-lcnt-1LL)][0], fr[(int)(k-lcnt-1LL)][1]);
    }
    t--;
  }
  
  return 0;
}
0