結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー Alex WiceAlex Wice
提出日時 2020-05-29 22:38:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,251 ms / 3,500 ms
コード長 2,578 bytes
コンパイル時間 2,599 ms
コンパイル使用メモリ 205,508 KB
実行使用メモリ 65,336 KB
最終ジャッジ日時 2024-04-23 23:59:30
合計ジャッジ時間 25,918 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 15 ms
5,376 KB
testcase_04 AC 11 ms
5,376 KB
testcase_05 AC 12 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 13 ms
5,376 KB
testcase_10 AC 7 ms
5,376 KB
testcase_11 AC 8 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 1,212 ms
65,200 KB
testcase_14 AC 1,198 ms
65,204 KB
testcase_15 AC 1,201 ms
65,328 KB
testcase_16 AC 1,216 ms
65,336 KB
testcase_17 AC 1,207 ms
65,204 KB
testcase_18 AC 1,202 ms
65,332 KB
testcase_19 AC 1,205 ms
65,328 KB
testcase_20 AC 1,203 ms
65,332 KB
testcase_21 AC 1,219 ms
65,284 KB
testcase_22 AC 1,209 ms
65,328 KB
testcase_23 AC 1,210 ms
65,204 KB
testcase_24 AC 1,251 ms
65,208 KB
testcase_25 AC 1,206 ms
65,328 KB
testcase_26 AC 1,205 ms
65,336 KB
testcase_27 AC 1,211 ms
65,332 KB
testcase_28 AC 1,208 ms
65,204 KB
testcase_29 AC 1,194 ms
65,332 KB
testcase_30 AC 1,184 ms
65,332 KB
testcase_31 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i, a, b) for (int i = a; i < int(b); ++i)
#define trav(a, v) for (auto& a : v)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const ll mod = (119 << 23) + 1, root = 3;  // = 998244353
// For p < 2^30 there is also e.g. (5 << 25, 3), (7 << 26, 3),
// (479 << 21, 3) and (483 << 21, 5). The last two are > 10^9.

ll modpow(ll a, ll e)
{
    if (e == 0)
        return 1;
    ll x = modpow(a * a % mod, e >> 1);
    return e & 1 ? x * a % mod : x;
}

typedef vector<ll> vl;
void ntt(ll* x, ll* temp, ll* roots, int N, int skip)
{
    if (N == 1)
        return;
    int n2 = N / 2;
    ntt(x, temp, roots, n2, skip * 2);
    ntt(x + skip, temp, roots, n2, skip * 2);
    rep(i, 0, N) temp[i] = x[i * skip];
    rep(i, 0, n2)
    {
        ll s = temp[2 * i], t = temp[2 * i + 1] * roots[skip * i];
        x[skip * i] = (s + t) % mod;
        x[skip * (i + n2)] = (s - t) % mod;
    }
}
void ntt(vl& x, bool inv = false)
{
    ll e = modpow(root, (mod - 1) / sz(x));
    if (inv)
        e = modpow(e, mod - 2);
    vl roots(sz(x), 1), temp = roots;
    rep(i, 1, sz(x)) roots[i] = roots[i - 1] * e % mod;
    ntt(&x[0], &temp[0], &roots[0], sz(x), 1);
}
vl conv(vl a, vl b)
{
    int s = sz(a) + sz(b) - 1;
    if (s <= 0)
        return {};
    int L = s > 1 ? 32 - __builtin_clz(s - 1) : 0, n = 1 << L;
    if ((rand() % 2 == 0) &&
        s <= 200) {  // (factor 10 optimization for |a|,|b| = 10)
        vl c(s);
        rep(i, 0, sz(a)) rep(j, 0, sz(b)) c[i + j] =
            (c[i + j] + a[i] * b[j]) % mod;
        trav(x, c) if (x < 0) x += mod;
        return c;
    }
    a.resize(n);
    ntt(a);
    b.resize(n);
    ntt(b);
    vl c(n);
    ll d = modpow(n, mod - 2);
    rep(i, 0, n) c[i] = a[i] * b[i] % mod * d % mod;
    ntt(c, true);
    c.resize(s);
    trav(x, c) if (x < 0) x += mod;
    return c;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int N, Q;
    cin >> N >> Q;
    vector<vl> polys;
    polys.assign(N, {});
    for (int i = 0; i < N; ++i) {
        ll x;
        cin >> x;
        polys[i].push_back(x % mod - 1);
        polys[i].push_back(1);
    }

    int skip = 1;
    while (skip < N) {
        for (int i = 0; i + skip < N; i += 2 * skip) {
            polys[i] = conv(polys[i], polys[i + skip]);
        }
        skip *= 2;
    }

    for (int i = 0; i < Q; ++i) {
        int q;
        cin >> q;
        cout << polys[0][q] % mod << "\n";
    }
    return 0;
}
0