POJ 3090 Visible Lattice Points(莫比乌斯反演)

本文介绍了一种通过莫比乌斯函数简化计算第一象限中可见格点数量的方法,使用了数学技巧和算法优化,避免了暴力求解,并提供了一个有效的AC代码实现。
部署运行你感兴趣的模型镜像

传送门

Description

A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For example, the point (4, 2) is not visible since the line from the origin passes through (2, 1). The figure below shows the points (x, y) with 0 ≤ x, y ≤ 5 with lines from the origin to the visible points.

Write a program which, given a value for the size, N, computes the number of visible points (x, y) with 0 ≤ x, y ≤ N.

Input

The first line of input contains a single integer C (1 ≤ C ≤ 1000) which is the number of datasets that follow.

Each dataset consists of a single line of input containing a single integer N (1 ≤ N ≤ 1000), which is the size.

Output

For each dataset, there is to be one line of output consisting of: the dataset number starting at 1, a single space, the size, a single space and the number of visible points for that size.

Sample Input

4
2
4
5
231

Sample Output

1 2 5
2 4 13
3 5 21
4 231 32549

对于这道题题目说要满足原点能够看见就说名与原点的连线之间没有其他的点,也就是那个点坐标的gcd为1

暴力的话就是 ∑ i = 1 n ∑ j = 1 n g c d ( i , j ) \sum_{i=1}^{n}\sum_{j=1}^{n}gcd(i,j) i=1nj=1ngcd(i,j)

而对于这个式子我们是可以通过莫比乌斯来化简的,于是有

∑ i = 1 n ∑ j = 1 n g c d ( i , j ) = ∑ i = 1 n ∑ j = 1 n ∑ d ∣ g c d ( i , j ) μ ( d ) \sum_{i=1}^{n}\sum_{j=1}^{n}gcd(i,j)=\sum_{i=1}^{n}\sum_{j=1}^{n}\sum_{d|gcd(i,j)}\mu(d) i=1nj=1ngcd(i,j)=i=1nj=1ndgcd(i,j)μ(d)
我们把d提到前面来就有 ∑ d = 1 n μ ( d ) ∑ i = 1 n / d ∑ j = 1 n / d   ∑ d = 1 n μ ( d ) ∗ ⌊ n d ⌋ ∗ ⌊ n d ⌋ \sum_{d=1}^{n}\mu(d)\sum_{i=1}^{n/d}\sum_{j=1}^{n/d}\\~\\ \sum_{d=1}^{n}\mu(d)* \lfloor \frac{n}{d}\rfloor*\lfloor\frac{n}{d}\rfloor d=1nμ(d)i=1n/dj=1n/d d=1nμ(d)dndn
然后对这个式子进行一个分块就行了

不过还有一个要注意的地方就是ans要初始化为2,因为坐标轴上还有两个点。

AC代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4;
int miu[maxn];
int vis[maxn];
void mobius(){
	for(int i=1;i<=maxn;i++)miu[i]=1;
	for(int i=2;i<maxn;i++){
		if(!vis[i]){
			miu[i]=-1;
			for(int j=i+i;j<maxn;j+=i){
				vis[j]=1;
				if((j/i)%i==0)miu[j]=0;
				else miu[j]=-miu[j];
			}
		}
	}
	for(int i=1;i<=maxn;i++){
		miu[i]+=miu[i-1];
	}
}
int main()
{
	mobius();
	int t;
	cin>>t;
	for(int i=1;i<=t;i++)
	{
		int n,r=0,ans=2;
		cin>>n;
		for(int j=1;j<=n;j=r+1){
			r=n/(n/j);
			ans+=(miu[r]-miu[j-1])*(n/j)*(n/j);
		}
		cout<<i<<" "<<n<<" "<<ans<<endl;
	}
	return 0;
}

您可能感兴趣的与本文相关的镜像

Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

内容概要:本文提出了一种基于融合鱼鹰算法和柯西变异的改进麻雀优化算法(OCSSA),用于优化变分模态分解(VMD)的参数,进而结合卷积神经网络(CNN)与双向长短期记忆网络(BiLSTM)构建OCSSA-VMD-CNN-BILSTM模型,实现对轴承故障的高【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)精度诊断。研究采用西储大学公开的轴承故障数据集进行实验验证,通过优化VMD的模态数和惩罚因子,有效提升了信号分解的准确性与稳定性,随后利用CNN提取故障特征,BiLSTM捕捉时间序列的深层依赖关系,最终实现故障类型的智能识别。该方法在提升故障诊断精度与鲁棒性方面表现出优越性能。; 适合人群:具备一定信号处理、机器学习基础,从事机械故障诊断、智能运维、工业大数据分析等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①解决传统VMD参数依赖人工经验选取的问题,实现参数自适应优化;②提升复杂工况下滚动轴承早期故障的识别准确率;③为智能制造与预测性维护提供可靠的技术支持。; 阅读建议:建议读者结合Matlab代码实现过程,深入理解OCSSA优化机制、VMD信号分解流程以及CNN-BiLSTM网络架构的设计逻辑,重点关注参数优化与故障分类的联动关系,并可通过更换数据集进一步验证模型泛化能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值