結果

問題 No.28 末尾最適化
ユーザー dangodango
提出日時 2023-07-09 15:46:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 911 ms / 5,000 ms
コード長 1,090 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 87,304 KB
実行使用メモリ 78,692 KB
最終ジャッジ日時 2023-09-30 19:06:28
合計ジャッジ時間 1,781 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
76,048 KB
testcase_01 AC 911 ms
78,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

INF = 2 ** 25
MOD = 100000009
def gen(seed, N):
    x = [0] * (N + 1)
    x[0] = seed
    for n in range(1, N + 1):
        pre = x[n - 1]
        x[n] = 1 + (pre * pre % MOD + pre * 12345) % MOD
    return x
def getFactors(n):
    res = []
    p = 2
    while p * p <= n:
        if n % p == 0:
            e = 0
            while n % p == 0:
                e += 1
                n //= p
            res.append((p, e))
        p += 1
    if n != 1:
        res.append((n, 1))
    return res
def solve(seed, N, K, B):
    x = gen(seed, N)
    factors = getFactors(B)
    res = INF
    for i in range(len(factors)):
        p, e = factors[i]
        y = x[:]
        for j in range(len(y)):
            num = y[j]
            cnt = 0
            while num % p == 0:
                cnt += 1
                num //= p
            y[j] = cnt
        y.sort()
        sum_ = sum(y[:K])
        res = min(res, sum_ // e)
    return res
if __name__ == '__main__':
    Q = int(input())
    for i in range(Q):
        seed, N, K, B = map(int, input().split())
        print(solve(seed, N, K, B))
0