結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー sapphire__15sapphire__15
提出日時 2021-08-27 21:31:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 952 ms / 2,000 ms
コード長 3,633 bytes
コンパイル時間 1,615 ms
コンパイル使用メモリ 137,492 KB
実行使用メモリ 177,816 KB
最終ジャッジ日時 2023-08-13 08:03:40
合計ジャッジ時間 9,778 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 863 ms
161,344 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,384 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 6 ms
4,400 KB
testcase_12 AC 810 ms
153,364 KB
testcase_13 AC 373 ms
76,512 KB
testcase_14 AC 552 ms
106,600 KB
testcase_15 AC 618 ms
120,196 KB
testcase_16 AC 226 ms
47,320 KB
testcase_17 AC 196 ms
42,556 KB
testcase_18 AC 233 ms
49,268 KB
testcase_19 AC 826 ms
157,108 KB
testcase_20 AC 377 ms
76,156 KB
testcase_21 AC 952 ms
177,816 KB
testcase_22 AC 472 ms
91,116 KB
testcase_23 AC 505 ms
98,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <cassert>
#include <stack>
#include <numeric>
#include <iomanip>
#include <limits>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <cstring>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<ll, ll> Pll;
typedef std::pair<double, double> Pdd;

#define rip(_i, _n, _s) for (int _i = (_s); _i < (int)(_n); _i++)
#define all(_l) _l.begin(), _l.end()
#define rall(_l) _l.rbegin(), _l.rend()
#define MM << " " <<

template<typename _T>
using MaxHeap = std::priority_queue<_T>;
template<typename _T>
using MinHeap = std::priority_queue<_T, std::vector<_T>, std::greater<_T>>;

template<typename _T>
inline bool chmax(_T &_l, const _T _b) {
    if (_b > _l) {
        _l = _b;
        return true;
    }
    return false;
}
template<typename _T>
inline bool chmin(_T &_l, const _T _b) {
    if (_l > _b) {
        _l = _b;
        return true;
    }
    return false;
}

template<typename _T>
void vdeb(const std::vector<_T> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        if (i == bb.size() - 1) std::cout << bb[i];
        else std::cout << bb[i] << ' ';
    }
    std::cout << '\n';
}
template<typename _T>
void vdeb(const std::vector<std::vector<_T>> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        std::cout << i << ": ";
        vdeb(bb[i]);
    }
    std::cout << '\n';
}

class Eratosthenes {
    std::vector<std::vector<long long>> large_sieve;
    std::vector<long long> small_sieve;

    public:
    const long long l;
    const long long r;
    Eratosthenes(const long long l_, const long long r_) :
    large_sieve(r_-l_), small_sieve(sqrt(r_)+2), l(l_), r(r_) {
        assert(0 < l && l < r);
        const long long SQ = small_sieve.size();
        std::vector<long long> tmp;
        for(int i = 0; i < r-l; i++) {
            tmp.push_back(i+l);
        }
        for(long long i = 2; i < SQ; i++) {
            if(small_sieve[i] > 0) continue;
            for(long long j = i*i; j < (long long)small_sieve.size(); j += i) {
                small_sieve[j] = i;
            }
            for(long long j = (l+i-1)/i*i; j < r; j += i) {
                while(tmp[j-l]%i == 0) {
                    large_sieve[j-l].push_back(i);
                    tmp[j-l] /= i;
                }
            }
        }
        for(long long i = l; i < r; i++) {
            while(1 < tmp[i-l] && tmp[i-l] < SQ && 0 < small_sieve[tmp[i-l]]) {
                large_sieve[i-l].push_back(small_sieve[tmp[i-l]]);
                tmp[i-l] /= small_sieve[tmp[i-l]];
            }
            if(tmp[i-l] > 1) large_sieve[i-l].push_back(tmp[i-l]);
        }
    }
    
    std::vector<long long> factorization(long long x) {
        assert(l <= x && x < r);
        return large_sieve[x-l];
    }

    bool is_prime(long long x) {
        assert(l <= x && x < r);
        return x != 1 && large_sieve[x-l].size() == 1;
    }

    void varify() {
        for(long long i = l; i < r; i++) {
            long long now = 1;
            for(auto &&j : large_sieve[i-l]) {
                assert(1 < j);
                now *= j;
            }
            assert(now == i);
        }
    }
};

using namespace std;

int main() {
    int l ,r; cin >> l >> r;
    Eratosthenes er(l ,r*2);
    int ans = 0;
    rip(i,r+1, l) {
        if(er.is_prime(i)) ans++;
    }
    rip(i, r*2, l*2+1) {
        if(er.is_prime(i)) ans++;
    }
    cout << ans << endl;
}
0