結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー milanis48663220milanis48663220
提出日時 2020-05-29 23:16:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 428 ms / 3,500 ms
コード長 2,627 bytes
コンパイル時間 854 ms
コンパイル使用メモリ 89,836 KB
実行使用メモリ 17,196 KB
最終ジャッジ日時 2024-04-24 01:16:02
合計ジャッジ時間 9,940 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 14 ms
5,376 KB
testcase_04 AC 10 ms
5,376 KB
testcase_05 AC 10 ms
5,376 KB
testcase_06 AC 9 ms
5,376 KB
testcase_07 AC 9 ms
5,376 KB
testcase_08 AC 11 ms
5,376 KB
testcase_09 AC 12 ms
5,376 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 8 ms
5,376 KB
testcase_12 AC 6 ms
5,376 KB
testcase_13 AC 421 ms
17,068 KB
testcase_14 AC 425 ms
17,064 KB
testcase_15 AC 422 ms
17,196 KB
testcase_16 AC 421 ms
17,068 KB
testcase_17 AC 419 ms
17,196 KB
testcase_18 AC 425 ms
17,064 KB
testcase_19 AC 423 ms
17,192 KB
testcase_20 AC 427 ms
17,192 KB
testcase_21 AC 417 ms
17,196 KB
testcase_22 AC 423 ms
17,064 KB
testcase_23 AC 422 ms
17,068 KB
testcase_24 AC 425 ms
17,196 KB
testcase_25 AC 427 ms
17,196 KB
testcase_26 AC 426 ms
17,136 KB
testcase_27 AC 422 ms
17,192 KB
testcase_28 AC 428 ms
17,176 KB
testcase_29 AC 413 ms
17,072 KB
testcase_30 AC 409 ms
17,068 KB
testcase_31 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

const ll MOD = 998244353;
ll N, Q;
ll A[200000], B[10000]; 
    
#define root 3
    
ll add(const ll x, const ll y)
{
    return (x + y < MOD) ? x + y : x + y - MOD;
}
    
ll sub(const ll x, const ll y)
{
    return (x >= y) ? (x - y) : (MOD - y + x);
}
    
ll mul(const ll x, const ll y)
{
    return (unsigned long long)x * y % MOD;
}
    
ll mod_pow(ll x, ll n)
{
    ll res = 1;
    while(n > 0){
        if(n & 1){ res = mul(res, x); }
        x = mul(x, x);
        n >>= 1;
    }
    return res;
}
    
ll inverse(const ll x)
{
    return mod_pow(x, MOD - 2);
}
    
void ntt(vector<ll>& a, const bool rev = false)
{
    ll i, j, k, l, p, q, r, s;
    const ll size = a.size();
    if(size == 1) return;
    vector<ll> b(size);
    r = rev ? (MOD - 1 - (MOD - 1) / size) : (MOD - 1) / size;
    s = mod_pow(root, r);
    vector<ll> kp(size / 2 + 1, 1);
    for(i = 0; i < size / 2; ++i) kp[i + 1] = mul(kp[i], s);
    for(i = 1, l = size / 2; i < size; i <<= 1, l >>= 1){
        for(j = 0, r = 0; j < l; ++j, r += i){
            for(k = 0, s = kp[i * j]; k < i; ++k){
                p = a[k + r], q = a[k + r + size / 2];
                b[k + 2 * r] = add(p, q);
                b[k + 2 * r + i] = mul(sub(p, q), s);
            }
        }
        swap(a, b);
    }
    if(rev){
        s = inverse(size);
        for(i = 0; i < size; i++){ a[i] = mul(a[i], s); }
    }
}
    
vector<ll> convolute(const vector<ll>& a, const vector<ll>& b)
{
    const ll size = (ll)a.size() + (ll)b.size() - 1;
    ll t = 1;
    while(t < size){ t <<= 1; }
    vector<ll> A(t, 0), B(t, 0);
    for(ll i = 0; i < (ll)a.size(); i++){ A[i] = a[i]; }
    for(ll i = 0; i < (ll)b.size(); i++){ B[i] = b[i]; }
    ntt(A), ntt(B);
    for (ll i = 0; i < t; i++){ A[i] = mul(A[i], B[i]); }
    ntt(A, true);
    A.resize(size);
    return A;
}

vector<ll> calc(ll l, ll r){
    if(l == r){
        vector<ll> ans;
        ans.push_back(A[l]-1); ans.push_back(1);
        return ans;
    }
    ll c = (l+r)/2;
    auto vl = calc(l, c);
    auto vr = calc(c+1, r);
    auto ans =  convolute(vl, vr);
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N >> Q;
    for(ll i = 0; i < N; i++) {
        cin >> A[i];
        A[i]%= MOD;
    }
    for(ll i = 0; i < Q; i++) cin >> B[i];
    auto conv = calc(0, N-1);
    for(ll i = 0; i < Q; i++) {
        cout << conv[B[i]] << endl;
    }    
}
0