結果

問題 No.2262 Fractions
ユーザー とりゐとりゐ
提出日時 2023-03-09 11:05:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,065 ms / 2,000 ms
コード長 5,233 bytes
コンパイル時間 730 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 141,424 KB
最終ジャッジ日時 2023-08-18 00:25:15
合計ジャッジ時間 35,310 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 666 ms
106,296 KB
testcase_01 AC 293 ms
78,904 KB
testcase_02 AC 281 ms
79,648 KB
testcase_03 AC 312 ms
79,736 KB
testcase_04 AC 312 ms
79,200 KB
testcase_05 AC 298 ms
80,148 KB
testcase_06 AC 295 ms
79,184 KB
testcase_07 AC 304 ms
79,464 KB
testcase_08 AC 298 ms
78,908 KB
testcase_09 AC 309 ms
78,808 KB
testcase_10 AC 298 ms
79,232 KB
testcase_11 AC 624 ms
85,832 KB
testcase_12 AC 627 ms
85,492 KB
testcase_13 AC 638 ms
85,724 KB
testcase_14 AC 640 ms
86,100 KB
testcase_15 AC 634 ms
85,060 KB
testcase_16 AC 267 ms
81,140 KB
testcase_17 AC 265 ms
81,064 KB
testcase_18 AC 263 ms
80,752 KB
testcase_19 AC 955 ms
113,036 KB
testcase_20 AC 895 ms
109,784 KB
testcase_21 AC 953 ms
114,388 KB
testcase_22 AC 903 ms
110,516 KB
testcase_23 AC 768 ms
102,016 KB
testcase_24 AC 978 ms
136,604 KB
testcase_25 AC 1,024 ms
139,864 KB
testcase_26 AC 1,014 ms
136,544 KB
testcase_27 AC 1,024 ms
135,060 KB
testcase_28 AC 1,038 ms
138,276 KB
testcase_29 AC 1,065 ms
139,756 KB
testcase_30 AC 1,018 ms
137,484 KB
testcase_31 AC 1,065 ms
140,256 KB
testcase_32 AC 998 ms
137,848 KB
testcase_33 AC 1,062 ms
140,452 KB
testcase_34 AC 930 ms
138,488 KB
testcase_35 AC 527 ms
141,424 KB
testcase_36 AC 527 ms
141,324 KB
testcase_37 AC 108 ms
78,980 KB
testcase_38 AC 111 ms
79,044 KB
testcase_39 AC 924 ms
111,648 KB
testcase_40 AC 947 ms
111,624 KB
testcase_41 AC 950 ms
111,248 KB
testcase_42 AC 967 ms
112,924 KB
testcase_43 AC 913 ms
111,332 KB
testcase_44 AC 1,022 ms
137,884 KB
testcase_45 AC 1,049 ms
140,468 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