結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー harady_a_humanharady_a_human
提出日時 2020-05-25 19:18:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,281 ms / 3,500 ms
コード長 4,375 bytes
コンパイル時間 1,965 ms
コンパイル使用メモリ 179,980 KB
実行使用メモリ 29,564 KB
最終ジャッジ日時 2024-04-23 19:55:51
合計ジャッジ時間 26,094 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 26 ms
5,376 KB
testcase_04 AC 17 ms
5,376 KB
testcase_05 AC 18 ms
5,376 KB
testcase_06 AC 16 ms
5,376 KB
testcase_07 AC 15 ms
5,376 KB
testcase_08 AC 18 ms
5,376 KB
testcase_09 AC 20 ms
5,376 KB
testcase_10 AC 10 ms
5,376 KB
testcase_11 AC 15 ms
5,376 KB
testcase_12 AC 9 ms
5,376 KB
testcase_13 AC 1,278 ms
29,560 KB
testcase_14 AC 1,243 ms
29,428 KB
testcase_15 AC 1,238 ms
29,432 KB
testcase_16 AC 1,241 ms
29,556 KB
testcase_17 AC 1,249 ms
29,560 KB
testcase_18 AC 1,259 ms
29,564 KB
testcase_19 AC 1,242 ms
29,432 KB
testcase_20 AC 1,249 ms
29,560 KB
testcase_21 AC 1,256 ms
29,564 KB
testcase_22 AC 1,281 ms
29,432 KB
testcase_23 AC 1,270 ms
29,432 KB
testcase_24 AC 1,240 ms
29,552 KB
testcase_25 AC 1,240 ms
29,432 KB
testcase_26 AC 1,245 ms
29,564 KB
testcase_27 AC 1,246 ms
29,560 KB
testcase_28 AC 1,261 ms
29,432 KB
testcase_29 AC 1,198 ms
29,436 KB
testcase_30 AC 1,196 ms
29,428 KB
testcase_31 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define int long long
#define pb push_back
#define eb emplace_back
#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int (i)=(a);(i)>=(int)(b);(i)--)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define ve vector
#define vi vector<int>
#define vp vector<pair<int,int>>
#define vvi vector<vector<int>>
#define UNIQUE(a) sort(all(a)), a.erase(unique(all(a)), a.end())
#define Double double

using ll = long long;
const ll mod = 998244353;

#define sz(c) ((int)(c).size())
#define ten(x) ((int)1e##x)

typedef pair<int, int> Pii;

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 mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }

template<int mod, int primitive_root>
class NTT {
public:
	int get_mod() const { return mod; }
	void _ntt(vector<ll>& a, int sign) {
		const int n = sz(a);
		assert((n ^ (n&-n)) == 0); //n = 2^k

		const int g = 3; //g is primitive root of mod
		int h = (int)mod_pow(g, (mod - 1) / n, mod); // h^n = 1
		if (sign == -1) h = (int)mod_inv(h, mod); //h = h^-1 % mod

		//bit reverse
		int i = 0;
		for (int j = 1; j < n - 1; ++j) {
			for (int k = n >> 1; k >(i ^= k); k >>= 1);
			if (j < i) swap(a[i], a[j]);
		}

		for (int m = 1; m < n; m *= 2) {
			const int m2 = 2 * m;
			const ll base = mod_pow(h, n / m2, mod);
			ll w = 1;
			rep(x, m) {
				for (int s = x; s < n; s += m2) {
					ll u = a[s];
					ll d = a[s + m] * w % mod;
					a[s] = u + d;
					if (a[s] >= mod) a[s] -= mod;
					a[s + m] = u - d;
					if (a[s + m] < 0) a[s + m] += mod;
				}
				w = w * base % mod;
			}
		}

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

	// 畳み込み演算を行う
	vector<ll> convolution(const vector<ll>& a, const vector<ll>& b){
		int ntt_size = 1;
		while (ntt_size < sz(a) + sz(b)) ntt_size *= 2;

		vector<ll> _a = a, _b = b;
		_a.resize(ntt_size); _b.resize(ntt_size);

		ntt(_a);
		ntt(_b);

		rep(i, ntt_size){
			(_a[i] *= _b[i]) %= mod;
		}

		intt(_a);
		return _a;
	}
};

ll garner(vector<Pii> mr, int mod){
	mr.emplace_back(mod, 0);

	vector<ll> coffs(sz(mr), 1);
	vector<ll> constants(sz(mr), 0);
	rep(i, sz(mr) - 1){
		// coffs[i] * v + constants[i] == mr[i].second (mod mr[i].first) を解く
		ll v = (mr[i].second - constants[i]) * mod_inv<ll>(coffs[i], mr[i].first) % mr[i].first;
		if (v < 0) v += mr[i].first;

		for (int j = i + 1; j < sz(mr); j++) {
			(constants[j] += coffs[j] * v) %= mr[j].first;
			(coffs[j] *= mr[i].first) %= mr[j].first;
		}
	}

	return constants[sz(mr) - 1];
}

typedef NTT<167772161, 3> NTT_1;
typedef NTT<469762049, 3> NTT_2;
typedef NTT<1224736769, 3> NTT_3;
typedef NTT<998244353, 3> nntttt;

//任意のmodで畳み込み演算 O(n log n)
vector<ll> int32mod_convolution(vector<ll> a, vector<ll> b,int mod){
	for (auto& x : a) x %= mod;
	for (auto& x : b) x %= mod;
	NTT_1 ntt1; NTT_2 ntt2; NTT_3 ntt3;
	auto x = ntt1.convolution(a, b);
	auto y = ntt2.convolution(a, b);
	auto z = ntt3.convolution(a, b);

	vector<ll> ret(sz(x));
	vector<Pii> mr(3);
	rep(i, sz(x)){
		mr[0].first = ntt1.get_mod(), mr[0].second = (int)x[i];
		mr[1].first = ntt2.get_mod(), mr[1].second = (int)y[i];
		mr[2].first = ntt3.get_mod(), mr[2].second = (int)z[i];
		ret[i] = garner(mr, mod);
	}

	return ret;
}

nntttt ntT;

vector<ll> A;
vector<ll> saiki(int l, int r) {
    if (r == l + 1) return vector<int>{A[l], 1};
    vector<ll> tL = saiki(l, (l+r)/2), tR = saiki((l+r)/2, r);
    vector<ll> L, R;
    for (auto &&elm : tL) L.pb(elm);
    for (auto &&elm : tR) R.pb(elm);
    return ntT.convolution(L, R);
}
 
signed main() {
    int n; cin >> n; int m; cin >> m;
    A.resize(n); rep (i, n) cin >> A[i], A[i]--, A[i] %= mod;
    auto ans = saiki(0, n);
    rep (i, m) {
      int a; cin >> a;
      cout << ans[a] << endl;
    }
}
0