YY的GCD
Time Limit: 10 Sec Memory Limit: 512 MB
[Submit][Status][Discuss]
Description
求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对k。
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
Solution
Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long s64; const int ONE = ; int T;
int n,m;
bool isp[ONE];
int prime[],p_num;
int miu[ONE],sum[ONE];
s64 Ans; int get()
{
int res=,Q=; char c;
while( (c=getchar())< || c>)
if(c=='-')Q=-;
if(Q) res=c-;
while((c=getchar())>= && c<=)
res=res*+c-;
return res*Q;
} void Getmiu(int MaxN)
{
miu[] = ;
for(int i=; i<=MaxN; i++)
{
if(!isp[i])
prime[++p_num] = i, miu[i] = -;
for(int j=; j<=p_num, i*prime[j]<=MaxN; j++)
{
isp[i * prime[j]] = ;
if(i % prime[j] == )
{
miu[i * prime[j]] = ;
break;
}
miu[i * prime[j]] = -miu[i];
}
}
for(int j=; j<=p_num; j++)
for(int i=; i*prime[j]<=MaxN; i++)
sum[i * prime[j]] += miu[i];
for(int i=; i<=MaxN;i++)
sum[i] += sum[i-];
} void Solve()
{
n=get(); m=get();
if(n > m) swap(n,m);
Ans = ;
for(int i=, j=; i<=n; i=j+)
{
j = min(n/(n/i), m/(m/i));
Ans += (s64) (n/i) * (m/i) * (sum[j] - sum[i-]);
}
printf("%lld\n",Ans);
} int main()
{
Getmiu(ONE-);
T=get();
while(T--)
Solve();
}