結果
問題 | No.2589 Prepare Integers |
ユーザー | 👑 p-adic |
提出日時 | 2023-12-17 23:52:10 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 276 ms / 2,000 ms |
コード長 | 2,471 bytes |
コンパイル時間 | 303 ms |
コンパイル使用メモリ | 82,816 KB |
実行使用メモリ | 80,156 KB |
最終ジャッジ日時 | 2024-09-27 08:11:48 |
合計ジャッジ時間 | 6,701 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
52,992 KB |
testcase_01 | AC | 41 ms
52,992 KB |
testcase_02 | AC | 37 ms
52,736 KB |
testcase_03 | AC | 34 ms
53,120 KB |
testcase_04 | AC | 256 ms
78,400 KB |
testcase_05 | AC | 259 ms
78,204 KB |
testcase_06 | AC | 232 ms
78,712 KB |
testcase_07 | AC | 245 ms
78,876 KB |
testcase_08 | AC | 236 ms
79,904 KB |
testcase_09 | AC | 225 ms
78,304 KB |
testcase_10 | AC | 217 ms
79,464 KB |
testcase_11 | AC | 203 ms
77,568 KB |
testcase_12 | AC | 255 ms
78,528 KB |
testcase_13 | AC | 260 ms
78,844 KB |
testcase_14 | AC | 233 ms
78,492 KB |
testcase_15 | AC | 241 ms
79,188 KB |
testcase_16 | AC | 276 ms
79,388 KB |
testcase_17 | AC | 219 ms
78,468 KB |
testcase_18 | AC | 226 ms
80,156 KB |
testcase_19 | AC | 233 ms
78,364 KB |
testcase_20 | AC | 215 ms
79,020 KB |
testcase_21 | AC | 216 ms
78,608 KB |
testcase_22 | AC | 213 ms
78,016 KB |
testcase_23 | AC | 218 ms
78,044 KB |
testcase_24 | AC | 233 ms
78,300 KB |
testcase_25 | AC | 206 ms
78,152 KB |
testcase_26 | AC | 231 ms
78,836 KB |
testcase_27 | AC | 177 ms
78,120 KB |
ソースコード
R=range x=0 def Expand( X , digit ): global x answer=[] for d in R(digit): if x==0:break if X[d]!=0: answer+=[x%X[d]] x//=X[d] else:answer+=[0] return answer u0=u1=v0=v1=0 def PartitionOfUnity( b_0 , b_1 , K ): global u0 , u1 , v0 , v1 a=[[1,0],[0,1]] b=[b_0,b_1] i_0=b_0<b_1 i_1=1-i_0 while b[i_1]!=0: q=b[i_0]//b[i_1] a[i_0][i_0]=(a[i_0][i_0]-q*a[i_1][i_0])%K a[i_0][i_1]=(a[i_0][i_1]-q*a[i_1][i_1])%K b[i_0]-=q*b[i_1] i_0,i_1=i_1,i_0 u0,u1,v0,v1=a[i_0][0],a[i_0][1],a[i_1][0],a[i_1][1] return b[i_0] def SetGcd( gcd , gcd_inv , exp , K ): L=len(exp)-1 if gcd_inv[L]==0: gcd[L][L]=PartitionOfUnity(K,exp[L],K) gcd_inv[L]=K//gcd[L][L] for d in R(L): gcd[L][d] , exp[d] = exp[d]*u1%K , exp[d]*gcd_inv[L]%K else: gcd[L][L]=PartitionOfUnity(gcd[L][L],exp[L],K) gcd_inv[L]=K//gcd[L][L] for d in R(L): gcd[L][d] , exp[d] = ( gcd[L][d] * u0 + exp[d] * u1 ) % K , ( gcd[L][d] * v0 + exp[d] * v1 ) % K exp.pop() while len(exp) and exp[-1] == 0:exp.pop() if len(exp):SetGcd(gcd,gcd_inv,exp,K) O=print J=lambda:map(int,input().split()) K,Q=J() digit = 0 K_vec=[] power = K*10**18 while power > 0: power //= K digit+=1 K_vec+=[K] gcd=[[0]*(d+1)for d in R(digit)] gcd_inv=[0]*digit for q in R(Q): type,x=J() if type==1: exp=Expand(K_vec,digit) SetGcd( gcd , gcd_inv , exp , K ) elif type == 2: if x == 1: O(0) continue x-=1 exp = Expand( gcd_inv , digit ) if x > 0: O( -1 ) continue L=len(exp)-1 coef=[0]*( L + 1 ) for l in R(L,-1,-1): if gcd[l][l] != 0: exp[l] = (exp[l] - coef[l] // gcd[l][l])%K for d in R(l+1): coef[d] = ( coef[d] + exp[l] * gcd[l][d] ) % K answer = 0 power = 1 for d in R(L+1): answer += coef[d] * power power *= K O( answer ) elif type == 3: if x == 0: O( 1 ) continue exp = Expand( K_vec , digit ) L = len(exp) - 1 coef=[0]*( L + 1 ) lb=[0]*( L + 1 ) for l in R(L,-1,-1): if gcd[l][l] == 0: r = exp[l] - lb[l] if r < 0:break if r > 0 or l==0: coef[l]=1 break else: r = exp[l] - lb[l] % gcd[l][l] if r < 0:break coef[l] = r // gcd[l][l] r %=gcd[l][l] if r > 0: coef[l]+=1 break elif l==0: coef[l]+=1 break diff = ( coef[l] - lb[l] // gcd[l][l] ) % K for d in R(l+1):lb[d] = ( lb[d] + diff * gcd[l][d] ) % K answer = 0 prod = 1 for d in R(L+1): answer += coef[d] * prod if gcd_inv[d] != 0:prod *= gcd_inv[d]; O( answer )