結果
問題 | No.1383 Numbers of Product |
ユーザー |
👑 ![]() |
提出日時 | 2021-02-07 22:58:10 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 793 ms / 2,000 ms |
コード長 | 4,681 bytes |
コンパイル時間 | 393 ms |
コンパイル使用メモリ | 82,256 KB |
実行使用メモリ | 168,472 KB |
最終ジャッジ日時 | 2024-11-22 23:23:15 |
合計ジャッジ時間 | 20,757 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 51 |
ソースコード
#Miller-Rabinの素数判定法def Miller_Rabin_Primality_Test(N,Times=20):"""Miller-Rabinによる整数Nの素数判定を行う.N:整数※:Trueは正確にはProbably Trueである(Falseは確定False)."""from random import randint as riif N==2:return Trueif N==1 or N%2==0:return Falseq=N-1k=0while q&1==0:k+=1q>>=1for _ in range(Times):m=ri(2,N-1)y=pow(m,q,N)if y==1:continueflag=Truefor i in range(k):if (y+1)%N==0:flag=Falsebreaky*=yy%=Nif flag:return Falsereturn True#ポラード・ローアルゴリズムによって素因数を発見する#参考元:https://judge.yosupo.jp/submission/6131def Find_Factor_Rho(N):if N==1:return 1from math import gcdm=1<<(N.bit_length()//8+1)for c in range(1,99):f=lambda x:(x*x+c)%Ny,r,q,g=2,1,1,1while g==1:x=yfor i in range(r):y=f(y)k=0while k<r and g==1:for i in range(min(m, r - k)):y=f(y)q=q*abs(x - y)%Ng=gcd(q,N)k+=mr <<=1if g<N:if Miller_Rabin_Primality_Test(g):return gelif Miller_Rabin_Primality_Test(N//g):return N//greturn N#ポラード・ローアルゴリズムによる素因数分解#参考元:https://judge.yosupo.jp/submission/6131def Pollard_Rho_Prime_Factorization(N):I=2res=[]while I*I<=N:if N%I==0:k=0while N%I==0:k+=1N//=Ires.append([I,k])I+=1+(I%2)if I!=101 or N<2**20:continuewhile N>1:if Miller_Rabin_Primality_Test(N):res.append([N,1])N=1else:j=Find_Factor_Rho(N)k=0while N%j==0:N//=jk+=1res.append([j,k])if N>1:res.append([N,1])res.sort(key=lambda x:x[0])return res#================================================def General_Binary_Decrease_Search(L,R,cond,Integer=True,ep=1/(1<<20),Times=50):"""条件式が単調減少であるとき,一般的な二部探索を行う.L:解の下限R:解の上限cond:条件(1変数関数,広義単調減少 or 広義単調減少を満たす)Integer:解を整数に制限するか?ep:Integer=Falseのとき,解の許容する誤差"""if not(cond(L)):return Noneif cond(R):return Rif Integer:L-=1while R-L>1:C=L+(R-L)//2if cond(C):L=Celse:R=Creturn Lelse:while (R-L)>=ep and Times:Times-=1C=L+(R-L)/2if cond(C):L=Celse:R=Creturn Ldef Floor_Root(a,k):"""floor(a^(1/k)) を求める.a:非負整数k:正の整数"""assert 0<=a and 0<kif a==0:return 0if k==1:return a#大体の値を求める.x=int(pow(a,1/k))#増やすwhile pow(x+1,k)<=a:x+=1#減らすwhile pow(x,k)>a:x-=1return x#================================================def f(x):D=K*K+4*xR=Floor_Root(D,2)if R**2!=D:return Noneb=(-K+R)if b%2==1:return Noneelse:return b//2#================================================from collections import defaultdictN,K,M=map(int,input().split())#B=1の解の範囲を求める.alpha=General_Binary_Decrease_Search(0,N,lambda x:x*(x+K)<=N)#B>=2の解を求める.F=defaultdict(int)a=1while a*(a+K)*(a+2*K)<=N:p=a*(a+K)*(a+2*K)F[p]+=1q=a+3*Kwhile p*q<=N:p*=qF[p]+=1q+=Ka+=1if M>=2:Ans=0for n in F:b=0t=f(n)if t!=None and 1<=t<=alpha:b=1if F[n]+b==M:Ans+=1else:Ans=0beta=alphafor n in F:if F[n]==1:t=f(n)if t==None or not(1<=t<=alpha):Ans+=1else:beta-=1else:t=f(n)if t!=None and 1<=t<=alpha:beta-=1Ans+=betaprint(Ans)