結果

問題 No.206 数の積集合を求めるクエリ
ユーザー outlineoutline
提出日時 2021-02-09 03:57:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 7,000 ms
コード長 4,492 bytes
コンパイル時間 1,534 ms
コンパイル使用メモリ 142,788 KB
実行使用メモリ 14,904 KB
最終ジャッジ日時 2023-09-20 12:54:53
合計ジャッジ時間 3,779 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 42 ms
14,868 KB
testcase_18 AC 34 ms
14,712 KB
testcase_19 AC 41 ms
14,728 KB
testcase_20 AC 33 ms
14,868 KB
testcase_21 AC 36 ms
14,772 KB
testcase_22 AC 36 ms
14,804 KB
testcase_23 AC 41 ms
14,708 KB
testcase_24 AC 47 ms
14,780 KB
testcase_25 AC 46 ms
14,840 KB
testcase_26 AC 39 ms
14,676 KB
testcase_27 AC 36 ms
14,836 KB
testcase_28 AC 41 ms
14,904 KB
testcase_29 AC 41 ms
14,904 KB
testcase_30 AC 38 ms
14,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

namespace FastFourierTransform{
    using real = double;

    // 複素数構造体
    struct C{
        real x, y;
        C() : x(0), y(0) {}
        C(real x, real y) : x(x), y(y) {}

        inline C operator+(const C &c) const {return C(x + c.x, y + c.y);}
        inline C operator-(const C &c) const {return C(x - c.x, y - c.y);}
        inline C operator*(const C &c) const {return C(x * c.x - y * c.y, x * c.y + y * c.x);}
        // 複素共役演算子
        inline C conj() const {return C(x, -y);}
    };

    const real PI = acosl(-1);
    int base = 1;
    vector<C> rts = {{0,0},
                     {1,0}};
    vector<int> rev = {0,1};

    void ensure_base(int nbase){
        if(nbase <= base)   return;
        rev.resize(1 << nbase);
        rts.resize(1 << nbase);
        for(int i = 0; i < (1 << nbase); ++i){
            rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
        }
        while(base < nbase){
            real angle = PI * 2.0 / (1 << (base + 1));
            for(int i = 1 << (base - 1); i < (1 << base); ++i){
                rts[i << 1] = rts[i];
                real angle_i = angle * (2*i + 1 - (1 << base));
                rts[(i << 1) + 1] = C(cos(angle_i), sin(angle_i));
            }
            ++base;
        }
    }

    void fft(vector<C> &a, int n){
        assert((n & (n - 1)) == 0);
        int zeros = __builtin_ctz(n);
        ensure_base(zeros);
        int shift = base - zeros;
        for(int i = 0; i < n; ++i){
            if(i < (rev[i] >> shift)){
                swap(a[i], a[rev[i] >> shift]);
            }
        }
        for(int k = 1; k < n; k <<= 1){
            for(int i = 0; i < n; i += 2 * k){
                for(int j = 0; j < k; ++j){
                    C z = a[i + j + k] * rts[j + k];
                    a[i + j + k] = a[i + j] - z;
                    a[i + j] = a[i + j] + z;
                }
            }
        }
    }

    vector<int64_t> multiply(const vector<int> &a, const vector<int> &b){
        int need = (int)a.size() + (int)b.size() - 1;
        int nbase = 1;
        while((1 << nbase) < need)  ++nbase;
        ensure_base(nbase);
        int sz = 1 << nbase;
        vector<C> fa(sz);
        for(int i = 0; i < sz; ++i){
            int x = (i < (int)a.size() ? a[i] : 0);
            int y = (i < (int)b.size() ? b[i] : 0);
            fa[i] = C(x, y);
        }
        fft(fa, sz);
        C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0);
        for(int i = 0; i <= (sz >> 1); ++i){
            int j = (sz - i) & (sz - 1);
            C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r;
            fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r;
            fa[i] = z;
        }
        for(int i = 0; i < (sz >> 1); ++i){
            C A0 = (fa[i] + fa[i + (sz >> 1)]) * t;
            C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * rts[(sz >> 1) + i];
            fa[i] = A0 + A1 * s;
        }
        fft(fa, sz >> 1);
        vector<int64_t> ret(need);
        for(int i = 0; i < need; ++i){
            ret[i] = llround(i & 1 ? fa[i >> 1].y : fa[i >> 1].x);
        }
        return ret;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int L, M, N, A, B, Q;
    cin >> L >> M >> N;
    vector<int> a(N), b(N);
    for(int i = 0; i < L; ++i){
        cin >> A;
        a[A - 1] = 1;
    }
    for(int i = 0; i < M; ++i){
        cin >> B;
        b[N - B] = 1;
    }
    auto c = FastFourierTransform::multiply(a, b);
    cin >> Q;
    for(int i = 0; i < Q; ++i){
        cout << c[N - 1 + i] << '\n';
    }

    return 0;
}
0