ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)
2015-10-09 17:47
357 查看
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,这样就可以用容斥或者莫比乌斯解决了。
代码:
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; }
相关文章推荐
- Microsoft.AspNet.FriendlyUrls发布到IIS后404报错的解决方案
- android之APP模块编译
- MyEclipse中ctrl+F组合解决find查询相关问题
- Struts2之自定义局部类型转换器、全局类型转换器
- 调节系统音量VC++(适合win和xp系统) 打开静音 增减和减小系统音量
- Lucene系列-FieldCache
- 设计模式六大原则
- Lucene系列-FieldCache
- 三、oracle 用户管理一
- node lesson4--eventproxy不懂
- Struts2之自定义局部类型转换器、全局类型转换器
- Wi-Fi 20mhz 和 40mhz 频段带宽的区别是什么? 图解
- iOS开发Icon图标设置 (转)
- Windows 7、Windows Server 2008等系统音频系统底层API[转]
- 完全卸载SQL Sever2008 R2
- oracle sql*plus常用命令
- Kalman Filter算法入门
- Maven project转换为Dynamic web project
- 使用FLIRT签名识别库
- http://www.360doc.com/content/07/0103/19/16773_317325.shtml