結果

問題 No.28 末尾最適化
ユーザー yaoshimaxyaoshimax
提出日時 2015-02-15 00:42:16
言語 Python2
(2.7.18)
結果
TLE  
実行時間 -
コード長 938 bytes
コンパイル時間 67 ms
コンパイル使用メモリ 6,524 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-06 01:11:40
合計ジャッジ時間 6,863 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
6,196 KB
testcase_01 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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