結果

問題 No.28 末尾最適化
ユーザー vwxyzvwxyz
提出日時 2021-07-17 19:40:27
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,350 ms / 5,000 ms
コード長 1,422 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 102,164 KB
最終ジャッジ日時 2023-09-21 12:55:51
合計ジャッジ時間 2,546 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 261 ms
94,388 KB
testcase_01 AC 1,350 ms
102,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
from collections import Counter, deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        item, heap[0] = heap[0], item
        heapq._siftup_max(heap, 0)
    return item
from math import gcd as GCD, modf
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines

Q=int(readline())
mod=10**8+9
primes=[2,3,5,7,11,13,17,19,23,29,31]
for _ in range(Q):
    X,N,K,B=map(int,readline().split())
    dct=defaultdict(int)
    for p in primes:
        if B%p==0:
            while B%p==0:
                B//=p
                dct[p]+=1
    dct_lst=defaultdict(list)
    XX=X
    for p in dct:
        cnt=0
        while XX%p==0:
            XX//=p
            cnt+=1
        dct_lst[p].append(cnt)
    for _ in range(N):
        X=1+(X**2+X*12345)%mod
        XX=X
        for p in dct:
            cnt=0
            while XX%p==0:
                XX//=p
                cnt+=1
            dct_lst[p].append(cnt)
    for p in dct_lst:
        dct_lst[p].sort()
    ans=min(sum(dct_lst[p][:K])//dct[p] for p in dct)
    print(ans)
0