結果

問題 No.732 3PrimeCounting
ユーザー fumofumofunifumofumofuni
提出日時 2022-08-19 00:42:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 684 ms / 3,000 ms
コード長 5,297 bytes
コンパイル時間 2,811 ms
コンパイル使用メモリ 214,432 KB
実行使用メモリ 41,940 KB
最終ジャッジ日時 2024-04-16 08:12:46
合計ジャッジ時間 66,028 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 668 ms
41,616 KB
testcase_01 AC 672 ms
41,632 KB
testcase_02 AC 672 ms
41,592 KB
testcase_03 AC 672 ms
41,616 KB
testcase_04 AC 669 ms
41,720 KB
testcase_05 AC 670 ms
41,756 KB
testcase_06 AC 664 ms
41,592 KB
testcase_07 AC 673 ms
41,604 KB
testcase_08 AC 671 ms
41,664 KB
testcase_09 AC 669 ms
41,620 KB
testcase_10 AC 667 ms
41,612 KB
testcase_11 AC 671 ms
41,600 KB
testcase_12 AC 666 ms
41,928 KB
testcase_13 AC 672 ms
41,804 KB
testcase_14 AC 674 ms
41,596 KB
testcase_15 AC 670 ms
41,636 KB
testcase_16 AC 663 ms
41,604 KB
testcase_17 AC 663 ms
41,628 KB
testcase_18 AC 670 ms
41,600 KB
testcase_19 AC 671 ms
41,696 KB
testcase_20 AC 671 ms
41,636 KB
testcase_21 AC 666 ms
41,688 KB
testcase_22 AC 670 ms
41,700 KB
testcase_23 AC 671 ms
41,596 KB
testcase_24 AC 676 ms
41,940 KB
testcase_25 AC 673 ms
41,696 KB
testcase_26 AC 673 ms
41,748 KB
testcase_27 AC 675 ms
41,756 KB
testcase_28 AC 679 ms
41,688 KB
testcase_29 AC 678 ms
41,692 KB
testcase_30 AC 674 ms
41,600 KB
testcase_31 AC 676 ms
41,676 KB
testcase_32 AC 671 ms
41,604 KB
testcase_33 AC 678 ms
41,672 KB
testcase_34 AC 675 ms
41,640 KB
testcase_35 AC 675 ms
41,628 KB
testcase_36 AC 676 ms
41,692 KB
testcase_37 AC 678 ms
41,684 KB
testcase_38 AC 679 ms
41,668 KB
testcase_39 AC 676 ms
41,612 KB
testcase_40 AC 680 ms
41,664 KB
testcase_41 AC 678 ms
41,604 KB
testcase_42 AC 675 ms
41,724 KB
testcase_43 AC 677 ms
41,612 KB
testcase_44 AC 675 ms
41,620 KB
testcase_45 AC 678 ms
41,644 KB
testcase_46 AC 684 ms
41,904 KB
testcase_47 AC 683 ms
41,692 KB
testcase_48 AC 673 ms
41,720 KB
testcase_49 AC 670 ms
41,600 KB
testcase_50 AC 670 ms
41,628 KB
testcase_51 AC 674 ms
41,616 KB
testcase_52 AC 673 ms
41,596 KB
testcase_53 AC 674 ms
41,616 KB
testcase_54 AC 670 ms
41,640 KB
testcase_55 AC 672 ms
41,636 KB
testcase_56 AC 669 ms
41,724 KB
testcase_57 AC 673 ms
41,616 KB
testcase_58 AC 677 ms
41,648 KB
testcase_59 AC 669 ms
41,724 KB
testcase_60 AC 677 ms
41,608 KB
testcase_61 AC 667 ms
41,632 KB
testcase_62 AC 663 ms
41,672 KB
testcase_63 AC 673 ms
41,800 KB
testcase_64 AC 666 ms
41,740 KB
testcase_65 AC 669 ms
41,688 KB
testcase_66 AC 669 ms
41,600 KB
testcase_67 AC 670 ms
41,648 KB
testcase_68 AC 672 ms
41,768 KB
testcase_69 AC 673 ms
41,688 KB
testcase_70 AC 667 ms
41,660 KB
testcase_71 AC 665 ms
41,704 KB
testcase_72 AC 669 ms
41,628 KB
testcase_73 AC 672 ms
41,516 KB
testcase_74 AC 672 ms
41,808 KB
testcase_75 AC 668 ms
41,656 KB
testcase_76 AC 669 ms
41,728 KB
testcase_77 AC 670 ms
41,788 KB
testcase_78 AC 665 ms
41,672 KB
testcase_79 AC 669 ms
41,700 KB
testcase_80 AC 670 ms
41,652 KB
testcase_81 AC 679 ms
41,700 KB
testcase_82 AC 668 ms
41,632 KB
testcase_83 AC 668 ms
41,632 KB
testcase_84 AC 682 ms
41,676 KB
testcase_85 AC 671 ms
41,736 KB
testcase_86 AC 662 ms
41,712 KB
testcase_87 AC 665 ms
41,676 KB
testcase_88 AC 664 ms
41,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,-1,0,1,1,1,-1,-1,0};
const ll dx[9]={-1,0,1,0,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

namespace NTT {
    //MOD9のNTT auto c=NTT::mul(a,b)で受け取り。
	std::vector<ll> tmp;
	size_t sz = 1;
 
	inline ll powMod(ll n, ll p, ll m) {
		ll res = 1;
		while (p) {
			if (p & 1) res = res * n % m;
			n = n * n % m;
			p >>= 1;
		}
		return res;
	}
	inline ll invMod(ll n, ll m) {
		return powMod(n, m - 2, m);
	}

    ll extGcd(ll a, ll b, ll &p, ll &q) {  
        if (b == 0) { p = 1; q = 0; return a; }  
        ll d = extGcd(b, a%b, q, p);  
        q -= a/b * p;  
        return d;  
    }

    pair<ll, ll> ChineseRem(const vector<ll> &b, const vector<ll> &m) {
        ll r = 0, M = 1;
        for (int i = 0; i < (int)b.size(); ++i) {
            ll p, q;
            ll d = extGcd(M, m[i], p, q); // p is inv of M/d (mod. m[i]/d)
            if ((b[i] - r) % d != 0) return make_pair(0, -1);
            ll tmp = (b[i] - r) / d * p % (m[i]/d);
            r += M * tmp;
            M *= m[i]/d;
        }
        return make_pair((r+M+M)%M, M);
    }
 
	template <ll Mod, ll PrimitiveRoot>
	struct NTTPart {
		static std::vector<ll> ntt(std::vector<ll> a, bool inv = false) {
			size_t mask = sz - 1;
			size_t p = 0;
			for (size_t i = sz >> 1; i >= 1; i >>= 1) {
				auto& cur = (p & 1) ? tmp : a;
				auto& nex = (p & 1) ? a : tmp;
				ll e = powMod(PrimitiveRoot, (Mod - 1) / sz * i, Mod);
				if (inv) e = invMod(e, Mod);
				ll w = 1;
				for (size_t j = 0; j < sz; j += i) {
					for (size_t k = 0; k < i; ++k) {
						nex[j + k] = (cur[((j << 1) & mask) + k] + w * cur[(((j << 1) + i) & mask) + k]) % Mod;
					}
					w = w * e % Mod;
				}
				++p;
			}
			if (p & 1) std::swap(a, tmp);
			if (inv) {
				ll invSz = invMod(sz, Mod);
				for (size_t i = 0; i < sz; ++i) a[i] = a[i] * invSz % Mod;
			}
			return a;
		}
		static std::vector<ll> mul(std::vector<ll> a, std::vector<ll> b) {
			a = ntt(a);
			b = ntt(b);
			for (size_t i = 0; i < sz; ++i) a[i] = a[i] * b[i] % Mod;
			a = ntt(a, true);
			return a;
		}
	};
	std::vector<ll> mul(std::vector<ll> a, std::vector<ll> b) {
		size_t m = a.size() + b.size() - 1;
		sz = 1;
		while (m > sz) sz <<= 1;
		tmp.resize(sz);
		a.resize(sz, 0);
		b.resize(sz, 0);
		vector<ll> c=NTTPart<998244353,3>::mul(a, b);
		c.resize(m);
		return c;
	}
    std::vector<ll> mul_ll(std::vector<ll> a, std::vector<ll> b) {
		size_t m = a.size() + b.size() - 1;
		sz = 1;
		while (m > sz) sz <<= 1;
		tmp.resize(sz);
		a.resize(sz, 0);
		b.resize(sz, 0);
		vector<ll> c=NTTPart<998244353,3>::mul(a, b);
        vector<ll> d=NTTPart<1224736769,3>::mul(a, b);
		c.resize(m);d.resize(m);
        vector<ll> e(m);
        rep(i,m)e[i]=ChineseRem({c[i],d[i]},{998244353,1224736769}).first;
		return e;
	}
};


struct osak{
    vector<long long> lpf;// least prime factor
    vector<long long> prime;// prime table
    osak(long long n){//linear_sieve
        lpf=vector<long long>(n+1,-1);
        for (int d = 2; d <= n; ++d) {
          if(lpf[d]==-1){
            lpf[d]=d;prime.emplace_back(d);
          }
          for(auto p:prime){
            if(p*d>n||p>lpf[d])break;
            lpf[p*d]=p;
          }
        }
    }
    map<long long,long long> factor(int n) {
        map<long long,long long> factor;
        while (n > 1) {
            factor[lpf[n]]++;
            n /= lpf[n];
        }
        return factor;
    }

    vector<long long> divisor(int N){//O(div.size())
        map<long long,long long> facs=factor(N);
        vector<long long> ret={1};
        for(auto p:facs){
            ll range=ret.size();
            ll now=1;
            for(int i=0;i<p.se;i++){
                now*=p.fi;
                for(int j=0;j<range;j++){
                    ret.emplace_back(ret[j]*now);
                }
            }
        }
        sort(ret.begin(),ret.end());
        return ret;
    }
};

int main(){
    osak os(300010);
    ll n;cin >> n;
    vl v(100010);
    for(auto p:os.prime){
        if(p<=n)v[p]++;
    }
    auto trp=NTT::mul_ll(NTT::mul_ll(v,v),v);
    ll ans=0;
    for(auto p:os.prime){
        ans+=trp[p];
    }
    //cout << ans << endl;
    vl db(200010);
    for(auto p:os.prime){
        if(p<=n)db[p*2]++;
    }
    auto sec=NTT::mul_ll(db,v);
    for(auto p:os.prime){
        ans-=sec[p]*3;
    }
    cout << ans/6 << endl;
}
0