H、Diff-prime Pairs

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 i / gcd(i, j) and j / gcd(i, 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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int gcd(int u,int v)
{
    int k=1,t;
    while(~u&1 && ~v&1)k<<=1,u>>=1,v>>=1;
    t=(u&1)?-v:u>>1;
    do
	{
        while(~t&1)t>>=1;
        t>0?u=t:v=-t;
    }while(t=u-v);
    return u*k;
}
bool isprime[10000010];
int main()
{
    int n;
    cin >> n;
    memset(isprime, true, sizeof(isprime));
    isprime[0] = false, isprime[1] = false;
    for (int i = 4; i <= 10000000; i += 2) // 所有大于 2 的偶数全都不是素数,首先划掉
        isprime[i] = false;
    for (int i = 2; i <= sqrt(n); i++)
        if (isprime[i])
            for (ll j = i * i; j <= n + 10; j += i + i)
                isprime[j] = false;
    long long int num = 0;
    int su[2][1000000],shu = 0;
    for(int count = 1;count <= n;++count)
        if(isprime[count])
		{
            su[0][shu] = count;
            ++shu;
        }
    for(int count = shu - 1;count >= 1;--count)
	{
        su[1][count] = (n / su[0][count]);
        num += su[1][count] * count;
    }
    cout << num * 2;
    return 0;
}
上一篇:2017-2018 ACM-ICPC, NEERC, Moscow Subregional Contest Problem G


下一篇:Solution -「香港网络赛 2016」A+B Problem