結果

問題 No.206 数の積集合を求めるクエリ
ユーザー Series_205Series_205
提出日時 2020-03-22 12:35:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 384 ms / 7,000 ms
コード長 4,219 bytes
コンパイル時間 2,178 ms
コンパイル使用メモリ 207,804 KB
実行使用メモリ 19,888 KB
最終ジャッジ日時 2023-08-26 03:35:00
合計ジャッジ時間 8,763 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 8 ms
4,376 KB
testcase_07 AC 8 ms
4,376 KB
testcase_08 AC 8 ms
4,380 KB
testcase_09 AC 8 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 12 ms
4,380 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 12 ms
4,380 KB
testcase_15 AC 12 ms
4,376 KB
testcase_16 AC 12 ms
4,380 KB
testcase_17 AC 250 ms
19,864 KB
testcase_18 AC 240 ms
19,796 KB
testcase_19 AC 247 ms
19,888 KB
testcase_20 AC 240 ms
19,888 KB
testcase_21 AC 243 ms
19,836 KB
testcase_22 AC 243 ms
19,716 KB
testcase_23 AC 247 ms
19,868 KB
testcase_24 AC 384 ms
19,888 KB
testcase_25 AC 375 ms
19,724 KB
testcase_26 AC 367 ms
19,724 KB
testcase_27 AC 328 ms
19,724 KB
testcase_28 AC 370 ms
19,732 KB
testcase_29 AC 367 ms
19,744 KB
testcase_30 AC 368 ms
19,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> using V = vector<T>;

constexpr int Mod = 998244353;
constexpr int mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;

template <typename T> constexpr bool chmax(T &a, const T &b) {
    if(a >= b) return false;
    a = b;
    return true;
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
    if(a <= b) return false;
    a = b;
    return true;
}

/*-------------------------------------------*/

class mycomplex {
    double r, i;

  public:
    constexpr mycomplex(const double &r = 0, const double &i = 0)
        : r(r), i(i) {}
    constexpr mycomplex(const complex<double> &c) : r(c.real()), i(c.imag()) {}
    constexpr double real() const { return r; }
    constexpr double imag() const { return i; }
    constexpr double abs() const { return hypot(r, i); }
    constexpr mycomplex &operator+=(const mycomplex &c) {
        r += c.r;
        i += c.i;
        return *this;
    }
    constexpr mycomplex &operator-=(const mycomplex &c) {
        r -= c.r;
        i -= c.i;
        return *this;
    }
    constexpr mycomplex &operator*=(const mycomplex &c) {
        double x = r;
        r = r * c.r - i * c.i;
        i = x * c.i + i * c.r;
        return *this;
    }
    constexpr mycomplex &operator/=(const mycomplex &c) {
        *this *= mycomplex(c.r, c.i);
        double dnm = c.r * c.r - c.i * c.i;
        r /= dnm;
        i /= dnm;
        return *this;
    }
    template <typename T> constexpr mycomplex &operator+=(const T &c) {
        return operator+=(mycomplex(c));
    }
    template <typename T> constexpr mycomplex operator+(const T &c) const {
        return mycomplex(*this) += c;
    }
    template <typename T> constexpr mycomplex &operator-=(const T &c) {
        return operator-=(mycomplex(c));
    }
    template <typename T> constexpr mycomplex operator-(const T &c) const {
        return mycomplex(*this) -= c;
    }
    template <typename T> constexpr mycomplex &operator*=(const T &c) {
        return operator*=(mycomplex(c));
    }
    template <typename T> constexpr mycomplex operator*(const T &c) const {
        return mycomplex(*this) *= c;
    }
    template <typename T> constexpr mycomplex &operator/=(const T &c) {
        return operator/=(mycomplex(c));
    }
    template <typename T> constexpr mycomplex operator/(const T &c) const {
        return mycomplex(*this) /= c;
    }
};

class FastFourierTransform {
    static void dft(V<mycomplex> &func, const int &inverse) {
        int sz = func.size();
        if(sz == 1) return;
        V<mycomplex> va, vb;
        rep(i, sz / 2) {
            va.push_back(func[2 * i]);
            vb.push_back(func[2 * i + 1]);
        }
        dft(va, inverse);
        dft(vb, inverse);
        mycomplex now(1);
        mycomplex zeta(polar(1.0, inverse * 2.0 * acos(-1) / sz));
        rep(i, sz) {
            func[i] = va[i & (sz >> 1) - 1] + now * vb[i & (sz >> 1) - 1];
            now *= zeta;
        }
    }

  public:
    template <typename T>
    static V<double> multipli(const V<T> &f, const V<T> &g) {
        V<mycomplex> nf, ng;
        int sz = 1;
        int n = f.size() + g.size();
        while(sz < n)
            sz <<= 1;
        nf.resize(sz);
        ng.resize(sz);
        rep(i, f.size()) nf[i] = f[i];
        rep(i, g.size()) ng[i] = g[i];
        dft(nf, 1);
        dft(ng, 1);
        rep(i, sz) nf[i] *= ng[i];
        dft(nf, -1);
        V<double> res;
        for(const auto &z : nf)
            res.push_back(z.real() / sz);
        return res;
    }
};

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

    int l, m, n;
    cin >> l >> m >> n;
    V<bool> a(n + 1), b(n + 1);
    rep(i, l) {
        int x;
        cin >> x;
        a[x] = true;
    }
    rep(i, m) {
        int x;
        cin >> x;
        b[n - x] = true;
    }
    int q;
    cin >> q;
    V<double> res = FastFourierTransform::multipli(a, b);
    rep(i, q) cout << int(res[i + n] + 0.1) << endl;

    return 0;
}
0