spoj 7001 Visible Lattice Points (莫比乌斯反演)

Visible Lattice Points

Consider a NNN lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many lattice points are visible from corner at (0,0,0) ? A point X is visible from point Y iff no other lattice point lies on the segment joining X and Y.

Input :
The first line contains the number of test cases T. The next T lines contain an interger N

Output :
Output T lines, one corresponding to each test case.

Sample Input :
3
1
2
5

Sample Output :
7
19
175

Constraints :
T <= 50
1 <= N <= 1000000

题目大意

从(0,0,0)的点往(n,n,n)的点看,能看到多少个点

思考

三维难以理解,所以降低到二维来思考,同样的二维平面如果要看到是什么情况,简单来说就是前方没有点斜率与他一样,那么转换成能看到的点,其实就是这个点是这个斜率的第一个点,也就是gcd(x,y)==1则它为第一个,大胆猜想三维平面,所以实际上所求得就是gcd(x,y,z)==1的那些点。那么题目就可以求了。
接下来用莫比乌斯反演,莫比乌斯反演很简单:
在这里插入图片描述
但这道题如果用这种格式是化简不出来的,所以应用另外一个版本
在这里插入图片描述
好的,那么我们假设f(n)为gcd(x,y,z)等于n的数量,
假设g(n)为n|gcd(x,y,z),也就是n是gcd(x,y,z)的约数的数量,那么很容易得到gn) = sigma{ n|d, f(d) },应用莫比乌斯反演我们可以得到f(n) = sigma{ n|d, mu(d/n) * g(d) },那么题目中所求的f(1)就是sigma{n|1,mu(d)*g(d)};
g(d)是很容易知道的,因为想要gcd(x,y,z)为d的倍数,实际上,xyz都需要为d的倍数,那么答案就是n中为d的倍数的乘,g(d)=(n/d)*(n/d)*(n/d);
到这里题目大部分已经解析完毕了,需要注意的是gcd中xyz不能为0所以等于0的情况我们要另外讨论,一个等于0实际上就是二维的平面了,那么上述的g(d)少乘一个即可,2个为0实际上就是一条线,那就只能看一个点,3个为0就是起点。综上所述,答案就是sum=sigma{mu(d)*((n/d)*(n/d)*(n/d+3))}+3;

下面给出ac代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000000+5;
long long mo[maxn], vis[maxn], prime[maxn];
void init () {
	mo[1] = 1;
	memset(vis, 0,sizeof(vis));
	int len = 0;
	for (int i = 2; i < maxn; ++i) {
		if (!vis[i]) {
			prime[len++] = i;
			mo[i] = -1;
		}
		for (int j = 0; j < len && (long long)prime[j] * i < maxn; ++j) {
			vis[ i * prime[j] ] = 1;
			if (i % prime[j] == 0) {
				mo[ i * prime[j] ] = 0;
				break;
			}
			mo[ i * prime[j] ] = -mo[i];
		}
	}
	// for (int i = 2; i < maxn; ++i) mo[i] += mo[i-1];
}
//此部分为莫比乌斯函数线性筛
int main()
{
	int a;
	int b;
	scanf("%d",&a);
	init();
	for(int i=0;i<a;i++)
	{
		scanf("%d",&b);
		long long sum=3;
		for(int k=1;k<=b;k++)
		{
			sum+=(mo[k]*(b/k)*(b/k)*(b/k+3));
		}
		printf("%lld\n",sum);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值