Untitled

 avatar
unknown
plain_text
5 months ago
1.3 kB
2
Indexable
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n; ll cnt;
bool p[10000001]; // 1e7 + 1

void sieve()
{
    for(int i = 2; i <= 10000000; i++) p[i] = true;
    p[0] = p[1] = false;
    int can = sqrt(10000000);
    for(int i = 2; i <= can; i++)
    {
        if(p[i])
        {
            for(int j = i*i; j <= 10000000; j+=i)
                p[j] = false;
        }
    }
}

void inp()
{
    cin >> n;
}

void sub1()
{
    cnt = 0;
    for(int x = 1; x <= n; x++)
    {
        for(int y = 1; y <= n; y++)
        {
            if(p[x+2*y]) ++cnt;
        }
    }
}

void sub2()
{
    cnt = 0;
    vector<int> v;
    for(int i = 2; i <= 10000000; i++) if(p[i]) v.push_back(i);
    for(int y = 1; y <= n; y++)
    {
        int l = 1 + 2*y, r = n + 2*y;
        auto itl = lower_bound(v.begin(), v.end(), l);
        auto itr = upper_bound(v.begin(), v.end(), r);
        int le = itl - v.begin();
        int ri = itr - v.begin() - 1;
        cnt += ri - le + 1;
    }
}

void sol()
{
    sieve();
    //sub1();
    //cout << cnt << '\n';
    sub2();
    cout << cnt << '\n';
}

int main(){ 
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    freopen("BIEUTHUCNT.inp","r",stdin);
    freopen("BIEUTHUCNT.out","w",stdout);

    inp();
    sol();
    
    return 0;
}
Editor is loading...
Leave a Comment