結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー roarisroaris
提出日時 2020-12-12 03:29:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 366 ms / 3,500 ms
コード長 3,013 bytes
コンパイル時間 2,242 ms
コンパイル使用メモリ 208,800 KB
実行使用メモリ 8,764 KB
最終ジャッジ日時 2023-10-20 01:55:04
合計ジャッジ時間 12,257 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 14 ms
4,348 KB
testcase_04 AC 9 ms
4,348 KB
testcase_05 AC 11 ms
4,348 KB
testcase_06 AC 9 ms
4,348 KB
testcase_07 AC 8 ms
4,348 KB
testcase_08 AC 10 ms
4,348 KB
testcase_09 AC 11 ms
4,348 KB
testcase_10 AC 6 ms
4,348 KB
testcase_11 AC 8 ms
4,348 KB
testcase_12 AC 5 ms
4,348 KB
testcase_13 AC 364 ms
8,764 KB
testcase_14 AC 363 ms
8,764 KB
testcase_15 AC 364 ms
8,764 KB
testcase_16 AC 364 ms
8,764 KB
testcase_17 AC 363 ms
8,764 KB
testcase_18 AC 362 ms
8,764 KB
testcase_19 AC 363 ms
8,764 KB
testcase_20 AC 366 ms
8,764 KB
testcase_21 AC 363 ms
8,764 KB
testcase_22 AC 364 ms
8,764 KB
testcase_23 AC 363 ms
8,764 KB
testcase_24 AC 365 ms
8,764 KB
testcase_25 AC 364 ms
8,764 KB
testcase_26 AC 364 ms
8,764 KB
testcase_27 AC 363 ms
8,764 KB
testcase_28 AC 364 ms
8,764 KB
testcase_29 AC 349 ms
8,764 KB
testcase_30 AC 351 ms
8,764 KB
testcase_31 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
typedef long long ll;
const int MOD = 998244353;

int N, Q;
int A[200010];

//https://yukicoder.me/submissions/536413
template<const int md>
struct NTT{
	inline void add(int &a, int b) { a += b; if(a >= md) a -= md; }
	inline void sub(int &a, int b) { a -= b; if(a < 0) a += md; }
	inline int mul(int a, int b) { return (int)((ll)a*b%md); }
	inline int power(int a, long long b) {
		int res = 1;
		while (b > 0) {
			if (b & 1) res = mul(res, a);
	    	a = mul(a, a);
			b >>= 1;
		}
		return res;
	}
	inline int inv(int a) {
		a %= md;
		if (a < 0) a += md;
		int b = md, u = 0, v = 1;
		while (a) {
			int t = b / a;
			b -= t * a; swap(a, b);
			u -= t * v; swap(u, v);
		}
		assert(b == 1);
		if (u < 0) u += md;
		return u;
	}
	
 	int max_base, root;
	vector<int> dw, idw;
	NTT() {
		int tmp = md - 1;
		max_base = 0;
		while (tmp % 2 == 0) {
			tmp /= 2;
			max_base++;
		}
		root = 2;
		while (power(root, (md-1)>>1) == 1) root++;
		dw.resize(max_base); idw.resize(max_base);
		rep(i, max_base){
			sub(dw[i], power(root, (md-1) >> (i+2)));
			idw[i] = inv(dw[i]);
		}
	}
	void fft(vector<int> &a, bool inv) {
		const int n = a.size();
		assert((n & (n - 1)) == 0);
		assert(__builtin_ctz(n) <= max_base);
		if(!inv){
			for(int m=n;m>>=1;){
				int w = 1;
				for(int s=0,k=0; s<n; s += 2*m){
					for(int i=s, j=s+m ; i < s+m; ++i, ++j) {
						int x = a[i], y = mul(a[j], w);
						a[j] = (x>=y?x-y:x+md-y);
						a[i] = (x+y>=md?x+y-md:x+y);
					}
					w = mul(w, dw[__builtin_ctz(++k)]);
				}
			}
		}
		else{
			for(int m=1;m<n;m*=2){
				int w = 1;
				for(int s=0,k=0; s<n; s += 2*m){
					for(int i=s, j=s+m ; i < s+m; ++i, ++j) {
						int x = a[i], y = a[j];
						a[j] = (x>=y?x-y:x+md-y);
						a[j] = mul(a[j], w);
						a[i] = (x+y>=md?x+y-md:x+y);
					}
					w = mul(w, idw[__builtin_ctz(++k)]);
				}
			}
		}
	}
	vector<int> multiply(vector<int> a, vector<int> b, int eq = 0) {
		int need = a.size() + b.size() - 1;
		int nbase = 0;
		while ((1 << nbase) < need) nbase++;
		int sz = 1 << nbase;
		a.resize(sz);
		b.resize(sz);
		fft(a, 0);
		if (eq) b = a; else fft(b, 0);
		int inv_sz = inv(sz);
		for (int i = 0; i < sz; i++) {
			a[i] = mul(mul(a[i], b[i]), inv_sz);
		}
		fft(a, 1);
		a.resize(need);
		return a;
	}
	vector<int> square(vector<int> a) {
		return multiply(a, a, 1);
	}
};

NTT<MOD> ntt;

vector<int> rec(int l, int r) {
    if (r-l==1) return {A[l]-1, 1};
    int m = (l+r)/2;
    vector<int> f = rec(l, m);
    vector<int> g = rec(m, r);
    return ntt.multiply(f, g);
}

int main() {
    //ループ変数が被っていないか?
    //制約を確認しているか?
    //変数のtypoがないか?
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N >> Q;
    rep(i, N) {
        ll Ai; cin >> Ai;
        A[i] = Ai%MOD;
    }
    
    vector<int> ans = rec(0, N);
    rep(i, Q) {
        int B; cin >> B;
        cout << ans[B] << endl;
    }
}
0