結果

問題 No.2262 Fractions
ユーザー とりゐとりゐ
提出日時 2023-04-07 21:25:40
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,513 bytes
コンパイル時間 136 ms
コンパイル使用メモリ 82,520 KB
実行使用メモリ 89,480 KB
最終ジャッジ日時 2024-04-10 16:34:05
合計ジャッジ時間 23,076 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
76,380 KB
testcase_01 TLE -
testcase_02 AC 1,830 ms
86,824 KB
testcase_03 AC 1,696 ms
88,056 KB
testcase_04 AC 1,716 ms
88,632 KB
testcase_05 AC 1,552 ms
87,168 KB
testcase_06 AC 1,545 ms
87,972 KB
testcase_07 AC 1,488 ms
85,556 KB
testcase_08 AC 1,434 ms
85,708 KB
testcase_09 AC 1,541 ms
87,864 KB
testcase_10 AC 1,466 ms
88,288 KB
testcase_11 AC 217 ms
77,420 KB
testcase_12 AC 215 ms
77,212 KB
testcase_13 AC 224 ms
77,648 KB
testcase_14 AC 211 ms
77,676 KB
testcase_15 AC 217 ms
77,456 KB
testcase_16 AC 141 ms
76,528 KB
testcase_17 AC 146 ms
76,580 KB
testcase_18 AC 150 ms
76,900 KB
testcase_19 AC 125 ms
76,544 KB
testcase_20 AC 124 ms
76,792 KB
testcase_21 AC 126 ms
76,956 KB
testcase_22 AC 126 ms
76,400 KB
testcase_23 AC 134 ms
76,648 KB
testcase_24 AC 107 ms
76,672 KB
testcase_25 AC 100 ms
76,396 KB
testcase_26 AC 120 ms
76,744 KB
testcase_27 AC 120 ms
76,396 KB
testcase_28 AC 107 ms
76,244 KB
testcase_29 AC 106 ms
76,628 KB
testcase_30 AC 102 ms
76,332 KB
testcase_31 AC 105 ms
76,184 KB
testcase_32 AC 109 ms
76,392 KB
testcase_33 AC 108 ms
76,396 KB
testcase_34 AC 102 ms
76,652 KB
testcase_35 AC 96 ms
76,288 KB
testcase_36 AC 97 ms
76,512 KB
testcase_37 AC 64 ms
65,664 KB
testcase_38 AC 63 ms
65,280 KB
testcase_39 AC 130 ms
77,180 KB
testcase_40 AC 130 ms
76,700 KB
testcase_41 AC 139 ms
76,752 KB
testcase_42 AC 128 ms
76,796 KB
testcase_43 AC 124 ms
77,312 KB
testcase_44 AC 108 ms
76,108 KB
testcase_45 AC 112 ms
76,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def stern_brocot_tree_search(max_val):
  lower=(1,1)
  upper=(0,1)
  now=(1,1)
  while True:
    now=(lower[0]+upper[0],lower[1]+upper[1])
    now_judge=judge_function(now)
    if now_judge:
      frm=lower
      to=upper
    else:
      frm=upper
      to=lower
    
    L=1
    R=2
    while judge_function((frm[0]+R*to[0],frm[1]+R*to[1]))==now_judge:
      L*=2
      R*=2
      if frm[0]+L*to[0]>max_val or frm[1]+L*to[1]>max_val:
        return to
    while L+1<R:
      mid=(L+R)//2
      if judge_function((frm[0]+mid*to[0],frm[1]+mid*to[1]))==now_judge and frm[0]+mid*to[0]<=max_val and frm[1]+mid*to[1]<=max_val:
        L=mid
      else:
        R=mid
    
    if now_judge:
      lower=(frm[0]+L*to[0],frm[1]+L*to[1])
    else:
      upper=(frm[0]+L*to[0],frm[1]+L*to[1])

def quotient_range(n):
  ret=[]
  i=1
  while i<=n:
    q=n//i
    j=n//q+1
    ret.append((i,j))
    i=j
  return ret

def floor_sum(n,m,a,b):
  ans=0
  while True:
    if a>=m:
      ans+=(n-1)*n*(a//m)//2
      a%=m
    if b>=m:
      ans+=n*(b//m)
      b%=m
    y_max=(a*n+b)//m
    x_max=(y_max*m-b)
    if y_max==0:
      return ans
    ans+=(n-(x_max+a-1)//a)*y_max
    n,m,a,b=y_max,a,m,(a-x_max%a)%a

def mertens_table(n):
  if n==0:
    return [[0],[0]]
  b=10**4
  small=[1]*(n//b+1)
  large=[1]*(b+1)
  small[0]=0
  large[0]=0
  
  prime=[1]*(n//b+1)
  for p in range(2,n//b+1):
    if not prime[p]:
      continue
    for j in range(p,n//b+1,p):
      if j>p:
        prime[j]=0
      if j%(p*p)==0:
        small[j]=0
      else:
        small[j]*=-1
  
  for i in range(1,n//b):
    small[i+1]+=small[i]
  
  for i in range(b,0,-1):
    l=2
    while l<=n//i:
      q=n//(i*l)
      r=n//(i*q)+1
      if i*l<=b:
        large[i]-=large[i*l]*(r-l)
      else:
        large[i]-=small[n//(i*l)]*(r-l)
      l=r
  return small,large

def range_g(L,R,P,Q):
  return floor_sum(R-L,Q,P,L*P)

def count(n,P,Q):
  res=0
  for L,R in QR:
    x=n//L
    if x<len(small):
      res+=small[x]*range_g(L,R,P,Q)
    else:
      res+=large[n//x]*range_g(L,R,P,Q)
  return res

def judge_function(res):
  P,Q=res
  return count(n,P,Q)>=k

for _ in range(int(input())):
  n,k=map(int,input().split())
  small,large=mertens_table(n)
  QR=quotient_range(n)
  c=count(n,n-1,n)
  if k==c+1:
    print('1/1')
    continue
  if k<=c:
    rev=False
  elif k<=2*c+1:
    rev=True
    k=2*c+2-k
  else:
    print(-1)
    continue
  ans=stern_brocot_tree_search(n)
  num,den=ans
  if rev:
    den,num=num,den
  print(str(num)+'/'+str(den))
0