Cattle off network ACM summer school and more training camp (third) H Diff-prime Pairs (contribution)

Cattle off network ACM summer school and more training camp (third) H Diff-prime Pairs (contribution)

Links: https://ac.nowcoder.com/acm/contest/141/H Source: Cattle-off network

Eddy has solved lots of problem involving calculating the number of coprime pairs within some range. This problem can be solved with inclusion-exclusion method. Eddy has implemented it lots of times. Someday, when he encounters another coprime pairs problem, he comes up with diff-prime pairs problem. diff-prime pairs problem is that given N, you need to find the number of pairs (i, j), where igcd(i,j)\frac{i}{gcd(i, j)}gcd(i,j)i and jgcd(i,j)\frac{j}{gcd(i,j)}gcd(i,j)j are both prime and i ,j ≤ N. gcd(i, j) is the greatest common divisor of i and j. Prime is an integer greater than 1 and has only 2 positive divisors.

Eddy tried to solve it with inclusion-exclusion method but failed. Please help Eddy to solve this problem.

Note that pair (i1, j1) and pair (i2, j2) are considered different if i1 ≠ i2 or j1 ≠ j2.

Enter a description:

Input has only one line containing a positive integer N.1 ≤ N ≤ 107

Output Description:

Output one line containing a non-negative integer indicating the number of diff-prime pairs (i,j) where i, j ≤ N

Example 1

Entry

copy

3

Export

copy

2

Example 2

Entry

copy

5

Export

copy

6

Meaning of the questions:

To give you an integer n (range \ ([1,1e7] \) ), you have to ask how much the number of \ ((i, j) \ ) satisfies \ (i / gcd (i, j) \) and \ (j / gcd (i, j ) \) are prime numbers.

Ideas:

Can be found by analysis (table hit), less than or equal to a prime number n, i, j, the contribution of the answer is \ (min (n / i, n / j) \)

Then we can screen 1 ~ N are prime numbers, while maintaining the answer,

We look ordered pair (i, j) (i <j) the contribution of the answer must be the n / j,

Then we use a cnt current record already has a number of prime numbers, the answer can be maintained directly after the screening. (Because the screen can be obtained from small to large prime number each, respectively)

Last answer * 2 (in turn ordered pairs)

Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int* p);
const int maxn = 1e7 + 50;
bool noprime[maxn + 50];
std::vector<int> p;
ll ans = 0ll;
ll n;
int getPrime()
{
    // 华丽的初始化
    memset(noprime, false, sizeof(noprime));
    p.clear();

    int m = (int)sqrt(maxn + 0.5);
    // 优化的埃筛
    for (int i = 2; i <= m; i++)
    {
        if (!noprime[i])
        {
            for (int j = i * i; j <= maxn; j += i)
            {
                noprime[j] = true;
            }
        }
    }
    ll cnt = 0ll;
    // 把素数加到vector里
    for (int i = 2; i <= n; i++)
    {
        if (!noprime[i])
        {
            ans += cnt * (n / i);
            cnt++;
            p.push_back(i);
        }
    }
    //返回vector的大小
    return p.size();

}
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    cin >> n;
    if (n <= 2)
    {
        cout << 0 << endl;
    } else
    {
        getPrime();
        cout << ans * 2ll << endl;
    }

    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Guess you like

Origin www.cnblogs.com/qieqiemin/p/11839376.html