結果

問題 No.2613 Sum of Combination
ユーザー 蜜蜂蜜蜂
提出日時 2021-03-05 17:59:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,312 bytes
コンパイル時間 4,956 ms
コンパイル使用メモリ 240,740 KB
実行使用メモリ 30,032 KB
最終ジャッジ日時 2024-01-19 20:50:14
合計ジャッジ時間 12,350 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 29 ms
15,524 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 28 ms
15,524 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 30 ms
15,524 KB
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#include <atcoder/all>
using namespace atcoder;

using ll = long long;
using ld = long double;
#define fi first
#define se second
#define pb push_back

const int MAX=510000;
ll MOD;

ll fac[MAX],finv[MAX],inv[MAX];

void COMinit(){
  fac[0]=fac[1]=1;
  finv[0]=finv[1]=1;
  inv[1]=1;
  for(int i=2;i<MAX;i++){
    fac[i]=fac[i-1]*i%MOD;
    inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
    finv[i]=finv[i-1]*inv[i]%MOD;
  }
}

ll COM(int n,int k){
  if(n<k) return 0;
  if(n<0||k<0) return 0;
  return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}

constexpr ll mod = 998244353;

random_device seed;
mt19937_64 randint(seed());

ll ggr(ll mi,ll ma) { // [mi, ma)
    return mi + randint() % (ma - mi);
}

int main(){
  ll n;
  int p;
  cin>>n>>p;

  int copyp=p;
  MOD=p;
  COMinit();

  vector<int> v;
  while(n>0){
    v.pb(n%p);
    n/=p;
  }

  if(p==2){
    ll ans=1;
    for(int i:v){
      if(i==1){
        ans*=2;
        ans%=mod;
      }
    }
    cout<<ans<<endl;
    return 0;
  }
  
  vector<int> divisor;
  p--;
  for(int i=2;i*i<=p;i++){
    if(p%i==0){
      divisor.pb(i);
      while(p%i==0){
        p/=i;
      }
    }
  }
  if(p>1){
    divisor.pb(p);
  }

  p=copyp;
  int root;

  while(true){
    int check=ggr(2,p);
    int flag=1;
    for(int i:divisor){
      if(pow_mod(check,(p-1)/i,p)==1){
        flag=0;
        break;
      }
    }
    if(flag==1){
      root=check;
      break;
    }
    if(p==2){
      root=1;
      break;
    }
  }

  cout<<root<<endl;
  
  vector<ll> powmod(p),invpowmod(p);
  //powmod[i] := root^i (mod p)
  //invpowmod[i] := root^(invpowmod[i]) = i (mod p)

  powmod[0]=1;
  for(int i=1;i<p;i++){
    powmod[i]=powmod[i-1]*root;
    powmod[i]%=p;
    invpowmod[powmod[i]]=i;
  }
  invpowmod[0]=-1;
  invpowmod[1]=0;
  
  vector<ll> a(p,0);
  a[0]=1;

  cout<<v.size()<<endl;

  for(int now:v){
    cout<<now<<endl;

    vector<ll> b(p,0);
    for(int i=0;i<=now;i++){
      int next=COM(now,i);
      if(next==0){
        continue;
      }
      b[invpowmod[next]]++;
    }
    
    vector<ll> c=convolution(a,b);
    for(int i=0;i<p;i++){
      a[i]=0;
    }
    for(int i=0;i<2*p-1;i++){
      a[i%(p-1)]+=c[i];
    }
  }

  ll ans=0;
  for(int i=0;i<p;i++){
    ans+=powmod[i]*a[i];
    ans%=mod;
  }
  cout<<ans<<endl;
}
0