結果

問題 No.28 末尾最適化
ユーザー tnodinotnodino
提出日時 2022-02-13 14:19:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,241 ms / 5,000 ms
コード長 1,006 bytes
コンパイル時間 1,080 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 273,580 KB
最終ジャッジ日時 2023-09-11 15:29:35
合計ジャッジ時間 4,694 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
77,764 KB
testcase_01 AC 3,241 ms
273,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import sqrt
from collections import defaultdict
def PrimeFactorization1(N):
    ret = defaultdict(int)
    for i in range(2,int(sqrt(N))+1):
        while N % i == 0:
            ret[i] += 1
            N //= i
    if N > 1:
        ret[N] += 1
    return ret

def PrimeFactorization2(N, L):
    ret = defaultdict(int)
    for p in L:
        while N % p == 0:
            ret[p] += 1
            N //= p
    return ret

mod = 10**8+9
Q = int(input())
for _ in range(Q):
    seed,N,K,B = map(int,input().split())
    X = [seed]
    for i in range(N):
        X.append(((X[i] * X[i]) + (X[i] * 12345)) % mod + 1)
    BP = PrimeFactorization1(B)
    L = list(BP.keys())
    XP = []
    for i in range(N+1):
        XP.append(PrimeFactorization2(X[i], L))
    ans = 1<<64
    for p in L:
        P = []
        for i in range(N+1):
            P.append(XP[i][p])
        P.sort()
        cnt = 0
        for i in range(K):
            cnt += P[i]
        ans = min(ans, cnt // BP[p])
    print(ans)
0