結果

問題 No.931 Multiplicative Convolution
ユーザー kuhakukuhaku
提出日時 2020-08-21 01:01:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,241 bytes
コンパイル時間 2,505 ms
コンパイル使用メモリ 221,112 KB
実行使用メモリ 10,104 KB
最終ジャッジ日時 2023-08-03 21:01:02
合計ジャッジ時間 8,638 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 23 ms
4,380 KB
testcase_02 AC 24 ms
4,376 KB
testcase_03 AC 23 ms
4,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 306 ms
9,856 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 356 ms
10,064 KB
testcase_15 AC 383 ms
10,020 KB
testcase_16 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
}

vector<ll> pn;

bool is_prime(ll n){
	if(n == 1) return false;
	for(ll i : pn){
		if(i * i > n) return true;
		if(n % i == 0) return false;
	}
	return true;
}

void init(ll n = 2e5){
	if(!pn.empty() && pn.back() >= n) return;
	pn.push_back(2);
	for(int i = 3; i < n + 100; ++i) if(is_prime(i)) pn.push_back(i);
}

vector<P> prime_factorization(ll n){
	init();
	vector<P> res;

	for(ll i : pn){
		ll cnt = 0;
		while(n % i == 0){
			n /= i;
			cnt++;
		}
		if(cnt) res.push_back({i, cnt});
		if(n < i * i) break;
	}
	if(n != 1) res.push_back({n, 1});

	return res;
}

Vec divisor_enumeration(ll n){
	auto v = prime_factorization(n);

	Vec res, a, b, cp;
	res.push_back(1);
	for(auto p : v){
		cp.resize(res.size());
		copy(res.begin(), res.end(), cp.begin());
		a.resize(res.size());
		for(int k = 1; k <= p.second; ++k){
			ll t = pow(p.first, k);
			for(int i = 0; i < a.size(); ++i) a[i] = cp[i] * t;
			merge(res.begin(), res.end(), a.begin(), a.end(), back_inserter(b));
			swap(res, b);
			b.clear();
		}
	}

	return res;
}

ll find_root(ll p){
	if(p == 3) return 2;
	auto v = divisor_enumeration(p - 1);

	for(ll i : v){
		unordered_set<ll> used;
		ll t = 1;
		bool flg = true;
		for(ll j = 0; j < p - 2; ++j){
			(t *= i) % p;
			if(used.find(t) != used.end()) {
				flg = false;
				break;
			}
			used.insert(t);
		}
		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