結果
問題 | No.2262 Fractions |
ユーザー |
![]() |
提出日時 | 2023-04-07 21:25:40 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,513 bytes |
コンパイル時間 | 689 ms |
コンパイル使用メモリ | 81,992 KB |
実行使用メモリ | 89,920 KB |
最終ジャッジ日時 | 2024-10-02 18:58:35 |
合計ジャッジ時間 | 23,593 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 44 TLE * 1 |
ソースコード
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=lowerto=upperelse:frm=upperto=lowerL=1R=2while judge_function((frm[0]+R*to[0],frm[1]+R*to[1]))==now_judge:L*=2R*=2if frm[0]+L*to[0]>max_val or frm[1]+L*to[1]>max_val:return towhile L+1<R:mid=(L+R)//2if 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=midelse:R=midif 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=1while i<=n:q=n//ij=n//q+1ret.append((i,j))i=jreturn retdef floor_sum(n,m,a,b):ans=0while True:if a>=m:ans+=(n-1)*n*(a//m)//2a%=mif b>=m:ans+=n*(b//m)b%=my_max=(a*n+b)//mx_max=(y_max*m-b)if y_max==0:return ansans+=(n-(x_max+a-1)//a)*y_maxn,m,a,b=y_max,a,m,(a-x_max%a)%adef mertens_table(n):if n==0:return [[0],[0]]b=10**4small=[1]*(n//b+1)large=[1]*(b+1)small[0]=0large[0]=0prime=[1]*(n//b+1)for p in range(2,n//b+1):if not prime[p]:continuefor j in range(p,n//b+1,p):if j>p:prime[j]=0if j%(p*p)==0:small[j]=0else:small[j]*=-1for i in range(1,n//b):small[i+1]+=small[i]for i in range(b,0,-1):l=2while l<=n//i:q=n//(i*l)r=n//(i*q)+1if i*l<=b:large[i]-=large[i*l]*(r-l)else:large[i]-=small[n//(i*l)]*(r-l)l=rreturn small,largedef range_g(L,R,P,Q):return floor_sum(R-L,Q,P,L*P)def count(n,P,Q):res=0for L,R in QR:x=n//Lif x<len(small):res+=small[x]*range_g(L,R,P,Q)else:res+=large[n//x]*range_g(L,R,P,Q)return resdef judge_function(res):P,Q=resreturn count(n,P,Q)>=kfor _ 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')continueif k<=c:rev=Falseelif k<=2*c+1:rev=Truek=2*c+2-kelse:print(-1)continueans=stern_brocot_tree_search(n)num,den=ansif rev:den,num=num,denprint(str(num)+'/'+str(den))