結果

問題 No.28 末尾最適化
ユーザー GOTKAKO
提出日時 2025-05-16 18:59:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 251 ms / 5,000 ms
コード長 3,381 bytes
コンパイル時間 2,859 ms
コンパイル使用メモリ 209,020 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-16 18:59:40
合計ジャッジ時間 3,187 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long mod = 1e8+9;
//入力が必ず-mod<a<modの時.
struct mint{
    long long v = 0;
    mint(){} mint(int a){v = a<0?a+mod:a;} mint(long long a){v = a<0?a+mod:a;}
    mint(unsigned long long a){v = a;}
    long long val(){return v;}
    void modu(){v %= mod;}
 
    mint &operator=(const mint &b) = default;
    mint operator-()const{return mint(0)-(*this);}
    mint operator+(const mint b)const{return mint(v)+=b;}
    mint operator-(const mint b)const{return mint(v)-=b;}
    mint operator*(const mint b)const{return mint(v)*=b;}
    mint operator/(const mint b)const{return mint(v)/=b;}
 
    mint operator+=(const mint b){
        v += b.v; if(v >= mod) v -= mod;
        return *this;
    }
    mint operator-=(const mint b){
        v -= b.v; if(v < 0) v += mod; 
        return *this;
    }   
    mint operator*=(const mint b){v = v*b.v%mod; return *this;}
    mint operator/=(mint b){
        if(b == 0) assert(false);
        int left = mod-2;
        while(left){if(left&1) *this *= b; b *= b; left >>= 1;}
        return *this;
    }
 
    mint operator++(){*this += 1; return *this;}
    mint operator--(){*this -= 1; return *this;}
    mint operator++(int){*this += 1; return *this;}
    mint operator--(int){*this -= 1; return *this;}
    bool operator==(const mint b)const{return v == b.v;}
    bool operator!=(const mint b)const{return v != b.v;}
    bool operator>(const mint b)const{return v > b.v;}
    bool operator>=(const mint b)const{return v >= b.v;}
    bool operator<(const mint b)const{return v < b.v;}
    bool operator<=(const mint b)const{return v <= b.v;}
 
    mint pow(long long n)const{
        mint ret = 1,p = v;
        if(n < 0) p = p.inv(),n = -n;
        while(n){
            if(n&1) ret *= p;
            p *= p; n >>= 1;
        }
        return ret;
    }
    mint inv()const{return mint(1)/v;}
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int Need = 100;
    vector<vector<pair<int,int>>> Pf(Need+1);
    vector<bool> prime(Need+1,true);
    prime.at(0) = false; prime.at(1) = false;
    for(int i=2; i<=Need; i++){
        if(!prime.at(i)) continue;
        Pf.at(i) = {{i,1}};
        for(int k=i+i; k<=Need; k+=i){
            prime.at(k) = false;
            int f = 0,k2 = k;
            while(k2%i == 0) f++,k2 /= i;
            Pf.at(k).push_back({i,f});
        }
    }

    int Q; cin >> Q;
    while(Q--){
        mint v; cin >> v.v; v.v %= mod;
        int N,K,B; cin >> N >> K >> B;
        auto P = Pf.at(B);
        int n = P.size();
        vector<vector<int>> C(n,vector<int>(31));
        for(int i=0; i<=N; i++){
            if(v == 0) v.v = mod;
            int idx = 0;
            for(auto [p,e] : P){
                int es = 0,x = v.v;
                while(x%p == 0) es++,x /= p;
                C.at(idx).at(es)++;
                idx++;
            }
            v = v*v+v*12345+1;
        }
        {
            int answer = 1001001001,idx = 0;
            for(auto [p,e] : P){
                int left = K,now = 0;
                for(int i=0; i<=30; i++){
                    int ope = min(left,C.at(idx).at(i));
                    now += ope*i; left -= ope;
                }
                answer = min(answer,now/e);
                idx++;
            }
            cout << answer << "\n";
        }
    }
}
0