結果

問題 No.206 数の積集合を求めるクエリ
ユーザー ace_amuroace_amuro
提出日時 2021-01-20 18:33:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 292 ms / 7,000 ms
コード長 2,120 bytes
コンパイル時間 2,282 ms
コンパイル使用メモリ 71,220 KB
実行使用メモリ 15,032 KB
最終ジャッジ日時 2023-08-24 22:29:44
合計ジャッジ時間 8,601 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,680 KB
testcase_01 AC 3 ms
7,572 KB
testcase_02 AC 3 ms
7,524 KB
testcase_03 AC 2 ms
7,636 KB
testcase_04 AC 2 ms
7,764 KB
testcase_05 AC 2 ms
7,520 KB
testcase_06 AC 8 ms
7,832 KB
testcase_07 AC 8 ms
7,740 KB
testcase_08 AC 9 ms
7,644 KB
testcase_09 AC 8 ms
7,788 KB
testcase_10 AC 3 ms
7,612 KB
testcase_11 AC 2 ms
7,768 KB
testcase_12 AC 9 ms
7,824 KB
testcase_13 AC 9 ms
7,640 KB
testcase_14 AC 9 ms
7,656 KB
testcase_15 AC 9 ms
7,640 KB
testcase_16 AC 9 ms
7,636 KB
testcase_17 AC 284 ms
14,852 KB
testcase_18 AC 273 ms
14,856 KB
testcase_19 AC 281 ms
14,912 KB
testcase_20 AC 271 ms
14,868 KB
testcase_21 AC 276 ms
14,912 KB
testcase_22 AC 275 ms
14,968 KB
testcase_23 AC 281 ms
15,032 KB
testcase_24 AC 292 ms
14,968 KB
testcase_25 AC 288 ms
14,984 KB
testcase_26 AC 280 ms
14,872 KB
testcase_27 AC 276 ms
14,932 KB
testcase_28 AC 283 ms
15,032 KB
testcase_29 AC 283 ms
14,980 KB
testcase_30 AC 279 ms
14,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
LL ww[101];
LL* e = ww + 50;
const int MAXN = 4e5 + 5;
LL P = 998244353;

LL qpow(LL a, LL k, LL p) {
    LL c = 1;
    while (k) {
        if (k & 1) c = (c * a) % p;
        k >>= 1;
        a = (a * a) % p;
    }
    return c;
}

void primeroot(LL* e, const LL& P, const LL& g) {
    int s = 0; LL q = P - 1;
    while ((q & 1) == 0) {s++; q >>= 1;}
    LL w = qpow(g, q, P);
    LL invw = qpow(w, P - 2, P);
    for (int h = s; h >= 0; h--) {
        e[h] = w;
        e[-h] = invw;
        w = (w * w) % P;
        invw = (invw * invw) % P;
    }
}

void ntt(LL* f, const int& h, const int& type) {
    if (h == 0) return;
    LL f0[1 << (h - 1)], f1[1 << (h - 1)];
    int n = 1 << h;
    for (int i = 0; i < n; i+=2) f0[i/2] = f[i];
    ntt(f0, h - 1, type);
    for (int i = 1; i < n; i+=2) f1[i/2] = f[i];
    ntt(f1, h - 1, type);
    LL w = e[type * h];
    LL x = 1;
    for (int k = 0; k < n / 2; k++) {
        f[k] = f0[k] + x * f1[k];
        f[k] %= P;
        f[k + n / 2] = f0[k] - x * f1[k];
        f[k + n / 2] %= P;
        x *= w; x %= P;
    }
}


LL a[MAXN], b[MAXN], c[MAXN];
int main() {
    primeroot(e, P, 3);
    int n, m,l, t,q;
    scanf("%d%d%d", &l, &m, &n);
    for (int i = 0; i < l; i++) {scanf("%d", &t);a[t]=1;}
    for (int i = 0; i < m; i++) {scanf("%d", &t);b[n-t]=1;}
    scanf("%d", &q);    
    int deg = 1, h = 0;
    while (deg < 2*n) {deg <<= 1; h++;}
    // printf("a:");for (int i = 0; i < deg; i++)printf(" %lld",a[i]);printf("\n");
    // printf("b:");for (int i = 0; i < deg; i++)printf(" %lld",b[i]);printf("\n");
    ntt(a, h, 1);
    ntt(b, h, 1);
    for (int i = 0; i < deg; i++) c[i] = a[i] * b[i] % P;
    ntt(c, h, -1);
    LL inv = qpow(deg, P - 2, P);
    for (int i = 0; i < deg; i++) {
        c[i] = (c[i] * inv) % P;
        c[i] = (c[i] + P) % P;
    }
    // printf("c:");for (int i = 0; i < deg; i++)printf(" %lld",c[i]);printf("\n");
    for(int v=0;v<q;v++){
        printf("%lld\n",c[n+v]);
    }

    return 0;
}
0