結果

問題 No.28 末尾最適化
ユーザー kyunakyuna
提出日時 2019-08-06 08:33:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 333 ms / 5,000 ms
コード長 1,189 bytes
コンパイル時間 914 ms
コンパイル使用メモリ 87,632 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-25 17:37:13
合計ジャッジ時間 1,757 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
const int MOD = (int)1e8 + 9;
const int INF = (int)1e9 + 7;
using ll = long long;

map<long long, int> prime_factorize(long long n) {
    map<long long, int> res;
    for (long long p = 2; p * p <= n; ++p) {
        while (n % p == 0) { ++res[p]; n /= p; }
    }
    if (n != 1) res[n] = 1;
    return res;
}

int main() {
    int Q; cin >> Q;
    while (Q--) {
        ll seed, N, K, B; cin >> seed >> N >> K >> B;
        vector<ll> X(N + 1, 0);
        X[0] = seed;
        for (int i = 1; i <= N; i++) {
            X[i] = 1LL + (X[i - 1] * X[i - 1] + X[i - 1] * 12345) % MOD;
        }
        int mi = INF;
        for (auto &p: prime_factorize(B)) {
            vector<int> cnt(N + 1);
            for (int i = 0; i <= N; i++) {
                int c = 0;
                while (X[i] % p.first == 0) X[i] /= p.first, c++;
                cnt[i] = c;
            }
            sort(cnt.begin(), cnt.end());
            int sum = 0;
            for (int i = 0; i < K; i++) sum += cnt[i];
            mi = min(mi, sum / p.second);
        }
        cout << mi << endl;
    }
    return 0;
}
0