結果
問題 | No.1661 Sum is Prime (Hard Version) |
ユーザー | むかで |
提出日時 | 2021-08-27 22:01:54 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 33 ms / 3,000 ms |
コード長 | 3,439 bytes |
コンパイル時間 | 2,499 ms |
コンパイル使用メモリ | 213,952 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-21 02:25:05 |
合計ジャッジ時間 | 3,434 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 3 ms
5,248 KB |
testcase_02 | AC | 23 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 1 ms
5,248 KB |
testcase_07 | AC | 1 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 2 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 2 ms
5,248 KB |
testcase_12 | AC | 20 ms
5,248 KB |
testcase_13 | AC | 20 ms
5,248 KB |
testcase_14 | AC | 22 ms
5,248 KB |
testcase_15 | AC | 28 ms
5,248 KB |
testcase_16 | AC | 23 ms
5,248 KB |
testcase_17 | AC | 22 ms
5,248 KB |
testcase_18 | AC | 10 ms
5,248 KB |
testcase_19 | AC | 17 ms
5,248 KB |
testcase_20 | AC | 11 ms
5,248 KB |
testcase_21 | AC | 19 ms
5,248 KB |
testcase_22 | AC | 15 ms
5,248 KB |
testcase_23 | AC | 33 ms
5,248 KB |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++) #define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++) #define setp(n) fixed << setprecision(n) 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 (a>b) { a=b; return 1; } return 0; } #define ll long long #define vll vector<ll> #define vi vector<int> #define pll pair<ll,ll> #define pi pair<int,int> #define all(a) (a.begin()),(a.end()) #define rall(a) (a.rbegin()),(a.rend()) #define fi first #define se second #define pb push_back #define ins insert #define debug(a) cerr<<(a)<<endl #define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl #define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;} using namespace std; template<class A, class B> ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";} template<class A, class B> istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;} template<class T> vector<T> make_vec(size_t a){ return vector<T>(a); } template<class T, class... Ts> auto make_vec(size_t a, Ts... ts){ return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } using i64 = long long; int isqrt(i64 n) { return sqrtl(n); } __attribute__((target("avx2"), optimize("O3", "unroll-loops"))) i64 prime_pi(const i64 N) { if (N <= 1) return 0; if (N == 2) return 1; const int v = isqrt(N); int s = (v + 1) / 2; vector<int> smalls(s); for (int i = 1; i < s; ++i) smalls[i] = i; vector<int> roughs(s); for (int i = 0; i < s; ++i) roughs[i] = 2 * i + 1; vector<i64> larges(s); for (int i = 0; i < s; ++i) larges[i] = (N / (2 * i + 1) - 1) / 2; vector<bool> skip(v + 1); const auto divide = [] (i64 n, i64 d) -> int { return double(n) / d; }; const auto half = [] (int n) -> int { return (n - 1) >> 1; }; int pc = 0; for (int p = 3; p <= v; p += 2) if (!skip[p]) { int q = p * p; if (i64(q) * q > N) break; skip[p] = true; for (int i = q; i <= v; i += 2 * p) skip[i] = true; int ns = 0; for (int k = 0; k < s; ++k) { int i = roughs[k]; if (skip[i]) continue; i64 d = i64(i) * p; larges[ns] = larges[k] - (d <= v ? larges[smalls[d >> 1] - pc] : smalls[half(divide(N, d))]) + pc; roughs[ns++] = i; } s = ns; for (int i = half(v), j = ((v / p) - 1) | 1; j >= p; j -= 2) { int c = smalls[j >> 1] - pc; for (int e = (j * p) >> 1; i >= e; --i) smalls[i] -= c; } ++pc; } larges[0] += i64(s + 2 * (pc - 1)) * (s - 1) / 2; for (int k = 1; k < s; ++k) larges[0] -= larges[k]; for (int l = 1; l < s; ++l) { int q = roughs[l]; i64 M = N / q; int e = smalls[half(M / q)] - pc; if (e < l + 1) break; i64 t = 0; for (int k = l + 1; k <= e; ++k) t += smalls[half(divide(M, roughs[k]))]; larges[0] += t - i64(e - l) * (pc + l - 1); } return larges[0] + 1; } //------------------------------------------------- int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll L,R; cin>>L>>R; ll ans=0; ans += prime_pi(R)-prime_pi(L-1); if (L<R) ans += prime_pi(2LL*R-1)-prime_pi(2LL*L); cout<<ans<<"\n"; return 0; }