結果

問題 No.732 3PrimeCounting
ユーザー mugen_1337mugen_1337
提出日時 2020-06-30 21:20:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 363 ms / 3,000 ms
コード長 4,630 bytes
コンパイル時間 2,383 ms
コンパイル使用メモリ 210,004 KB
実行使用メモリ 45,556 KB
最終ジャッジ日時 2023-10-11 11:38:52
合計ジャッジ時間 11,199 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 2 ms
4,368 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 2 ms
4,368 KB
testcase_10 AC 1 ms
4,368 KB
testcase_11 AC 1 ms
4,372 KB
testcase_12 AC 2 ms
4,368 KB
testcase_13 AC 1 ms
4,372 KB
testcase_14 AC 2 ms
4,368 KB
testcase_15 AC 2 ms
4,372 KB
testcase_16 AC 2 ms
4,368 KB
testcase_17 AC 2 ms
4,372 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 1 ms
4,372 KB
testcase_20 AC 2 ms
4,368 KB
testcase_21 AC 5 ms
4,368 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 2 ms
4,372 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 10 ms
5,012 KB
testcase_26 AC 8 ms
5,060 KB
testcase_27 AC 3 ms
4,368 KB
testcase_28 AC 3 ms
4,368 KB
testcase_29 AC 5 ms
4,372 KB
testcase_30 AC 5 ms
4,376 KB
testcase_31 AC 5 ms
4,372 KB
testcase_32 AC 9 ms
4,948 KB
testcase_33 AC 8 ms
5,008 KB
testcase_34 AC 8 ms
4,904 KB
testcase_35 AC 8 ms
4,824 KB
testcase_36 AC 9 ms
4,864 KB
testcase_37 AC 3 ms
4,368 KB
testcase_38 AC 2 ms
4,372 KB
testcase_39 AC 9 ms
4,980 KB
testcase_40 AC 6 ms
4,368 KB
testcase_41 AC 6 ms
4,472 KB
testcase_42 AC 6 ms
4,372 KB
testcase_43 AC 6 ms
4,372 KB
testcase_44 AC 5 ms
4,376 KB
testcase_45 AC 3 ms
4,372 KB
testcase_46 AC 4 ms
4,372 KB
testcase_47 AC 5 ms
4,372 KB
testcase_48 AC 11 ms
4,812 KB
testcase_49 AC 10 ms
4,976 KB
testcase_50 AC 6 ms
4,372 KB
testcase_51 AC 5 ms
4,368 KB
testcase_52 AC 4 ms
4,376 KB
testcase_53 AC 20 ms
6,440 KB
testcase_54 AC 153 ms
24,764 KB
testcase_55 AC 152 ms
24,792 KB
testcase_56 AC 151 ms
24,760 KB
testcase_57 AC 43 ms
9,400 KB
testcase_58 AC 43 ms
9,564 KB
testcase_59 AC 20 ms
6,532 KB
testcase_60 AC 68 ms
14,132 KB
testcase_61 AC 68 ms
14,128 KB
testcase_62 AC 165 ms
25,416 KB
testcase_63 AC 89 ms
15,164 KB
testcase_64 AC 72 ms
14,372 KB
testcase_65 AC 71 ms
14,324 KB
testcase_66 AC 2 ms
4,368 KB
testcase_67 AC 2 ms
4,372 KB
testcase_68 AC 158 ms
25,184 KB
testcase_69 AC 157 ms
25,348 KB
testcase_70 AC 152 ms
24,772 KB
testcase_71 AC 152 ms
24,808 KB
testcase_72 AC 88 ms
15,104 KB
testcase_73 AC 221 ms
27,104 KB
testcase_74 AC 223 ms
27,100 KB
testcase_75 AC 11 ms
4,940 KB
testcase_76 AC 156 ms
25,032 KB
testcase_77 AC 43 ms
9,512 KB
testcase_78 AC 204 ms
26,684 KB
testcase_79 AC 160 ms
25,200 KB
testcase_80 AC 198 ms
26,308 KB
testcase_81 AC 153 ms
24,948 KB
testcase_82 AC 3 ms
4,380 KB
testcase_83 AC 43 ms
9,472 KB
testcase_84 AC 65 ms
13,732 KB
testcase_85 AC 146 ms
24,720 KB
testcase_86 AC 197 ms
26,084 KB
testcase_87 AC 363 ms
45,524 KB
testcase_88 AC 344 ms
45,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#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 INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int 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;}

namespace FastFourierTransform{
  using real = long double;
 
    // 複素数型
    struct C{
        real x, y;
        C() : x(0), y(0) {}
        C(real x, real y) : x(x), y(y) {}
        inline C operator+(const C &c)const{ return C(x+c.x, y+c.y);}
        inline C operator-(const C &c)const{ return C(x-c.x, y-c.y);}
        inline C operator*(const C &c)const{ return C(x*c.x - y*c.y, x*c.y + y*c.x);}
        inline C conj() const{return C(x, -y);}
    };
 
    const real PI=acosl(-1);
    int base=1;
    vector<C> rts={{0, 0},
                   {1, 0}};
    vector<int> rev={0, 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);// nは2べきか
        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]);
        }
        //k:ブロックサイズの半分,nになったら終わりここがO(logn)
        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];//rtsは掛けるときの重み?
                    a[i+j+k]=a[i+j]-z;//更新
                    a[i+j]=a[i+j]+z;
                }
            }
        }
    }
 
    // 畳み込み,返り値のvectorのサイズはa.size+b.size()-1
    vector<long long> multiply(const vector< int > &a, const vector< int > &b) {
        int need=(int)a.size()+(int)b.size()-1;
        int nbase=1;// fftするために2ベキのサイズに変更
        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]-(fa[i]*fa[i]).conj())*r;// conjは共役複素数を求める
            fa[j]=(fa[i]*fa[i]-(fa[j]*fa[j]).conj())*r;// c++の複素数型complexとかにもあるやつ
            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].y : fa[i>>1].x);
        }
        return ret;
    }
};

vector<int> sieve(int n){
    vector<int> ret(n+1,1);
    ret[0]=0;
    if(n>0) ret[1]=0;
    for(int i=2;i*i<=n;i++){
        if(!ret[i]) continue;
        for(int j=i*2;j<=n;j+=i) ret[j]=0;
    }
    return ret;
}


signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int n;cin>>n;
    vector<int> p=sieve(3*n+10);
    vector<int> pp(n+1,0);
    rep(i,n+1)if(p[i])pp[i]=1;
    auto v2=FastFourierTransform::multiply(pp,pp);
    vector<int> v2i;
    for(auto x:v2)v2i.push_back(x);
    auto v=FastFourierTransform::multiply(v2i,pp);

    vector<int> ps;
    rep(i,n+1)if(p[i])ps.push_back(i);

    for(auto x:ps){
        for(auto y:ps){
            v[2*x+y]-=3;
        }
    }
    ll ans=0;
    rep(i,3*n+1)if(p[i])ans+=v[i]/6;
    cout<<ans<<endl;
    return 0;
}
0