結果

問題 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
コンパイル時間 462 ms
コンパイル使用メモリ 33,076 KB
実行使用メモリ 32,512 KB
最終ジャッジ日時 2024-05-05 07:31:16
合計ジャッジ時間 49,590 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 997 ms
31,232 KB
testcase_01 AC 236 ms
31,104 KB
testcase_02 AC 237 ms
30,976 KB
testcase_03 AC 246 ms
30,976 KB
testcase_04 AC 263 ms
31,104 KB
testcase_05 AC 251 ms
30,976 KB
testcase_06 AC 271 ms
31,104 KB
testcase_07 AC 260 ms
31,104 KB
testcase_08 AC 243 ms
30,976 KB
testcase_09 AC 267 ms
31,104 KB
testcase_10 AC 266 ms
31,104 KB
testcase_11 AC 932 ms
31,232 KB
testcase_12 AC 936 ms
31,104 KB
testcase_13 AC 950 ms
31,232 KB
testcase_14 AC 970 ms
31,104 KB
testcase_15 AC 941 ms
31,232 KB
testcase_16 AC 496 ms
31,104 KB
testcase_17 AC 506 ms
31,104 KB
testcase_18 AC 482 ms
31,104 KB
testcase_19 AC 1,421 ms
32,000 KB
testcase_20 AC 1,339 ms
31,872 KB
testcase_21 AC 1,440 ms
31,584 KB
testcase_22 AC 1,315 ms
31,744 KB
testcase_23 AC 1,142 ms
31,360 KB
testcase_24 AC 1,546 ms
32,384 KB
testcase_25 AC 1,557 ms
31,488 KB
testcase_26 AC 1,561 ms
31,616 KB
testcase_27 AC 1,538 ms
31,744 KB
testcase_28 AC 1,501 ms
31,360 KB
testcase_29 AC 1,547 ms
32,384 KB
testcase_30 AC 1,539 ms
32,512 KB
testcase_31 AC 1,564 ms
31,200 KB
testcase_32 AC 1,546 ms
31,104 KB
testcase_33 AC 1,562 ms
31,360 KB
testcase_34 AC 1,548 ms
32,384 KB
testcase_35 AC 1,556 ms
31,616 KB
testcase_36 AC 1,565 ms
31,104 KB
testcase_37 AC 85 ms
31,104 KB
testcase_38 AC 83 ms
30,976 KB
testcase_39 AC 1,491 ms
31,744 KB
testcase_40 AC 1,435 ms
31,712 KB
testcase_41 AC 1,440 ms
31,840 KB
testcase_42 AC 1,430 ms
31,760 KB
testcase_43 AC 1,430 ms
31,840 KB
testcase_44 AC 1,543 ms
32,256 KB
testcase_45 AC 1,536 ms
31,232 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