結果

問題 No.732 3PrimeCounting
ユーザー fumofumofunifumofumofuni
提出日時 2022-08-19 00:42:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 680 ms / 3,000 ms
コード長 5,297 bytes
コンパイル時間 2,269 ms
コンパイル使用メモリ 220,328 KB
実行使用メモリ 41,848 KB
最終ジャッジ日時 2024-10-06 23:17:37
合計ジャッジ時間 60,021 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 680 ms
41,640 KB
testcase_01 AC 677 ms
41,596 KB
testcase_02 AC 604 ms
41,700 KB
testcase_03 AC 608 ms
41,716 KB
testcase_04 AC 603 ms
41,612 KB
testcase_05 AC 607 ms
41,644 KB
testcase_06 AC 608 ms
41,836 KB
testcase_07 AC 604 ms
41,720 KB
testcase_08 AC 626 ms
41,660 KB
testcase_09 AC 626 ms
41,720 KB
testcase_10 AC 614 ms
41,784 KB
testcase_11 AC 610 ms
41,668 KB
testcase_12 AC 615 ms
41,664 KB
testcase_13 AC 608 ms
41,648 KB
testcase_14 AC 617 ms
41,720 KB
testcase_15 AC 608 ms
41,736 KB
testcase_16 AC 611 ms
41,688 KB
testcase_17 AC 616 ms
41,492 KB
testcase_18 AC 615 ms
41,668 KB
testcase_19 AC 609 ms
41,724 KB
testcase_20 AC 623 ms
41,716 KB
testcase_21 AC 602 ms
41,716 KB
testcase_22 AC 600 ms
41,668 KB
testcase_23 AC 597 ms
41,700 KB
testcase_24 AC 616 ms
41,716 KB
testcase_25 AC 609 ms
41,724 KB
testcase_26 AC 626 ms
41,724 KB
testcase_27 AC 605 ms
41,696 KB
testcase_28 AC 596 ms
41,684 KB
testcase_29 AC 611 ms
41,652 KB
testcase_30 AC 598 ms
41,664 KB
testcase_31 AC 591 ms
41,724 KB
testcase_32 AC 620 ms
41,656 KB
testcase_33 AC 600 ms
41,516 KB
testcase_34 AC 612 ms
41,844 KB
testcase_35 AC 619 ms
41,696 KB
testcase_36 AC 615 ms
41,612 KB
testcase_37 AC 612 ms
41,664 KB
testcase_38 AC 606 ms
41,744 KB
testcase_39 AC 609 ms
41,724 KB
testcase_40 AC 621 ms
41,636 KB
testcase_41 AC 620 ms
41,480 KB
testcase_42 AC 624 ms
41,720 KB
testcase_43 AC 616 ms
41,736 KB
testcase_44 AC 611 ms
41,720 KB
testcase_45 AC 600 ms
41,596 KB
testcase_46 AC 601 ms
41,784 KB
testcase_47 AC 596 ms
41,720 KB
testcase_48 AC 628 ms
41,668 KB
testcase_49 AC 615 ms
41,620 KB
testcase_50 AC 619 ms
41,724 KB
testcase_51 AC 614 ms
41,720 KB
testcase_52 AC 609 ms
41,720 KB
testcase_53 AC 601 ms
41,724 KB
testcase_54 AC 605 ms
41,652 KB
testcase_55 AC 604 ms
41,720 KB
testcase_56 AC 617 ms
41,720 KB
testcase_57 AC 609 ms
41,696 KB
testcase_58 AC 657 ms
41,720 KB
testcase_59 AC 622 ms
41,720 KB
testcase_60 AC 611 ms
41,616 KB
testcase_61 AC 619 ms
41,688 KB
testcase_62 AC 617 ms
41,624 KB
testcase_63 AC 625 ms
41,848 KB
testcase_64 AC 630 ms
41,668 KB
testcase_65 AC 626 ms
41,612 KB
testcase_66 AC 615 ms
41,716 KB
testcase_67 AC 618 ms
41,720 KB
testcase_68 AC 617 ms
41,808 KB
testcase_69 AC 620 ms
41,684 KB
testcase_70 AC 623 ms
41,776 KB
testcase_71 AC 620 ms
41,812 KB
testcase_72 AC 606 ms
41,716 KB
testcase_73 AC 603 ms
41,632 KB
testcase_74 AC 608 ms
41,776 KB
testcase_75 AC 593 ms
41,704 KB
testcase_76 AC 593 ms
41,688 KB
testcase_77 AC 598 ms
41,700 KB
testcase_78 AC 596 ms
41,724 KB
testcase_79 AC 604 ms
41,664 KB
testcase_80 AC 607 ms
41,640 KB
testcase_81 AC 610 ms
41,820 KB
testcase_82 AC 604 ms
41,608 KB
testcase_83 AC 598 ms
41,672 KB
testcase_84 AC 604 ms
41,632 KB
testcase_85 AC 595 ms
41,720 KB
testcase_86 AC 599 ms
41,724 KB
testcase_87 AC 630 ms
41,660 KB
testcase_88 AC 603 ms
41,720 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