这个可以枚举gcd,然后另i/gcd=x,j/gcd=y。。变成找n/gcd范围内的质数对(x,y),然后统计一下质数个数就可以了。。
#include<bits/stdc++.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge*j=h[x];j;j=j->next)
#define inf 1e9
#define succ(x) (1LL<<(x))
#define sqr(x) ((x)*(x))
#define mem(a) memset(a,0,sizeof(a))
#define lowbit(x) (x&(-x))
#define ll long long
#define moid (x+y>>1)
#define eps 1e-8
#define NM 10000005
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,prime[NM],m,tot;
bool v[NM];
ll a[NM],s;
int main(){
n=read();
tot=0;
inc(i,2,n){
if(!v[i])prime[++tot]=i;
inc(j,1,tot){
if(i*prime[j]>n)break;
v[i*prime[j]]=true;
if(i%prime[j]==0)break;
}
}
inc(i,2,n)a[i]=a[i-1]+!v[i];
inc(i,1,n)s+=a[n/i]*(a[n/i]-1);
return 0*printf("%lld\n",s);
}
Diff-prime Pairs
链接:https://www.nowcoder.com/acm/contest/141/H
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
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 and
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