結果

問題 No.931 Multiplicative Convolution
ユーザー tempura_pptempura_pp
提出日時 2019-11-22 21:37:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 4,082 bytes
コンパイル時間 1,432 ms
コンパイル使用メモリ 107,144 KB
実行使用メモリ 9,908 KB
最終ジャッジ日時 2023-08-01 11:11:05
合計ジャッジ時間 4,985 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 24 ms
4,376 KB
testcase_08 AC 244 ms
9,852 KB
testcase_09 AC 187 ms
9,764 KB
testcase_10 AC 226 ms
9,548 KB
testcase_11 AC 182 ms
9,504 KB
testcase_12 AC 165 ms
8,844 KB
testcase_13 AC 236 ms
9,756 KB
testcase_14 AC 246 ms
9,772 KB
testcase_15 AC 243 ms
9,908 KB
testcase_16 AC 237 ms
9,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=998244353 ;

template< int mod >
struct NumberTheoreticTransform {

  vector< int > rev, rts;
  int base, max_base, root;

  NumberTheoreticTransform() : base(1), rev{0, 1}, rts{0, 1} {
    assert(mod >= 3 && mod % 2 == 1);
    auto tmp = mod - 1;
    max_base = 0;
    while(tmp % 2 == 0) tmp >>= 1, max_base++;
    root = 2;
    while(mod_pow(root, (mod - 1) >> 1) == 1) ++root;
    assert(mod_pow(root, mod - 1) == 1);
    root = mod_pow(root, (mod - 1) >> max_base);
  }

  inline int mod_pow(int x, int n) {
    int ret = 1;
    while(n > 0) {
      if(n & 1) ret = mul(ret, x);
      x = mul(x, x);
      n >>= 1;
    }
    return ret;
  }

  inline int inverse(int x) {
    return mod_pow(x, mod - 2);
  }

  inline unsigned add(unsigned x, unsigned y) {
    x += y;
    if(x >= mod) x -= mod;
    return x;
  }

  inline unsigned mul(unsigned a, unsigned b) {
    return 1ull * a * b % (unsigned long long) mod;
  }

  void ensure_base(int nbase) {
    if(nbase <= base) return;
    rev.resize(1 << nbase);
    rts.resize(1 << nbase);
    for(int i = 0; i < (1 << nbase); i++) {
      rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
    }
    assert(nbase <= max_base);
    while(base < nbase) {
      int z = mod_pow(root, 1 << (max_base - 1 - base));
      for(int i = 1 << (base - 1); i < (1 << base); i++) {
        rts[i << 1] = rts[i];
        rts[(i << 1) + 1] = mul(rts[i], z);
      }
      ++base;
    }
  }


  void ntt(vector< int > &a) {
    const int n = (int) a.size();
    assert((n & (n - 1)) == 0);
    int zeros = __builtin_ctz(n);
    ensure_base(zeros);
    int shift = base - zeros;
    for(int i = 0; i < n; i++) {
      if(i < (rev[i] >> shift)) {
        swap(a[i], a[rev[i] >> shift]);
      }
    }
    for(int k = 1; k < n; k <<= 1) {
      for(int i = 0; i < n; i += 2 * k) {
        for(int j = 0; j < k; j++) {
          int z = mul(a[i + j + k], rts[j + k]);
          a[i + j + k] = add(a[i + j], mod - z);
          a[i + j] = add(a[i + j], z);
        }
      }
    }
  }


  vector< int > multiply(vector< int > a, vector< int > b) {
    int need = a.size() + b.size() - 1;
    int nbase = 1;
    while((1 << nbase) < need) nbase++;
    ensure_base(nbase);
    int sz = 1 << nbase;
    a.resize(sz, 0);
    b.resize(sz, 0);
    ntt(a);
    ntt(b);
    int inv_sz = inverse(sz);
    for(int i = 0; i < sz; i++) {
      a[i] = mul(a[i], mul(b[i], inv_sz));
    }
    reverse(a.begin() + 1, a.end());
    ntt(a);
    a.resize(need);
    return a;
  }
};

ll powmod(ll n,ll k, ll p){
    ll ret=1;
    while(k){
        if(k&1)ret=ret*n%p;
        n=n*n%p;
        k>>=1;
    }
    return ret;
}

int main(){
    int p;
    cin>>p;
    if(p==2){
        ll x,y;
        cin>>x>>y;
        cout<<x*y%mod<<endl;
        return 0;
    }
    int r = 2;
    while(1){
        bool ok = true;
        for(int j=2;j*j<p;++j){
            if((p-1)%j==0){
                if(powmod(r,j,p)==1){
                    ok=false;break;
                }
                if(powmod(r,(p-1)/j,p)==1){
                    ok=false;
                    break;
                }
            }
        }
        if(ok)break;
        ++r;
    }
    vector<ll> a(p),b(p);
    rep(i,p-1)cin>>a[i+1];
    rep(i,p-1)cin>>b[i+1];
    vector<int> c(p), d(p);
    rep(i,p-1){
        c[i]=a[powmod(r,i,p)];
        d[i]=b[powmod(r,i,p)];
    }
    NumberTheoreticTransform<998244353> ntt;
    auto ret = ntt.multiply(c, d);
    vector<int> ans(p);
    rep(i,ret.size()){
        ans[powmod(r,i,p)]+=ret[i];
    }
    rep(i,p-1){
        cout<<ans[i+1]%mod<<" \n"[i+2==p];
    }
    return 0;
}
0