結果

問題 No.28 末尾最適化
コンテスト
ユーザー yaoshimax
提出日時 2015-02-15 00:42:16
言語 PyPy2
(7.3.15)
結果
WA  
実行時間 -
コード長 938 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 152 ms
コンパイル使用メモリ 77,604 KB
最終ジャッジ日時 2025-12-03 13:52:44
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 1 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from heapq import heappush,heappop
Q=int(raw_input())
primes=[2,3,5,7,11,13,17,19,23,29]
for i in range(Q):
   seed,N,K,B=map(int,raw_input().split())
   ps=[]
   for p in primes:
      cnt=0
      while B%p==0:
         cnt+=1
         B/=p
      if cnt>0:
         ps.append((p,cnt))
   hp = [[] for j in range(len(ps))]
   for j in range(N+1):
      x=seed
      #print x,
      for k in range(len(ps)):
         p=ps[k][0]
         cnt=0
         while seed%p == 0:
            seed/=p
            cnt+=1
         #print cnt,
         if len(hp[k]) < K:
            heappush(hp[k],-cnt)
         elif hp[k][0]<-cnt:
            heappop(hp[k])
            heappush(hp[k],-cnt)
      #print
      seed=x*x
      seed%=100000009
      seed+=x*12345
      seed%=100000009 
      seed+=1
   ans = 5000
   for j in range(len(ps)):
      curAns = 0
      for s in hp[j]:
         curAns-=s
      ans = min(ans,curAns/ps[j][1])
   print ans

0