码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 2820 YY的GCD

时间:2015-03-11 17:29:53      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 10000010;
int mu[maxn], prime[maxn], vis[maxn], sum[maxn];
int cnt;
LL a, b;
void mobi(int n)
{
	mu[1] = 1;
	for(int i = 2; i <= n; i++)
	{
		if(!vis[i])
		{
			prime[cnt++] = i;
			mu[i] = -1;
			sum[i] = 1;
		}
		for(int j = 0; j < cnt && i*prime[j] <= n; j++)
		{
			vis[i*prime[j]] = 1;
			if(i%prime[j])
			{
				mu[i*prime[j]] = -mu[i];
				sum[i*prime[j]] = mu[i]-sum[i];
			}
			else
			{
				mu[i*prime[j]] = 0;
				sum[i*prime[j]] = mu[i];
				break;
			}
		}
	}
	for(int i = 1; i <= n; i++)
		sum[i] += sum[i-1];
}

LL cal(LL n, LL m)
{
	LL ans = 0;
	if(n > m)
		swap(n, m);
	int pos;
	for(int i = 1; i <= n; i = pos+1)
	{
		pos = min(n/(n/i), m/(m/i));
		ans += (sum[pos]-sum[i-1])*(m/i)*(n/i);
	}
	return ans;
}
int main()
{
	mobi(10000000);
	int T;
	scanf("%d", &T);
	while(T--)
	{
		
		scanf("%lld %lld", &a, &b);
		LL ans = cal(a, b);
		printf("%lld\n", ans);
	}
	return 0;
}


 

BZOJ 2820 YY的GCD

标签:

原文地址:http://blog.csdn.net/u011686226/article/details/44198993

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!