結果

問題 No.931 Multiplicative Convolution
ユーザー pockynypockyny
提出日時 2019-10-12 06:05:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,561 bytes
コンパイル時間 1,007 ms
コンパイル使用メモリ 110,352 KB
実行使用メモリ 11,864 KB
最終ジャッジ日時 2024-04-19 10:20:34
合計ジャッジ時間 4,669 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
7,296 KB
testcase_01 AC 59 ms
7,296 KB
testcase_02 AC 61 ms
9,448 KB
testcase_03 AC 59 ms
9,328 KB
testcase_04 AC 59 ms
9,452 KB
testcase_05 AC 59 ms
7,424 KB
testcase_06 AC 60 ms
9,452 KB
testcase_07 AC 64 ms
7,936 KB
testcase_08 RE -
testcase_09 WA -
testcase_10 RE -
testcase_11 WA -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
template<class T> T extgcd(T a, T b, T& x, T& y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; }
template<class T> T mod_inv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; }
ll pw(ll a, ll b){
    ll x = 1;
    while (b){
        while(!(b&1)){
            (a *= a) %= mod; b /= 2;
        }
        (x *= a) %= mod; b--;
    }
    return x;
}

void _ntt(vector <ll> &a,int sign) {
    int i,j,k,n = a.size(),m = 1;
    while(m<n || m==1) m <<= 1;
    a.resize(m); n = m;
    const int g = 3;
    ll h = pw(g,(mod - 1)/n);
    if(sign==-1) h = (int)mod_inv(h,mod);
    i = 0;
    for(j=1;j<n - 1;j++) {
        for(k=(n >> 1); k>(i ^= k); k >>= 1);
        if(j<i) swap(a[i],a[j]);
    }
    for(i=1;i<n;i*= 2){
        const int i2 = 2*i;
        const ll base = pw(h,n/i2);
        ll w = 1;
        for(j=0;j<i;j++){
            for(k=j;k<n;k += i2){
                ll u = a[k],d = a[k + i]*w%mod;
                a[k] = u + d;
                if(a[k]>=mod) a[k] -= mod;
                a[k + i] = u - d;
                if(a[k + i]<0) a[k + i] += mod;
            }
            (w *= base) %= mod;
        }
    }
    for(ll x: a){
        if(x<0) x += mod;
    }
}

void ntt(vector<ll>& input) {
	_ntt(input, 1);
}
void intt(vector<ll>& input) {
	_ntt(input, -1);
    ll x = input.size();
	const int n_inv = mod_inv(x, mod);
	for (auto& x : input) (x *= n_inv) %= mod;
}

ll a[140000] = {},b[140000] = {},ans[100010],c[100010];
int p,gen = -1;
bool gensi(ll g){
	int i;
	ll ret = 1;
	for(i=1;i<p - 1;i++){
		(ret *= g) %= p;
		if(ret==1) return false;
	}
	return true;
}

vector<ll> u(262144),v(262144);
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int i;
	cin >> p;
	for(i=1;i<p;i++) cin >> a[i];
	for(i=1;i<p;i++) cin >> b[i];
	vector<ll> w;
	for(i=1;i<p;i++) w.push_back(i);
	random_device seed_gen;
  	mt19937 engine(seed_gen());
	shuffle(w.begin(),w.end(),engine);
	for(i=0;i<p - 1;i++){
		if(gensi(w[i])){
			gen = w[i];
			break;
		}
	}
	ll j = 1;
	for(i=0;i<p - 1;i++){
		u[i] = a[j];
		v[i] = b[j];
		(j *= gen) %= p;
	}
	j = 1;
	for(i=0;i<2*p;i++){
		c[i] = j;
		(j *= gen) %= p;
	}
	ntt(u); ntt(v);
	for(i=0;i<262144;i++){
		(u[i] *= v[i]) %= mod;
	}
	intt(u);
	for(i=0;i<2*p;i++){
		(ans[c[i]] += u[i]) %= mod;
	}
	for(i=1;i<=p - 1;i++){
		cout << ans[i] << " ";
	}
	cout << endl;
}
0