Description
神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种
傻×必然不会了,于是向你来请教……多组输入
Input
第一行一个整数T 表述数据组数接下来T行,每行两个正整数,表示N, M
Output
T行,每行一个整数表示第i组数据的结果
Sample Input
2
10 10
100 100
10 10
100 100
Sample Output
30
2791
2791
HINT
T = 10000
N, M <= 10000000
Source
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~莫比乌斯函数+容斥原理~
过程推导详见黄学长的博客:http://hzwer.com/6142.html~
#include<cstdio>
#include<iostream>
using namespace std;
#define ll long long
ll n,a,b,c[1000001],miu[10000001],num[10000001],ans,ne;
bool bb[10000001];
ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
miu[1]=1;
for(int i=2;i<=10000000;i++)
{
if(!bb[i]) c[++c[0]]=i,miu[i]=-1;
for(int j=1;j<=c[0] && c[j]*i<=10000000;j++)
{
bb[i*c[j]]=1;
if(i%c[j]) miu[i*c[j]]=-miu[i];
else break;
}
}
for(int i=1;i<=c[0];i++)
for(int j=1;c[i]*j<=10000000;j++) num[c[i]*j]+=miu[j];
for(int i=1;i<=10000000;i++) num[i]+=num[i-1];
n=read();
while(n--)
{
a=read();b=read();ans=0;
if(a>b) swap(a,b);
for(int i=1;i<=a;i=ne)
{
ne=min(a/(a/i),b/(b/i))+1;
ans+=(num[ne-1]-num[i-1])*(a/i)*(b/i);
}
printf("%lld\n",ans);
}
return 0;
}