結果

問題 No.28 末尾最適化
ユーザー beetbeet
提出日時 2018-11-16 11:48:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 230 ms / 5,000 ms
コード長 1,208 bytes
コンパイル時間 2,697 ms
コンパイル使用メモリ 207,764 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 01:26:43
合計ジャッジ時間 2,952 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;
}
0