結果
問題 | No.732 3PrimeCounting |
ユーザー |
|
提出日時 | 2022-08-19 00:42:30 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 935 ms / 3,000 ms |
コード長 | 5,297 bytes |
コンパイル時間 | 2,492 ms |
コンパイル使用メモリ | 213,060 KB |
最終ジャッジ日時 | 2025-01-31 00:02:12 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 89 |
ソースコード
#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; }