Submission #70983599


Source Code Expand

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

const int mod = 998244353;
const int g = 3;
const int v_max = 500000;

vector<ll> fact(v_max + 1);
vector<ll> invfact(v_max + 1);

ll power(ll a, ll b) {
    ll res = 1;
    a %= mod;
    while (b > 0) {
        if (b & 1) res = (res * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return res;
}

ll modInverse(ll n) {
    return power(n, mod - 2);
}

void bit_reversal(vector<ll>& a, int n, int log_n) {
    for (int i = 0; i < n; ++i) {
        int rev = 0;
        for (int j = 0; j < log_n; ++j) {
            if ((i >> j) & 1) {
                rev |= (1 << (log_n - 1 - j));
            }
        }
        if (i < rev) {
            swap(a[i], a[rev]);
        }
    }
}

void ntt(vector<ll>& a, bool invert) {
    int n = a.size();
    int log_n = 0;
    while ((1 << log_n) < n) log_n++;

    bit_reversal(a, n, log_n);

    for (int len = 2; len <= n; len <<= 1) {
        ll wlen = power(g, (mod - 1) / len);
        if (invert) {
            wlen = modInverse(wlen);
        }

        for (int i = 0; i < n; i += len) {
            ll w = 1;
            for (int j = 0; j < len / 2; ++j) {
                ll u = a[i + j];
                ll v = (a[i + j + len / 2] * w) % mod;

                a[i + j] = (u + v) % mod;
                a[i + j + len / 2] = (u - v + mod) % mod;

                w = (w * wlen) % mod;
            }
        }
    }

    if (invert) {
        ll n_inv = modInverse(n);
        for (int i = 0; i < n; ++i) {
            a[i] = (a[i] * n_inv) % mod;
        }
    }
}

void solve()
{
    int n, m;
    cin >> n >> m;

    fact[0] = 1;
    invfact[0] = 1;
    for (int i = 1; i <= v_max; ++i) {
        fact[i] = (fact[i - 1] * i) % mod;
    }
    invfact[v_max] = modInverse(fact[v_max]);
    for (int i = v_max - 1; i >= 1; --i) {
        invfact[i] = (invfact[i + 1] * (i + 1)) % mod;
    }

    vector<int> count_a(v_max + 1, 0);
    vector<int> count_b(v_max + 1, 0);

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        count_a[x]++;
    }
    for (int i = 0; i < m; ++i) {
        int x;
        cin >> x;
        count_b[x]++;
    }

    int l = 1;
    while (l <= 2 * v_max) {
        l <<= 1;
    }

    vector<ll> poly_p(l, 0);
    vector<ll> poly_q(l, 0);

    for (int k = 0; k <= v_max; ++k) {
        int i = v_max - k;
        poly_p[k] = (static_cast<ll>(count_a[i]) * fact[i]) % mod;
    }

    for (int j = 0; j <= v_max; ++j) {
        poly_q[j] = (static_cast<ll>(count_b[j]) * invfact[j]) % mod;
    }

    ntt(poly_p, false);
    ntt(poly_q, false);

    vector<ll> poly_r(l);
    for (int i = 0; i < l; ++i) {
        poly_r[i] = (poly_p[i] * poly_q[i]) % mod;
    }

    ntt(poly_r, true);

    ll totalsum = 0;
    for (int k = 0; k <= v_max; ++k) {
        ll c_k = poly_r[v_max - k];
        ll h_k = invfact[k];

        totalsum = (totalsum + c_k * h_k) % mod;
    }

    cout << totalsum << "\n";
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    //cin>>t;
    t=1;
    while(t--) solve();
    return 0;
}

Submission Info

Submission Time
Task G - Sum of Binom(A, B)
User Luongdung
Language C++23 (GCC 15.2.0)
Score 575
Code Size 3281 Byte
Status AC
Exec Time 279 ms
Memory 39744 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 575 / 575
Status
AC × 3
AC × 20
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 234 ms 39636 KiB
00_sample_01.txt AC 237 ms 39672 KiB
00_sample_02.txt AC 237 ms 39628 KiB
01_random_00.txt AC 269 ms 39608 KiB
01_random_01.txt AC 250 ms 39744 KiB
01_random_02.txt AC 262 ms 39636 KiB
01_random_03.txt AC 264 ms 39632 KiB
01_random_04.txt AC 274 ms 39740 KiB
01_random_05.txt AC 279 ms 39608 KiB
01_random_06.txt AC 274 ms 39692 KiB
01_random_07.txt AC 273 ms 39740 KiB
01_random_08.txt AC 262 ms 39584 KiB
01_random_09.txt AC 268 ms 39632 KiB
02_handmade_00.txt AC 237 ms 39668 KiB
02_handmade_01.txt AC 276 ms 39632 KiB
02_handmade_02.txt AC 260 ms 39736 KiB
02_handmade_03.txt AC 260 ms 39736 KiB
02_handmade_04.txt AC 262 ms 39736 KiB
02_handmade_05.txt AC 271 ms 39632 KiB
02_handmade_06.txt AC 268 ms 39740 KiB