結果

問題 No.206 数の積集合を求めるクエリ
ユーザー ctyl_0ctyl_0
提出日時 2015-09-14 00:07:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 394 ms / 7,000 ms
コード長 2,754 bytes
コンパイル時間 1,086 ms
コンパイル使用メモリ 90,640 KB
実行使用メモリ 37,368 KB
最終ジャッジ日時 2023-09-26 11:48:16
合計ジャッジ時間 9,061 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 10 ms
4,668 KB
testcase_07 AC 11 ms
4,380 KB
testcase_08 AC 11 ms
4,380 KB
testcase_09 AC 11 ms
4,564 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 11 ms
4,500 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 11 ms
4,564 KB
testcase_15 AC 10 ms
4,384 KB
testcase_16 AC 11 ms
4,552 KB
testcase_17 AC 379 ms
37,032 KB
testcase_18 AC 354 ms
36,968 KB
testcase_19 AC 379 ms
37,164 KB
testcase_20 AC 357 ms
37,248 KB
testcase_21 AC 360 ms
37,200 KB
testcase_22 AC 360 ms
37,008 KB
testcase_23 AC 382 ms
37,132 KB
testcase_24 AC 394 ms
37,096 KB
testcase_25 AC 382 ms
37,204 KB
testcase_26 AC 363 ms
37,208 KB
testcase_27 AC 352 ms
37,324 KB
testcase_28 AC 368 ms
36,976 KB
testcase_29 AC 368 ms
37,368 KB
testcase_30 AC 357 ms
37,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <complex>

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)

using namespace std;
typedef long long ll;
typedef complex<double> cmpd;


int inputValue(){
    int a;
    cin >> a;
    return a;
};

void inputArray(int * p, int a){
    rep(i, a){
        cin >> p[i];
    }
};

void inputVector(vector<int> * p, int a){
    rep(i, a){
        int input;
        cin >> input;
        p -> push_back(input);
    }
}

template <typename T>
void output(T a, int precision) {
    if(precision > 0){
        cout << setprecision(precision)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}

// 高速フーリエ変換
vector<cmpd> dft(vector<cmpd> & f, int deg, bool dir){
    // 終了条件
    if (deg == 1) {
        return f;
    }
    
    // 半分の長さのf0, f1をつくる
    vector<cmpd> f0(deg >> 1), f1(deg >> 1);
    
    // f0: 奇数番目, f1: 偶数番目
    rep(i, deg >> 1){
        f0[i] = f[i << 1];
        f1[i] = f[i << 1 | 1];
    }
    
    // それぞれについてdft
    f0 = dft(f0 , deg >> 1, dir);
    f1 = dft(f1 , deg >> 1, dir);
    
    // dir: 順変換ならtrue, 逆変換ならfalse
    cmpd zeta = (dir == true) ? polar<double>(1, 2 * M_PI / deg) : polar<double>(1, -2 * M_PI / deg);
    
    cmpd pow_zeta(1);
    
    rep(i, deg){
        f[i] = f0[i % (deg >> 1)] + pow_zeta * f1[i % (deg >> 1)];
        pow_zeta *= zeta;
    }
    return f;
}

vector<cmpd> idft(vector<cmpd> & f, int deg){
    // fのサイズは整形されている
    vector<cmpd> ret = dft(f, deg, false);
    rep(i, deg){
        ret[i] /= deg;
    }
    return ret;
}

// 多項式乗算
vector<cmpd> multipoly (vector<cmpd> & a, vector<cmpd> & b){
    
    // 多項式を2^n次に整形.係数が0のところは0詰め
    int nmax = (int)a.size() + (int)b.size() - 1;
    int n = 1;
    while(n < nmax) n <<= 1;
    a.resize(n);
    b.resize(n);
    
    vector<cmpd> fc(n);
    vector<cmpd> fa = dft(a, n, true);
    vector<cmpd> fb = dft(b, n, true);
    rep(i, n){
        fc[i] = fa[i] * fb[i];
    }
    return idft(fc, n);
}

int main(int argc, const char * argv[]) {
    
    // source code
    int L = inputValue();
    int M = inputValue();
    int N = inputValue();
    N++;
    vector<cmpd> A(N);
    rep(i, L){
        A[inputValue()] = 1;
    }
    vector<cmpd> B(N);
    rep(i, M){
        B[N - inputValue()] = 1;
    }
    int Q = inputValue();
    
    auto C = multipoly(A, B);
    
    repd(i, N, N + Q){
        output((int)(C[i].real() + 0.5), 0);
    }
    
    
    
    return 0;
}
0