19暑假数论函数G

本文介绍了一种使用莫比乌斯反演算法解决三维晶格中从一角看另一角可见点数的问题,通过整除分块和前缀和技巧,实现了高效计算。文章包含完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
空间和平面上的点都可用莫比乌斯反演解决
懒得写公式了。。。参考博客:https://www.cnblogs.com/shenben/p/6748677.html

#include<bits/stdc++.h>
#define N 1000005
#define ll long long
using namespace std;
int p[N],check[N],tot;
int mu[N];
ll sum[N];
int T,n,m,d;
ll ans;
void init()              //算出莫比乌斯函数和前缀和 
{
	mu[1]=1;
	n=1e6;
	for(int i=2;i<=n;i++)
	{
		if (!check[i])
		{
			p[++tot]=i;
			mu[i]=-1;
		}
		for (int j=1;j<=tot && p[j]*i<=n;j++)
		{
			check[i*p[j]]=1;
			if (i%p[j]==0)
			{
				mu[i*p[j]]=0;
				break;	
			}
			else mu[i*p[j]]=-mu[i];
		}
	}
	for(int i=1;i<=n;i++) 
	sum[i]=(ll)mu[i]+sum[i-1];	
}
ll calc(int n)               //整除分块 
{
	ll ret=3;
	for (int L=1,R;L<=n;L=R+1)
	{
		R=n/(n/L);	
		ret+=(1ll)*(sum[R]-sum[L-1])*((1ll)*(n/L)*(n/L)*(n/L)+3ll*(n/L)*(n/L));
	}
	return ret;
} 
int main()
{
	init();
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d",&n);
		ans=calc(n);
		printf("%lld\n",ans);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值