結果

問題 No.206 数の積集合を求めるクエリ
ユーザー mugen_1337mugen_1337
提出日時 2021-04-09 08:56:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 7,000 ms
コード長 3,824 bytes
コンパイル時間 2,187 ms
コンパイル使用メモリ 210,640 KB
実行使用メモリ 23,580 KB
最終ジャッジ日時 2023-09-06 17:54:28
合計ジャッジ時間 4,861 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 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,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 4 ms
4,448 KB
testcase_07 AC 4 ms
4,384 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,384 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 4 ms
4,432 KB
testcase_17 AC 101 ms
23,360 KB
testcase_18 AC 91 ms
23,376 KB
testcase_19 AC 98 ms
23,384 KB
testcase_20 AC 91 ms
23,372 KB
testcase_21 AC 95 ms
23,364 KB
testcase_22 AC 97 ms
23,324 KB
testcase_23 AC 100 ms
23,336 KB
testcase_24 AC 114 ms
23,372 KB
testcase_25 AC 111 ms
23,380 KB
testcase_26 AC 100 ms
23,580 KB
testcase_27 AC 98 ms
23,500 KB
testcase_28 AC 104 ms
23,356 KB
testcase_29 AC 100 ms
23,360 KB
testcase_30 AC 96 ms
23,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

struct FastFourierTransform{
    using Real = long double;
    using C=complex<Real>;
    const Real PI=acosl(-1);
    int base;
    vector<C> rts;
    vector<int> rev;

    FastFourierTransform():base(1){
        rts.emplace_back(0,0);
        rts.emplace_back(1,0);
        rev.push_back(0);
        rev.push_back(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;
                }
            }
        }
    }
    
    // C[k] = sum_i A[i] * B[k-i]
    vector<long long> 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]-conj(fa[i]*fa[i]))*r;
            fa[j]=(fa[i]*fa[i]-conj(fa[j]*fa[j]))*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<long long> ret(need);
        for(int i=0;i<need;i++) ret[i]=llround(i&1?fa[i>>1].imag():fa[i>>1].real());
        return ret;
    }
};


signed main(){
    int N,M,S;
    cin>>N>>M>>S;
    vector<int> A(S+1),B(S+1);
    rep(i,N){
        int p;cin>>p;
        A[p]++;
    }
    rep(j,M){
        int p;cin>>p;
        B[p]++;
    }
    reverse(ALL(B));

    FastFourierTransform F;
    auto C=F.multiply(A,B);

    int Q;cin>>Q;

    rep(i,Q) cout<<C[S+i]<<"\n";
    return 0;
}
0