結果

問題 No.1066 #いろいろな色 / Red and Blue and more various colors (Easy)
ユーザー pockynypockyny
提出日時 2020-05-30 02:33:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 2,480 bytes
コンパイル時間 763 ms
コンパイル使用メモリ 80,008 KB
実行使用メモリ 9,936 KB
最終ジャッジ日時 2023-08-06 14:10:01
合計ジャッジ時間 2,503 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,008 KB
testcase_01 AC 5 ms
8,000 KB
testcase_02 AC 4 ms
8,004 KB
testcase_03 AC 5 ms
8,216 KB
testcase_04 AC 4 ms
8,028 KB
testcase_05 AC 4 ms
8,040 KB
testcase_06 AC 4 ms
8,004 KB
testcase_07 AC 5 ms
7,976 KB
testcase_08 AC 21 ms
8,864 KB
testcase_09 AC 4 ms
8,104 KB
testcase_10 AC 35 ms
9,800 KB
testcase_11 AC 18 ms
8,908 KB
testcase_12 AC 17 ms
8,820 KB
testcase_13 AC 18 ms
8,808 KB
testcase_14 AC 18 ms
8,976 KB
testcase_15 AC 33 ms
9,620 KB
testcase_16 AC 12 ms
8,464 KB
testcase_17 AC 13 ms
8,572 KB
testcase_18 AC 31 ms
9,864 KB
testcase_19 AC 18 ms
8,912 KB
testcase_20 AC 4 ms
8,224 KB
testcase_21 AC 22 ms
9,176 KB
testcase_22 AC 4 ms
8,048 KB
testcase_23 AC 4 ms
8,036 KB
testcase_24 AC 4 ms
8,024 KB
testcase_25 AC 36 ms
9,768 KB
testcase_26 AC 35 ms
9,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
const int gen = 3;
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];
                ll 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 = x * n_inv % mod;
}

void multiply(vector<ll> & input1,vector<ll> & input2){
    int l = input1.size(),r = input2.size();
    int s = 1;
    while(s<l + r) s <<= 1;
    input1.resize(s); input2.resize(s);
    ntt(input1); ntt(input2);
    for(int i=0;i<input1.size();i++){
        (input1[i] *= input2[i]) %= mod;
    }
    intt(input1);
    input1.resize(l + r);
}

vector<ll> v[200010];
void solve(int l,int r){
    if(l==r) return;
    if(r - l==1){
        multiply(v[l],v[r]);
        return ;
    }
    solve(l,(l + r)/2); solve((l + r)/2 + 1,r);
    multiply(v[l],v[(l + r)/2 + 1]);
}

int main(){
    int i,n,q;
    cin >> n >> q;
    for(i=0;i<n;i++){
        ll a; cin >> a;
        v[i].push_back((a - 1 + mod)%mod); v[i].push_back(1);
    }
    solve(0,n - 1);
    for(i=0;i<q;i++){
        int b; cin >> b;
        cout << v[0][b] << endl;
    }
}
0