結果

問題 No.2262 Fractions
ユーザー とりゐとりゐ
提出日時 2023-03-09 11:05:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,013 ms / 2,000 ms
コード長 5,233 bytes
コンパイル時間 211 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 139,664 KB
最終ジャッジ日時 2024-05-05 07:16:16
合計ジャッジ時間 32,117 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 618 ms
105,456 KB
testcase_01 AC 251 ms
77,952 KB
testcase_02 AC 266 ms
76,544 KB
testcase_03 AC 282 ms
77,568 KB
testcase_04 AC 276 ms
77,184 KB
testcase_05 AC 255 ms
76,928 KB
testcase_06 AC 254 ms
77,824 KB
testcase_07 AC 259 ms
77,312 KB
testcase_08 AC 249 ms
76,928 KB
testcase_09 AC 273 ms
77,056 KB
testcase_10 AC 261 ms
77,312 KB
testcase_11 AC 587 ms
84,096 KB
testcase_12 AC 572 ms
83,840 KB
testcase_13 AC 642 ms
84,736 KB
testcase_14 AC 672 ms
85,120 KB
testcase_15 AC 578 ms
84,608 KB
testcase_16 AC 224 ms
79,488 KB
testcase_17 AC 231 ms
78,208 KB
testcase_18 AC 232 ms
78,592 KB
testcase_19 AC 904 ms
111,172 KB
testcase_20 AC 849 ms
108,152 KB
testcase_21 AC 877 ms
111,564 KB
testcase_22 AC 844 ms
108,352 KB
testcase_23 AC 725 ms
101,164 KB
testcase_24 AC 930 ms
137,344 KB
testcase_25 AC 962 ms
139,008 KB
testcase_26 AC 960 ms
135,424 KB
testcase_27 AC 984 ms
135,424 KB
testcase_28 AC 955 ms
137,344 KB
testcase_29 AC 1,013 ms
138,880 KB
testcase_30 AC 938 ms
136,192 KB
testcase_31 AC 988 ms
138,880 KB
testcase_32 AC 967 ms
136,704 KB
testcase_33 AC 970 ms
139,008 KB
testcase_34 AC 854 ms
137,344 KB
testcase_35 AC 470 ms
139,664 KB
testcase_36 AC 468 ms
139,028 KB
testcase_37 AC 86 ms
77,568 KB
testcase_38 AC 90 ms
77,312 KB
testcase_39 AC 834 ms
111,204 KB
testcase_40 AC 843 ms
110,308 KB
testcase_41 AC 854 ms
112,616 KB
testcase_42 AC 879 ms
111,964 KB
testcase_43 AC 847 ms
112,476 KB
testcase_44 AC 948 ms
135,936 KB
testcase_45 AC 953 ms
139,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd


class Fraction:
    def __init__(self, num, den=1):
        if type(num) == float or type(num) == str:
            assert den == 1
            if type(num) == float:
                S = str("{:.10f}".format(num))
            else:
                S = num
            N = len(S)
            point = -1
            for i in range(N):
                if S[i] == ".":
                    point = i
            if point != -1:
                F = Fraction(int(S[:point] + S[point + 1 :]), 10 ** (N - 1 - point))
            else:
                F = Fraction(int(S), 1)
            self.num = F.num
            self.den = F.den
        elif type(num) == type(den) == int:
            assert den != 0
            """
            inf = 10**9
            if den == 0:
                if num > 0:
                    self.num = inf
                    self.den = 1
                elif num < 0:
                    self.num = -inf
                    self.den = 1
                else:
                    raise Exception
                return
            """
            den, num = max((den, num), (-den, -num))
            g = gcd(den, num)
            self.num = num // g
            self.den = den // g
        else:
            raise Exception

    def __str__(self):
        return str(self.num) + "/" + str(self.den)

    def __pos__(self):
        return self

    def __neg__(self):
        num, den = self.num, self.den
        return Fraction(-num, den)

    def __add__(self, other):
        if type(other) != Fraction:
            other = Fraction(other)
        num1, den1 = self.num, self.den
        num2, den2 = other.num, other.den
        return Fraction(num1 * den2 + num2 * den1, den1 * den2)

    def __sub__(self, other):
        if type(other) != Fraction:
            other = Fraction(other)
        num1, den1 = self.num, self.den
        num2, den2 = other.num, other.den
        return Fraction(num1 * den2 - num2 * den1, den1 * den2)

    def __mul__(self, other):
        if type(other) != Fraction:
            other = Fraction(other)
        num1, den1 = self.num, self.den
        num2, den2 = other.num, other.den
        return Fraction(num1 * num2, den1 * den2)

    def __truediv__(self, other):
        if type(other) != Fraction:
            other = Fraction(other)
        num1, den1 = self.num, self.den
        num2, den2 = other.num, other.den
        return Fraction(num1 * den2, num2 * den1)

    def __pow__(self, x):
        assert type(x) == int
        num, den = self.num, self.den
        if x >= 0:
            return Fraction(num**x, den**x)
        else:
            return Fraction(den ** (-x), num ** (-x))

    __radd__ = __add__

    def __rsub__(self, other):
        if type(other) != Fraction:
            other = Fraction(other)
        num1, den1 = other.num, other.den
        num2, den2 = self.num, self.den
        return Fraction(num1 * den2 - num2 * den1, den1 * den2)

    __rmul__ = __mul__

    def __rtruediv__(self, other):
        if type(other) != Fraction:
            other = Fraction(other)
        num1, den1 = other.num, other.den
        num2, den2 = self.num, self.den
        return Fraction(num1 * den2, num2 * den1)

    def __eq__(self, other):
        if type(other) != Fraction:
            other = Fraction(other)
        num1, den1 = self.num, self.den
        num2, den2 = other.num, other.den
        return num1 == num2 and den1 == den2

    def __lt__(self, other):
        if type(other) != Fraction:
            other = Fraction(other)
        num1, den1 = self.num, self.den
        num2, den2 = other.num, other.den
        return num1 * den2 < num2 * den1

    def __le__(self, other):
        if type(other) != Fraction:
            other = Fraction(other)
        num1, den1 = self.num, self.den
        num2, den2 = other.num, other.den
        return num1 * den2 <= num2 * den1

    def __gt__(self, other):
        if type(other) != Fraction:
            other = Fraction(other)
        num1, den1 = self.num, self.den
        num2, den2 = other.num, other.den
        return num1 * den2 > num2 * den1

    def __ge__(self, other):
        if type(other) != Fraction:
            other = Fraction(other)
        num1, den1 = self.num, self.den
        num2, den2 = other.num, other.den
        return num1 * den2 >= num2 * den1
    
    def floor(self):
      return self.num//self.den

from sys import stdin
input=lambda :stdin.readline()[:-1]

def calc(n,a):
  dp=[0]*(n+1)
  for i in range(1,n+1):
    s=(a*i)//n
    dp[i]+=s
    if dp[i]==0:
      continue
    for j in range(2*i,n+1,i):
      dp[j]-=dp[i]
  return sum(dp)

def solve():
  n,k=map(int,input().split())
  c=calc(n,n-1)
  if k==c+1:
    print('1/1')
    return
  if k<=c:
    rev=False
  elif k<=2*c+1:
    rev=True
    k=2*c+2-k
  else:
    print(-1)
    return

  ng,ok=n,0
  while abs(ok-ng)>1:
    mid=(ok+ng)//2
    if calc(n,mid)<=k:
      ok=mid
    else:
      ng=mid

  k-=calc(n,ok)
  fracs=[]

  for p in range(1,n+1):
    q=(ok*p+n-1)//n
    if q*n<(ok+1)*p:
      if gcd(p,q)==1:
        fracs.append(Fraction(q,p))

  fracs.sort()
  if rev:
    print(1/fracs[k])
  else:
    print(fracs[k])

for _ in range(int(input())):
  solve()
0