結果

問題 No.2266 Fractions (hard)
ユーザー とりゐとりゐ
提出日時 2023-04-07 21:39:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 792 ms / 6,000 ms
コード長 2,502 bytes
コンパイル時間 472 ms
コンパイル使用メモリ 82,292 KB
実行使用メモリ 77,952 KB
最終ジャッジ日時 2024-04-14 11:52:32
合計ジャッジ時間 13,666 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
76,048 KB
testcase_01 AC 452 ms
77,904 KB
testcase_02 AC 341 ms
77,728 KB
testcase_03 AC 42 ms
58,864 KB
testcase_04 AC 64 ms
65,608 KB
testcase_05 AC 71 ms
73,520 KB
testcase_06 AC 134 ms
76,480 KB
testcase_07 AC 97 ms
75,824 KB
testcase_08 AC 77 ms
75,996 KB
testcase_09 AC 68 ms
72,696 KB
testcase_10 AC 69 ms
71,988 KB
testcase_11 AC 53 ms
64,576 KB
testcase_12 AC 83 ms
76,096 KB
testcase_13 AC 413 ms
77,796 KB
testcase_14 AC 200 ms
77,048 KB
testcase_15 AC 215 ms
77,292 KB
testcase_16 AC 551 ms
77,044 KB
testcase_17 AC 473 ms
77,268 KB
testcase_18 AC 626 ms
77,660 KB
testcase_19 AC 551 ms
77,952 KB
testcase_20 AC 190 ms
77,232 KB
testcase_21 AC 559 ms
77,696 KB
testcase_22 AC 314 ms
77,024 KB
testcase_23 AC 236 ms
77,776 KB
testcase_24 AC 238 ms
77,364 KB
testcase_25 AC 763 ms
77,584 KB
testcase_26 AC 236 ms
77,396 KB
testcase_27 AC 491 ms
77,700 KB
testcase_28 AC 235 ms
77,728 KB
testcase_29 AC 236 ms
77,396 KB
testcase_30 AC 236 ms
77,452 KB
testcase_31 AC 237 ms
77,804 KB
testcase_32 AC 489 ms
77,704 KB
testcase_33 AC 40 ms
59,220 KB
testcase_34 AC 537 ms
77,580 KB
testcase_35 AC 540 ms
77,604 KB
testcase_36 AC 792 ms
77,556 KB
testcase_37 AC 563 ms
77,580 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(1):
  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