BZOJ 2820 YY的GCD

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

Sample Output

30
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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值