結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー 👑 PCTprobabilityPCTprobability
提出日時 2021-08-27 21:29:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 7,085 bytes
コンパイル時間 6,578 ms
コンパイル使用メモリ 305,532 KB
実行使用メモリ 85,664 KB
最終ジャッジ日時 2023-08-13 07:57:08
合計ジャッジ時間 10,708 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
85,592 KB
testcase_01 AC 122 ms
85,416 KB
testcase_02 AC 126 ms
85,468 KB
testcase_03 AC 125 ms
85,408 KB
testcase_04 AC 126 ms
85,452 KB
testcase_05 AC 126 ms
85,592 KB
testcase_06 AC 125 ms
85,520 KB
testcase_07 AC 125 ms
85,420 KB
testcase_08 AC 129 ms
85,424 KB
testcase_09 AC 125 ms
85,500 KB
testcase_10 AC 126 ms
85,524 KB
testcase_11 AC 120 ms
85,416 KB
testcase_12 AC 120 ms
85,400 KB
testcase_13 AC 119 ms
85,524 KB
testcase_14 AC 118 ms
85,584 KB
testcase_15 AC 120 ms
85,396 KB
testcase_16 AC 120 ms
85,420 KB
testcase_17 AC 125 ms
85,464 KB
testcase_18 AC 121 ms
85,412 KB
testcase_19 AC 120 ms
85,664 KB
testcase_20 AC 120 ms
85,452 KB
testcase_21 AC 130 ms
85,408 KB
testcase_22 AC 125 ms
85,452 KB
testcase_23 AC 123 ms
85,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define endl "\n"
typedef pair<int, int> Pii;
#define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i))
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(x) begin(x), end(x)
#define all(s) (s).begin(),(s).end()
//#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
//#define rep(i, n) rep2(i, 0, n)
#define PB push_back 
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define rever(vec) reverse(vec.begin(), vec.end())
#define sor(vec) sort(vec.begin(), vec.end())
//#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define fi first
#define se second
#define pb push_back
#define P pair<ll,ll>
#define NP next_permutation
//const ll mod = 1000000009;
//const ll mod = 998244353;
const ll mod = 1000000007;
const ll inf = 4100000000000000000ll;
const ld eps = ld(0.00000000001);
//static const long double pi = 3.141592653589793;
template<class T>void vcin(vector<T> &n){for(int i=0;i<int(n.size());i++) cin>>n[i];}
template<class T,class K>void vcin(vector<T> &n,vector<K> &m){for(int i=0;i<int(n.size());i++) cin>>n[i]>>m[i];}
template<class T>void vcout(vector<T> &n){for(int i=0;i<int(n.size());i++){cout<<n[i]<<" ";}cout<<endl;}
template<class T>void vcin(vector<vector<T>> &n){for(int i=0;i<int(n.size());i++){for(int j=0;j<int(n[i].size());j++){cin>>n[i][j];}}}
template<class T>void vcout(vector<vector<T>> &n){for(int i=0;i<int(n.size());i++){for(int j=0;j<int(n[i].size());j++){cout<<n[i][j]<<" ";}cout<<endl;}cout<<endl;}
void yes(bool a){cout<<(a?"yes":"no")<<endl;}
void YES(bool a){cout<<(a?"YES":"NO")<<endl;}
void Yes(bool a){cout<<(a?"Yes":"No")<<endl;}
void possible(bool a){ cout<<(a?"possible":"impossible")<<endl; }
void Possible(bool a){ cout<<(a?"Possible":"Impossible")<<endl; }
void POSSIBLE(bool a){ cout<<(a?"POSSIBLE":"IMPOSSIBLE")<<endl; }
template<class T>auto min(const T& a){ return *min_element(all(a)); }
template<class T>auto max(const T& a){ return *max_element(all(a)); }
template<class T,class F>void print(pair<T,F> a){cout<<a.fi<<" "<<a.se<<endl;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0;}
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0;}
template<class T> void ifmin(T t,T u){if(t>u){cout<<-1<<endl;}else{cout<<t<<endl;}}
template<class T> void ifmax(T t,T u){if(t>u){cout<<-1<<endl;}else{cout<<t<<endl;}}
template<typename T,typename ...Args>auto make_vector(T x,int arg,Args ...args){if constexpr(sizeof...(args)==0)return vector<T>(arg,x);else return vector(arg,make_vector<T>(x,args...));}
ll fastgcd(ll u,ll v){ll shl=0;while(u&&v&&u!=v){bool eu=!(u&1);bool ev=!(v&1);if(eu&&ev){++shl;u>>=1;v>>=1;}else if(eu&&!ev){u>>=1;}else if(!eu&&ev){v>>=1;}else if(u>=v){u=(u-v)>>1;}else{ll tmp=u;u=(v-u)>>1;v=tmp;}}return !u?v<<shl:u<<shl;}
ll modPow(ll a, ll n, ll mod) { if(mod==1) return 0;ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }
vector<ll> divisor(ll x){ vector<ll> ans; for(ll i = 1; i * i <= x; i++){ if(x % i == 0) {ans.push_back(i); if(i*i!=x){ ans.push_back(x / ans[i]);}}}sor(ans); return ans; }
ll pop(ll x){return __builtin_popcountll(x);}
ll poplong(ll x){ll y=-1;while(x){x/=2;y++;}return y;}
template<class T>
struct Sum{
  vector<T> data;
  Sum(const vector<T>& v):data(v.size()+1){
    for(ll i=0;i<v.size();i++) data[i+1]=data[i]+v[i];
  }
  T get(ll l,ll r) const {
    return data[r]-data[l];
  }
};
template<class T>
struct Sum2{
  vector<vector<T>> data;
  Sum2(const vector<vector<T>> &v):data(v.size()+1,vector<T>(v[0].size()+1)){
    for(int i=0;i<v.size();i++) for(int j=0;j<v[i].size();j++) data[i+1][j+1]=data[i][j+1]+v[i][j];
    for(int i=0;i<v.size();i++) for(int j=0;j<v[i].size();j++) data[i+1][j+1]+=data[i+1][j];
  }
  T get(ll x1,ll y1,ll x2,ll y2) const {
    return data[x2][y2]+data[x1][y1]-data[x1][y2]-data[x2][y1];
  }
};

void cincout(){
  ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
  cout<< fixed << setprecision(10);
}
const int N = 3e5 + 9;

namespace pcf {
	// initialize once by calling init()
	#define MAXN 10000010 // initial sieve limit
	#define MAX_PRIMES 1000010 // max size of the prime array for sieve
	#define PHI_N 100000
	#define PHI_K 100

	unsigned int ar[(MAXN >> 6) + 5] = {0};
	int len = 0; // total number of primes generated by sieve
	int primes[MAX_PRIMES];
	int counter[MAXN]; // counter[m] --> number of primes <= i
	int dp[PHI_N][PHI_K]; // precal of yo(n,k)
	bitset <MAXN> fl;

	void sieve(int n) {
	    fl[1] = true;
	    for (int i = 4; i <= n; i += 2) fl[i] = true;
	    for (int i = 3; i * i <= n; i += 2) {
	        if (!fl[i]) {
	            for (int j = i * i; j <= n; j += i << 1) fl[j] = 1;
	        }
	    }
	    for (int i = 1; i <= n; i++) {
	        if (!fl[i]) primes[len++] = i;
	        counter[i] = len;
	    }
	}
	void init() {
	    sieve(MAXN - 1);
	    // precalculation of phi upto size (PHI_N,PHI_K)
	    int k, n, res;
	    for (n = 0; n < PHI_N; n++) dp[n][0] = n;
	    for (k = 1; k < PHI_K; k++) {
	        for (n = 0; n < PHI_N; n++) {
	            dp[n][k] = dp[n][k - 1] - dp[n / primes[k - 1]][k - 1];
	        }
	    }
	}
	// returns number of integers less or equal n which are
	// not divisible by any of the first k primes
	// recurrence --> yo(n , k) = yo(n , k-1) - yo(n / p_k , k-1)
	// for sum of primes yo(n,k)=yo(n,k-1)-p_k*yo(n/p_k,k-1)
	long long yo(long long n, int k) {
	    if (n < PHI_N && k < PHI_K) return dp[n][k];
	    if (k == 1) return ((++n) >> 1);
	    if (primes[k - 1] >= n) return 1;
	    return yo(n, k - 1) - yo(n / primes[k - 1], k - 1);
	}
	long long Legendre(long long n) {
	    if (n < MAXN) return counter[n];
	    int lim = sqrt(n) + 1;
	    int k = upper_bound(primes, primes + len, lim) - primes;
	    return yo(n, k) + (k - 1);
	}
	//complexity: n^(2/3).(log n^(1/3))
	long long Lehmer(long long n) {
      if(n<0) return 0;
	    if (n < MAXN) return counter[n];
	    long long w, res = 0;
	    int i, j, a, b, c, lim;
	    b = sqrt(n), c = Lehmer(cbrt(n)), a = Lehmer(sqrt(b)), b = Lehmer(b);
	    res = yo(n, a) + (((b + a - 2) * (b - a + 1)) >> 1);
	    for (i = a; i < b; i++) {
	        w = n / primes[i];
	        lim = Lehmer(sqrt(w)), res -= Lehmer(w);
	        if (i <= c) {
	            for (j = i; j < lim; j++) {
	                res += j;
	                res -= Lehmer(w / primes[j]);
	            }
	        }
	    }
	    return res;
	}
}
int main() {
  cincout();
  ll l,r;
  cin>>l>>r;
  pcf::init();
  swap(l,r);
  if(l==1&&r==1){
    cout<<0<<endl;
    return 0;
  }
  cout<<pcf::Lehmer(l)-pcf::Lehmer(r-1)+pcf::Lehmer(2*l-1)-pcf::Lehmer(2*r-1)-(r==1)<<endl;
}
0