ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)
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
题目大意就是求不同种斜率的个数。
第一反应的话枚举所有斜率,然后去掉相同斜率,而相同斜率的特征的就是,斜率的分子分母约分后和其中某个斜率一质。
于是,我只需要考虑分子分母互质的斜率即可。
于是就可以枚举斜率的分母或者分子,如果枚举斜率的分母,
比如x = 1,那么y只能取1
x = 2,那么y取[1, 2]与2互质的数,
x = 3, 那么y取[1, 3]与3互质的数
…
…
(注意x = n, y = 0和x = 0, y = 1也要加上。)
于是整个结果就是x取遍[1, n],y取遍[1, n]求互质的对数。
这和之前的一道莫比乌斯一样,不过k取1,这样就可以用容斥或者莫比乌斯解决了。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define LL long long using namespace std; const int maxN = 1005;
int n;
int prime[maxN], u[maxN];
bool vis[maxN]; void mobius()
{
memset(vis, false,sizeof(vis));
u[1] = 1;
int cnt = 0;
for(int i = 2; i < maxN; i++)
{
if(!vis[i])
{
prime[cnt++] = i;
u[i] = -1;
}
for(int j = 0; j < cnt && i*prime[j] < maxN; j++)
{
vis[i*prime[j]] = true;
if(i%prime[j])
u[i*prime[j]] = -u[i];
else
{
u[i*prime[j]] = 0;
break;
}
}
}
} void work()
{
LL ans = 0;
for (int i = 1; i <= n; ++i)
ans += (LL)u[i]*(n/i)*(n/i);
printf("%I64d\n", ans+2);
} int main()
{
//freopen("test.in", "r", stdin);
mobius();
int T;
scanf("%d", &T);
for (int times = 1; times <= T; ++times)
{
scanf("%d", &n);
printf("%d %d ", times, n);
work();
}
return 0;
}
ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)的更多相关文章
- POJ3090 Visible Lattice Points
/* * POJ3090 Visible Lattice Points * 欧拉函数 */ #include<cstdio> using namespace std; int C,N; / ...
- SPOJ 7001. Visible Lattice Points (莫比乌斯反演)
7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...
- [SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演
7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...
- Visible Lattice Points (莫比乌斯反演)
Visible Lattice Points 题意 : 从(0,0,0)出发在(N,N,N)范围内有多少条不从重合的直线:我们只要求gcd(x,y,z) = 1; 的点有多少个就可以了: 比如 : 点 ...
- POJ3090 Visible Lattice Points 欧拉函数
欧拉函数裸题,直接欧拉函数值乘二加一就行了.具体证明略,反正很简单. 题干: Description A lattice point (x, y) in the first quadrant (x a ...
- ACM学习历程—HDU4717 The Moving Points(模拟退火 || 三分法)
Description There are N points in total. Every point moves in certain direction and certain speed. W ...
- POJ3090 Visible Lattice Points (数论:欧拉函数模板)
题目链接:传送门 思路: 所有gcd(x, y) = 1的数对都满足题意,然后还有(1, 0) 和 (0, 1). #include <iostream> #include <cst ...
- [POJ3090]Visible Lattice Points(欧拉函数)
答案为3+2*∑φ(i),(i=2 to n) Code #include <cstdio> int T,n,A[1010]; void Init(){ for(int i=2;i< ...
- ACM学习历程—HDU 5072 Coprime(容斥原理)
Description There are n people standing in a line. Each of them has a unique id number. Now the Ragn ...
随机推荐
- 初识Modbus TCP/IP-------------C#编写Modbus TCP客户端程序(二)
由于感觉上一次写的篇幅过长,所以新开一贴,继续介绍Modbus TCP/IP的初步认识, 书接上回 3).03(0x03)功能码--------读保持寄存器 请求与响应格式 这是一个请求读寄存器108 ...
- GitHub从无到有
一步一步教你如何在GitHub上上传自己的项目 2018年07月04日 09:23:40 夏雨薇安 阅读数:22764 首先你得注册一个自己的GitHub账号,注册网址:https://githu ...
- fzu2020( c(n,m)%p,其中n, m, p (1 <= m <= n <= 10^9, m <= 10^4, m < p < 10^9, p是素数) )
基本的模板题,统计分子分母中p出现的次数,然后求逆元取模. // // main.cpp // fzu2020 // // Created by 陈加寿 on 15/12/27. // Copyrig ...
- 【BZOJ3488】[ONTAK2010]Highways 扫描线+树状数组
[BZOJ3488][ONTAK2010]Highways Description 给一棵n个点的树以及m条额外的双向边q次询问,统计满足以下条件的u到v的路径:恰经过一条额外的边不经过树上u到v的路 ...
- ArcGIS API for javascript Bookmarks(书签)示例2
1.运行效果图 说明:这篇博文介绍的书签位于地图之上 有关博文中引用的API文件 怎么iis上部署,请参考我前面的博文 2.HTML代码 <!DOCTYPE html> <html ...
- 海信电视 LED55K370 升级固件总结【含固件下载地址】
最早电视买回来,感觉垃圾软件太多,root后,删软件不小心删除了桌面,导致没桌面. 用ADB装了点软件,凑合可以用. 后来装了悟空遥控,然后装了沙发桌面,不影响使用了. 最近海信不停推送更新系统,改手 ...
- 混沌相关blog+节选
<数字化定量分析:一致性获利法时间跨度的定量研究> http://blog.sina.com.cn/s/blog_82cf83d50101a41q.html —— 用60分 ...
- Video Brightness Enhancement
Tone Mapping原是摄影学中的一个术语,因为打印相片所能表现的亮度范围不足以表现现实世界中的亮度域,而如果简单的将真实世界的整个亮度域线性压缩到照片所能表现的亮度域内,则会在明暗两端同时丢失很 ...
- NCL 小图对其问题
从昨天下午开始的折腾终于告一段落,虽然解决得不甚完善,只是图可以用了…… 问题起自想把之前手动拼成的一页四张的图用脚本自动生成,这样一方面应该对得更齐一点,另一方面大大节省人工. 这本来应该是件很容易 ...
- CommonJS与AMD、CMD
随着JS模块化编程的发展,处理模块之间的依赖关系变得至关重要,随后诞生了CommonJS.AMD与CMD规范,但es6的import/export能代替他们,但因为本人所使用的webpack也支持前三 ...