結果

問題 No.931 Multiplicative Convolution
ユーザー kuhaku
提出日時 2020-08-21 01:18:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 669 ms / 2,000 ms
コード長 4,104 bytes
コンパイル時間 2,526 ms
コンパイル使用メモリ 206,488 KB
最終ジャッジ日時 2025-01-13 04:34:08
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Vec = vector<ll>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define rep(i, n) REP(i, 0, n)
template <typename T>
bool chmax(T &a, const T b){if(a < b){a = b; return true;} return false;}
template <typename T>
bool chmin(T &a, const T b){if(a > b){a = b; return true;} return false;}
constexpr ll LINF = 1e18L+1;
constexpr ll MOD = 998244353;
const double PI = acos(-1.0);

template <int mod>
struct ModInt {
	int x;

	ModInt() : x(0) {}

	ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

	ModInt &operator+=(const ModInt &p) {
		if((x += p.x) >= mod) x -= mod;
		return *this;
	}

	ModInt &operator-=(const ModInt &p) {
		if((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}

	ModInt &operator*=(const ModInt &p) {
		x = (int) (1LL * x * p.x % mod);
		return *this;
	}

	ModInt &operator/=(const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}

	ModInt &operator++() {
		if((++x) >= mod) x -= mod;
		return *this;
	}

	ModInt operator++(int) {
		ModInt tmp(*this);
		operator++();
		return tmp;
	}

	ModInt &operator--() {
		if((x += mod - 1) >= mod) x -= mod;
		return *this;
	}

	ModInt operator--(int) {
		ModInt tmp(*this);
		operator--();
		return tmp;
	}

	ModInt operator-() const { return ModInt(-x); }

	ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

	ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

	ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

	ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

	bool operator==(const ModInt &p) const { return x == p.x; }

	bool operator!=(const ModInt &p) const { return x != p.x; }

	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while(b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}

	ModInt pow(int64_t n) const {
		ModInt res(1), mul(x);
		while(n > 0) {
			if(n & 1) res *= mul;
			mul *= mul;
			n >>= 1;
		}
		return res;
	}

	friend ostream &operator<<(ostream &os, const ModInt &p) {
		return os << p.x;
	}

	friend istream &operator>>(istream &is, ModInt &a) {
		int64_t t;
		is >> t;
		a = ModInt< mod >(t);
		return (is);
	}

	static int get_mod() { return mod; }
};
using Mint = ModInt<MOD>;

void ntt(vector<Mint> &a, bool inv){
	int N = a.size();
	int n = 2, d = N / 2;
	vector<Mint> cp(N);
	while (n <= N) {
		for (int p = 0; p < d; ++p) {
			Mint omega = Mint(3).pow((MOD - 1) / n);
			if (inv) omega = omega.inverse();
			Mint pow_omega = 1;
			for (int i = 0; i < n / 2; ++i) {
				cp[p + d * i] = a[p + 2 * d * i] + pow_omega * a[p + 2 * d * i + d];
				cp[p + d * i + N / 2] = a[p + 2 * d * i] - pow_omega * a[p + 2 * d * i + d];
				pow_omega *= omega;
			}
		}

		for (int i = 0; i < N; ++i) a[i] = cp[i];
		n <<= 1, d >>= 1;
	}
}
void conv(vector<Mint> &a, vector<Mint> b){
	int n = a.size() + b.size();
	int N = 1;
	while (N <= n) N <<= 1;

	a.resize(N);
	b.resize(N);

	ntt(a, false);
	ntt(b, false);

	for (int i = 0; i < N; ++i) {
		a[i] *= b[i] / N;
	}

	ntt(a, true);
}

ll find_root(ll p){
	for(ll i = 2; i < p; ++i){
		unordered_set<ll> used;
		ll t = 1;
		bool flg = true;
		for(ll j = 0; j < p - 2; ++j){
			used.insert(t);
			(t *= i) %= p;
			if(used.find(t) != used.end()) {
				flg = false;
				break;
			}
		}
		if(flg) return i;
	}
	return 0;
}

int main(void) {
	ll p;
	cin >> p;
	Vec a(p), b(p);
	rep(i, p - 1) cin >> a[i + 1];
	rep(i, p - 1) cin >> b[i + 1];

	ll r = find_root(p);
	cerr << r << endl;
	ll t = 1;
	vector<Mint> u(p), v(p);
	rep(i, p - 1){
		v[i] = a[t];
		u[i] = b[t];
		(t *= r) %= p;
	}
	conv(v, u);

	t = 1;
	vector<Mint> ans(p);
	rep(i, v.size()){
		// cerr << v[i] << endl;
		ans[t] += v[i];
		(t *= r) %= p;
	}

	rep(i, p - 1) cout << ans[i + 1] << " ";
	cout << endl;

	return 0;
}
0