結果
| 問題 | 
                            No.28 末尾最適化
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2016-03-24 01:33:08 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 114 ms / 5,000 ms | 
| コード長 | 1,599 bytes | 
| コンパイル時間 | 598 ms | 
| コンパイル使用メモリ | 63,336 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-01 21:42:29 | 
| 合計ジャッジ時間 | 1,072 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 2 | 
ソースコード
#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;
}