1007 H、Diff-prime Pairs 素数筛性质:获得一定范围内的素数数量 数论

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

题目描述

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 ≤ 10
7

输出描述:

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

分析

两个数互质,说明两个数的质因子各有一个差别,然后有相同的主部,设主部是i ,x1 = ai ,x2 = bi。

然后就是枚举主部i,看看有多少满足条件的a 和b 质数对就可以了,条件: a * i <= x1 ,

先用素数筛获得总共的质数数目,然后挨个删除不满足条件的质数数量,在满足条件的质数数量中选择两个就是 C2cnt = cnt * (cnt - 1); 将所有满足条件的加起来,就是答案。

 

#include<bits/stdc++.h>
#define TLE ios::sync_with_stdio(0),cin.tie(0)
#define endl "\n"
#define pb push_back
#define gg exit(0);
#define rt return;
#define bd cout<<"debug"<<endl;
#define db(x) cout<<#x<<':'<<x<<endl;
#define dbb(i,a) cout<<#i<<':'<<i<<' '<<#a<<':'<<a<<' '<<endl;
#define dbbb(i,a,b) cout<<#i<<':'<<i<<' '<<#a<<':'<<a<<' '<<#b<<':'<<b<<endl;
#define YES cout<<"YES"<<endl;
#define Yes cout<<"Yes"<<endl;
#define NO cout<<"NO"<<endl;
#define No cout<<"No"<<endl;
#define None cout<<-1<<endl;
#define el cout<<endl;
#define x first
#define y second
#define V vector
#define fo(i,j,n) for(int i = j;i<=n;i++)
#define of(i,n,j) for(int i = n;i>=j;i--)
#define all(a) a.begin(),a.end()
#define alll(a) a.begin()+1,a.end()
#define ms(a,b) memset(a, b, sizeof(a))
#define lowbit(x) (x&-x)
#define gcd(a,b) __gcd(a,b)
#define DEBUG 11243
using namespace std;
void clapping() {
     #if DEBUG == 1
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    #endif
}
template<class T>inline void read(T &res) {
    char c;T flag = 1;
    while((c = getchar()) < '0' || c > '9') if(c == '-') flag = -1;res = c - '0';
    while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
typedef pair<int,int> pii;
typedef pair<long,long>pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
int dy[] = {1,0,-1,0,1,1,-1,-1};
int dx[] = {0,1,0,-1,1,-1,1,-1};

/*文档区


*/

//-------------------------代码----------------------------

#define int ll
const int N = 1e7+10;
int n,m;
int primes[N],cnt;
bool res[N],st[N];
void solve()
{
    cin>>n;
    
    for(int i = 2;i<=N;i++) {
        int nums = 0;
        if(!st[i]) {
            primes[cnt ++] = i;
        }
        for(int j = 0;primes[j] * i <= N;j++) {
            st[primes[j] * i] = 1;
            if(i % primes[j] == 0) break;
        }
    }
    int ans = 0;
    for(int i = 1;i<=n;i++) {
        while(cnt >= 0 && primes[cnt - 1] * i > n) cnt -- ;
        ans += cnt * (cnt - 1);
    }
    cout<<ans<<endl;
}

signed main(){
    clapping();TLE;
    
//    int t;cin>>t;while(t -- )
    solve();
//    {solve(); }
    return 0;
}

/*样例区


*/

//------------------------------------------------------------

 

posted @ 2022-07-24 21:43  er007  阅读(33)  评论(0编辑  收藏  举报