結果

問題 No.1307 Rotate and Accumulate
ユーザー mugen_1337mugen_1337
提出日時 2021-05-16 20:27:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 5,000 ms
コード長 3,697 bytes
コンパイル時間 2,567 ms
コンパイル使用メモリ 211,136 KB
実行使用メモリ 25,372 KB
最終ジャッジ日時 2024-04-15 11:18:46
合計ジャッジ時間 5,411 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,948 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 47 ms
15,052 KB
testcase_09 AC 49 ms
15,164 KB
testcase_10 AC 52 ms
14,288 KB
testcase_11 AC 42 ms
14,836 KB
testcase_12 AC 51 ms
14,252 KB
testcase_13 AC 13 ms
6,944 KB
testcase_14 AC 28 ms
8,832 KB
testcase_15 AC 92 ms
25,280 KB
testcase_16 AC 92 ms
25,372 KB
testcase_17 AC 92 ms
25,320 KB
testcase_18 AC 84 ms
25,320 KB
testcase_19 AC 92 ms
25,360 KB
testcase_20 AC 92 ms
25,248 KB
testcase_21 AC 2 ms
6,944 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=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;
    }
};

FastFourierTransform F;

signed main(){
    int N,Q;cin>>N>>Q;
    vector<int> A(N),B(N,0);
    cin>>A;
    rep(i,N) A.push_back(A[i]);
    rep(i,Q){
        int r;cin>>r;
        B[(N-r)%N]++;
    }

    auto C=F.multiply(A,B);
    vector<ll> res;
    rep(i,N) res.push_back(C[i+N]);
    cout<<res<<endl;
    return 0;
}
0