結果
| 問題 |
No.1661 Sum is Prime (Hard Version)
|
| コンテスト | |
| ユーザー |
tko919
|
| 提出日時 | 2021-08-27 21:26:27 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 181 ms / 3,000 ms |
| コード長 | 2,299 bytes |
| コンパイル時間 | 2,181 ms |
| コンパイル使用メモリ | 201,200 KB |
| 最終ジャッジ日時 | 2025-01-24 02:28:46 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 22 |
ソースコード
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
bool Miller(ll n){
if(n<2 or (n&1)==0)return (n==2);
ll d=n-1; while((d&1)==0)d>>=1;
vector<ll> seeds;
auto MP=[&](ll x,ll t,ll m)->ll{
ll res=1;
while(t){
if(t&1)res=(__int128_t(res)*x)%m;
x=(__int128_t(x)*x)%m; t>>=1;
} return res;
};
if(n<(1<<30))seeds={2, 7, 61};
else seeds={2, 325, 9375, 28178, 450775, 9780504};
for(auto& x:seeds){
if(n<=x)break;
ll t=d,y=MP(x,t,n);
while(t!=n-1 and y!=1 and y!=n-1){
y=(__int128_t(y)*y)%n; t<<=1;
}
if(y!=n-1 and (t&1)==0)return 0;
} return 1;
}
struct PrimeCount{
const ll N; const ll M;
vector<int> lo; vector<ll> hi;
PrimeCount(const ll& _N):N(_N),M(int(sqrt(N))),lo(M+1),hi(M+1){
rep(i,1,M+1)lo[i]=i-1,hi[i]=N/i-1;
int cnt=0;
for(int p=2;p<=M;p++){
if(lo[p-1]==lo[p])continue;
const ll n=N/p;
const ll q=ll(p)*p;
const int w=M/p;
const ll L=min(M,N/q);
for(int i=1;i<=w;i++)hi[i]-=hi[i*p]-cnt;
const int t=min<int>(sqrt(n),L);
for(int i=w+1;i<=t;i++)hi[i]-=lo[n/i]-cnt;
for(int i=L,j=n/L;i>t;j++){
int c=lo[j];
while(j+1<=M and lo[j+1]==c)j++;
c-=cnt;
for(int e=max<int>(t,n/(j+1));i>e;i--)hi[i]-=c;
}
for(int i=M,j=M/p;j>=p;j--){
const int c=lo[j]-cnt;
for(int e=j*p;i>=e;i--)lo[i]-=c;
}
cnt++;
}
}
ll pi(ll x){
if(x<=M)return lo[x];
else return hi[N/x];
}
};
int main(){
ll L,R;
cin>>L>>R;
PrimeCount Ls(L-1),Rs(R),L2s(L*2),R2s(R*2);
ll res=Rs.pi(R)-Ls.pi(L-1)+R2s.pi(R*2)-L2s.pi(L*2);
cout<<res<<'\n';
return 0;
rep(a,L,R+1){
if(Miller(a))res++;
if(a!=R and Miller(a*2+1))res++;
}
cout<<res<<'\n';
return 0;
}
tko919