結果

問題 No.28 末尾最適化
ユーザー rpy3cpprpy3cpp
提出日時 2016-03-23 02:53:16
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,586 bytes
コンパイル時間 607 ms
コンパイル使用メモリ 63,988 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 05:10:49
合計ジャッジ時間 1,274 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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()
{
    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 << endl;
    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 - 1;
}
0