牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网

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.

输入描述:

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

输出描述:

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

示例1

输入

复制

3

输出

复制

2

示例2

输入

复制

5

输出

复制

6

题意:

给你一个整数n(范围是\([1,1e7]\)),问你有多少个数对\((i,j)\) 满足 \(i/gcd(i,j)\)\(j/gcd(i,j)\) 都是质数。

思路:

通过分析(打表)可以发现,对于小于等于n的质数i,j,对答案的贡献是\(min(n/i,n/j)\)

那么我们可以筛1~N 中质数的同时,维护答案,

我们来看有序对(i,j)(i < j)对答案的贡献一定是 n/j ,

那么我们用一个cnt记录当前前面已经有多少个质数了,在筛后直接维护答案即可。(因为筛后可以从小到大分别求出每一个质数)

最后答案*2(有序对的反过来)

代码

#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';
        }
    }
}