結果

問題 No.1728 [Cherry 3rd Tune] Bullet
ユーザー umezo
提出日時 2021-10-30 00:09:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,072 bytes
コンパイル時間 2,686 ms
コンパイル使用メモリ 211,036 KB
最終ジャッジ日時 2025-01-25 09:45:04
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 TLE * 9 MLE * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

const int MOD=1e9+7;

ll modpow(ll x,ll n){
  ll ans=1;
  while(n){
    if(n&1) ans=ans*x%MOD;
    x=x*x%MOD;
    n/=2;
  }
  return ans;
}

vector<ll> divisor(ll x){
  set<ll> s;
  for(ll i=1;i*i<=x;i++){
    if(x%i==0){
      s.insert(i);
      s.insert(x/i);
    }
  }
  vector<ll> res;
  for(auto y:s) res.push_back(y);
  sort(ALL(res));
  reverse(ALL(res));
  return res;
}
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int t;
  cin>>t;
  while(t--){
    ll n,c;
    cin>>n>>c;
    
    ll ans=modpow(c,n)*n%MOD;
    
    vector<ll> A=divisor(n);
    map<ll,ll> m;
    ll cnt=0;
    for(auto t:A){
      if(t==1){
        ans=(ans+(n-cnt)*modpow(c,2)%MOD)%MOD;
        break;
      }
      ll tmp=n/t;
      for(int i=2*t;i<=n;i+=t) tmp-=m[i];
      ans=(ans+tmp*modpow(c,2*t)%MOD)%MOD;
      m[t]=tmp;
      cnt+=tmp;
    }
    cout<<ans*modpow(2*n,MOD-2)%MOD<<endl;
  }
    
  return 0;
}
0