結果

問題 No.28 末尾最適化
ユーザー rpy3cpprpy3cpp
提出日時 2016-03-24 01:33:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 114 ms / 5,000 ms
コード長 1,599 bytes
コンパイル時間 518 ms
コンパイル使用メモリ 62,360 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-09 21:15:32
合計ジャッジ時間 1,086 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <ios>
#include <cstring>
#include <algorithm>

using namespace std;

const long long MOD = 100000009;

long long xs[10005];

int find_max(int seed, int N, int K, int B);

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int Q = 0;
    cin >> Q;
    vector<int> result(Q, 0);
    int seed, N, K, B;
    for (int i = 0; i != Q; ++i){
        cin >> seed >> N >> K >> B;
        result[i] = find_max(seed, N, K, B);
    }
    for (auto res : result) cout << res << '\n';
    return 0;
}


int find_max(int seed, int N, int K, int B){
    xs[0] = seed;
    for (int i = 0; i != N; ++i){
        xs[i + 1] = 1 + (xs[i] + 12345) * xs[i] % MOD;
    }
    int ps[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
    int counts[40] = {0};
    int record = 100000000;
    for (auto p : ps){
        int c = 0;
        while (B % p == 0){
            ++c;
            B /= p;
        }
        if (c){
            memset(counts, 0, 40);
            for (int i = 0; i <= N; ++i){
                int x = xs[i];
                int f = 0;
                for (; x % p == 0; ++f) x /= p;
                counts[f] += 1;
            }
            int Kcopy = K;
            int cum = 0;
            for (int i = 0; Kcopy; ++i){
                Kcopy -= counts[i];
                if (Kcopy < 0){
                    cum += (counts[i] + Kcopy) * i;
                    break;
                }else{
                    cum += counts[i] * i;
                }
            }
            record = min(record, cum / c);
        }
    }
    return record;
}

0