結果
| 問題 | No.28 末尾最適化 |
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2018-11-16 11:48:03 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 230 ms / 5,000 ms |
| コード長 | 1,208 bytes |
| 記録 | |
| コンパイル時間 | 2,297 ms |
| コンパイル使用メモリ | 201,492 KB |
| 最終ジャッジ日時 | 2025-01-06 16:44:31 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 2 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T>
map<T, Int> factorize(T x){
map<T, Int> res;
for(Int i=2;i*i<=x;i++){
while(x%i==0){
x/=i;
res[i]++;
}
}
if(x!=1) res[x]++;
return res;
}
//INSERT ABOVE HERE
const Int MOD = 100000009;
signed main(){
Int T;
cin>>T;
while(T--){
Int s,n,k,b;
cin>>s>>n>>k>>b;
vector<Int> x(n+1,s);
for(Int i=1;i<=n;i++) x[i]=1+(x[i-1]*x[i-1]+x[i-1]*12345)%MOD;
for(Int i=0;i<=n;i++){
if(x[i]==0){
cout<<0<<endl;
continue;
}
}
Int ans=1e9;
auto m=factorize(b);
for(auto p:m){
vector<Int> cnt(40);
for(Int i=0;i<=n;i++){
Int k=x[i],c=0;
while(k%p.first==0){
k/=p.first;
c++;
}
cnt[c]++;
}
Int res=0,num=0;
for(Int i=0;i<(Int)cnt.size();i++){
Int d=min(k-num,cnt[i]);
num+=d;
res+=d*i;
}
chmin(ans,res/p.second);
}
cout<<ans<<endl;
}
return 0;
}
beet