結果

問題 No.206 数の積集合を求めるクエリ
ユーザー siro53siro53
提出日時 2023-09-26 00:32:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 260 ms / 7,000 ms
コード長 6,680 bytes
コンパイル時間 2,146 ms
コンパイル使用メモリ 206,212 KB
実行使用メモリ 14,884 KB
最終ジャッジ日時 2023-09-26 00:32:18
合計ジャッジ時間 7,273 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,384 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 7 ms
4,380 KB
testcase_07 AC 7 ms
4,376 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 8 ms
4,376 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 7 ms
4,380 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 7 ms
4,376 KB
testcase_17 AC 245 ms
14,816 KB
testcase_18 AC 235 ms
14,812 KB
testcase_19 AC 239 ms
14,856 KB
testcase_20 AC 227 ms
14,884 KB
testcase_21 AC 260 ms
14,864 KB
testcase_22 AC 227 ms
14,880 KB
testcase_23 AC 238 ms
14,780 KB
testcase_24 AC 244 ms
14,828 KB
testcase_25 AC 259 ms
14,768 KB
testcase_26 AC 257 ms
14,880 KB
testcase_27 AC 230 ms
14,776 KB
testcase_28 AC 234 ms
14,852 KB
testcase_29 AC 239 ms
14,868 KB
testcase_30 AC 259 ms
14,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "combined.cpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < int(n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define REVERSE(v) reverse(ALL(v))
#define SZ(v) ((int)(v).size())
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void Case(int i) { cout << "Case #" << i << ": "; }
int popcount(int x) { return __builtin_popcount(x); }
ll popcount(ll x) { return __builtin_popcountll(x); }
#pragma endregion Macros

#line 2 "/Users/siro53/kyo-pro/compro_library/math/convolution/convolution.hpp"

#line 7 "/Users/siro53/kyo-pro/compro_library/math/convolution/convolution.hpp"

namespace fft {
    template <typename D> struct Complex {
        D x, y;
        Complex(): x(0), y(0) {};
        Complex(D x, D y) : x(x), y(y) {}
        Complex &operator+=(const Complex &c) {
            x += c.x;
            y += c.y;
            return (*this);
        }
        Complex &operator-=(const Complex &c) {
            x -= c.x;
            y -= c.y;
            return (*this);
        }
        Complex &operator*=(const Complex &c) {
            D nx = x * c.x - y * c.y;
            D ny = x * c.y + y * c.x;
            x = nx, y = ny;
            return (*this);
        }
        Complex &operator/=(const Complex& c) {
            // (a + bi) / (c + di)
            // (a + bi)(c - di) / (c^2 + d^2)
            // (ac + bd) + (bc - ad)i / (c^2 + d^2)
            D nx = (x * c.x + y * c.y) / (c.x * c.x + c.y * c.y);
            D ny = (y * c.x - x * c.y) / (c.x * c.x + c.y * c.y);
            x = nx, y = ny;
            return (*this);
        }
        Complex operator-() const { return Complex(-x, -y); }
        Complex operator+(const Complex &c) const { return Complex(*this) += c; }
        Complex operator-(const Complex &c) const { return Complex(*this) -= c; }
        Complex operator*(const Complex &c) const { return Complex(*this) *= c; }
        Complex operator/(const Complex &c) const { return Complex(*this) /= c; }
    };

    template<typename D>
    constexpr const D PI = std::acos(D(-1));

    template<typename D>
    inline Complex<D> omega(int k, int n) {
        return Complex<D>(std::cos(D(k) * 2 * PI<D> / n), std::sin(D(k) * 2 * PI<D> / n));
    }

    inline int revbit(int mask, int bitlen) {
        int res = 0;
        while(bitlen--) {
            res = (res << 1) | (mask & 1);
            mask >>= 1;
        }
        return res;
    }

    template<typename D>
    void fft(std::vector<Complex<D>>& a, int bitlen) {
        int n = (int)a.size();
        int len = n;
        while(len > 1) {
            for(int i = 0; i < n; i += len) {
                int t = len >> 1;
                for(int j = 0; j < t; j++) {
                    int p = i + j;
                    auto l = a[p];
                    auto r = a[p + t];
                    a[p] = l + r;
                    a[p + t] = (l - r) * omega<D>(j, len);
                }
            }
            len >>= 1;
        }
        for(int i = 0; i < n; i++) {
            int j = revbit(i, bitlen);
            if(i < j) std::swap(a[i], a[j]);
        }
    }

    template<typename D>
    void ifft(std::vector<Complex<D>>& a, int bitlen) {
        int n = (int)a.size();
        for(int i = 0; i < n; i++) {
            int j = revbit(i, bitlen);
            if(i < j) std::swap(a[i], a[j]);
        }
        int len = 2;
        while(len <= n) {
            for(int i = 0; i < n; i += len) {
                int t = len >> 1;
                for(int j = 0; j < t; j++) {
                    int p = i + j;
                    auto l = a[p];
                    auto r = a[p + t] * omega<D>(-j, len);
                    a[p] = l + r;
                    a[p + t] = l - r;
                }
            }
            len <<= 1;
        }
        for(int i = 0; i < n; i++) a[i] /= Complex<D>(n, 0);
    }

    template<typename D>
    std::vector<D> convolution(const std::vector<D>& a, const std::vector<D>& b) {
        int m = (int)a.size() + (int)b.size() - 1;
        int n = 1, bitlen = 0;
        while(n < m) {
            n <<= 1;
            bitlen++;
        }
        std::vector<Complex<D>> A(n), B(n);
        for(int i = 0; i < (int)a.size(); i++) A[i] = Complex<D>(a[i], 0);
        for(int i = 0; i < (int)b.size(); i++) B[i] = Complex<D>(b[i], 0);
        fft<D>(A, bitlen);
        fft<D>(B, bitlen);
        for(int i = 0; i < n; i++) A[i] *= B[i];
        ifft<D>(A, bitlen);
        std::vector<D> res(m);
        for(int i = 0; i < m; i++) res[i] = A[i].x;
        return res;
    } 
}; // namespace fft
#line 78 "combined.cpp"

int main() {
    int L, M, N;
    cin >> L >> M >> N;
    vector<double> fa(N+1, 0), fb(N+1, 0);
    REP(i, L) {
        int a;
        cin >> a;
        fa[a] += 1;
    }
    REP(i, M) {
        int b;
        cin >> b;
        fb[N - b] += 1;
    }
    auto c = fft::convolution<double>(fa, fb);
    int Q;
    cin >> Q;
    REP(v, Q) cout << (ll)(c[N + v] + 0.5) << '\n';
}
0