結果

問題 No.2266 Fractions (hard)
ユーザー とりゐとりゐ
提出日時 2023-04-07 22:05:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,195 ms / 6,000 ms
コード長 2,517 bytes
コンパイル時間 1,855 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 78,028 KB
最終ジャッジ日時 2024-04-14 11:54:26
合計ジャッジ時間 33,001 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
76,264 KB
testcase_01 AC 1,847 ms
77,804 KB
testcase_02 AC 445 ms
77,800 KB
testcase_03 AC 47 ms
62,208 KB
testcase_04 AC 70 ms
69,344 KB
testcase_05 AC 86 ms
76,264 KB
testcase_06 AC 214 ms
76,760 KB
testcase_07 AC 131 ms
76,368 KB
testcase_08 AC 98 ms
76,300 KB
testcase_09 AC 78 ms
76,268 KB
testcase_10 AC 82 ms
76,156 KB
testcase_11 AC 53 ms
63,308 KB
testcase_12 AC 104 ms
76,096 KB
testcase_13 AC 1,747 ms
78,028 KB
testcase_14 AC 213 ms
77,244 KB
testcase_15 AC 230 ms
77,632 KB
testcase_16 AC 1,760 ms
77,656 KB
testcase_17 AC 1,310 ms
77,924 KB
testcase_18 AC 1,966 ms
77,376 KB
testcase_19 AC 1,836 ms
77,684 KB
testcase_20 AC 203 ms
77,352 KB
testcase_21 AC 2,035 ms
77,680 KB
testcase_22 AC 1,023 ms
77,212 KB
testcase_23 AC 251 ms
77,680 KB
testcase_24 AC 250 ms
77,664 KB
testcase_25 AC 2,589 ms
77,708 KB
testcase_26 AC 252 ms
77,572 KB
testcase_27 AC 1,771 ms
77,700 KB
testcase_28 AC 252 ms
77,668 KB
testcase_29 AC 250 ms
77,556 KB
testcase_30 AC 251 ms
77,956 KB
testcase_31 AC 249 ms
77,712 KB
testcase_32 AC 1,643 ms
77,952 KB
testcase_33 AC 42 ms
58,756 KB
testcase_34 AC 917 ms
77,924 KB
testcase_35 AC 921 ms
77,692 KB
testcase_36 AC 3,195 ms
77,956 KB
testcase_37 AC 2,461 ms
77,724 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,Q,P,0)-floor_sum(L,Q,P,0)

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