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, yN.

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,这样就可以用容斥或者莫比乌斯解决了。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #define LL long long
  6.  
  7. using namespace std;
  8.  
  9. const int maxN = 1005;
  10. int n;
  11. int prime[maxN], u[maxN];
  12. bool vis[maxN];
  13.  
  14. void mobius()
  15. {
  16. memset(vis, false,sizeof(vis));
  17. u[1] = 1;
  18. int cnt = 0;
  19. for(int i = 2; i < maxN; i++)
  20. {
  21. if(!vis[i])
  22. {
  23. prime[cnt++] = i;
  24. u[i] = -1;
  25. }
  26. for(int j = 0; j < cnt && i*prime[j] < maxN; j++)
  27. {
  28. vis[i*prime[j]] = true;
  29. if(i%prime[j])
  30. u[i*prime[j]] = -u[i];
  31. else
  32. {
  33. u[i*prime[j]] = 0;
  34. break;
  35. }
  36. }
  37. }
  38. }
  39.  
  40. void work()
  41. {
  42. LL ans = 0;
  43. for (int i = 1; i <= n; ++i)
  44. ans += (LL)u[i]*(n/i)*(n/i);
  45. printf("%I64d\n", ans+2);
  46. }
  47.  
  48. int main()
  49. {
  50. //freopen("test.in", "r", stdin);
  51. mobius();
  52. int T;
  53. scanf("%d", &T);
  54. for (int times = 1; times <= T; ++times)
  55. {
  56. scanf("%d", &n);
  57. printf("%d %d ", times, n);
  58. work();
  59. }
  60. return 0;
  61. }

ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)的更多相关文章

  1. POJ3090 Visible Lattice Points

    /* * POJ3090 Visible Lattice Points * 欧拉函数 */ #include<cstdio> using namespace std; int C,N; / ...

  2. SPOJ 7001. Visible Lattice Points (莫比乌斯反演)

    7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...

  3. [SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演

    7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...

  4. Visible Lattice Points (莫比乌斯反演)

    Visible Lattice Points 题意 : 从(0,0,0)出发在(N,N,N)范围内有多少条不从重合的直线:我们只要求gcd(x,y,z) = 1; 的点有多少个就可以了: 比如 : 点 ...

  5. POJ3090 Visible Lattice Points 欧拉函数

    欧拉函数裸题,直接欧拉函数值乘二加一就行了.具体证明略,反正很简单. 题干: Description A lattice point (x, y) in the first quadrant (x a ...

  6. ACM学习历程—HDU4717 The Moving Points(模拟退火 || 三分法)

    Description There are N points in total. Every point moves in certain direction and certain speed. W ...

  7. POJ3090 Visible Lattice Points (数论:欧拉函数模板)

    题目链接:传送门 思路: 所有gcd(x, y) = 1的数对都满足题意,然后还有(1, 0) 和 (0, 1). #include <iostream> #include <cst ...

  8. [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< ...

  9. ACM学习历程—HDU 5072 Coprime(容斥原理)

    Description There are n people standing in a line. Each of them has a unique id number. Now the Ragn ...

随机推荐

  1. php之 人员的权限管理

    1.想好权限管理的作用? 2.有什么权限内容? 3.既然有权限管理那么就会有管理员? 4.登录后每个人员的界面会是不一样的? 一.想好这个权限是什么? 就做一个就像是vip的功能,普通用户和vip用户 ...

  2. python is == 的区别, 编码与解码.深浅拷贝

    一. is  ==  的区别 双等表示的是判断是否相等, 注意. 这个双等比较的是具体的值.而不是内存地址 is 比较的是地址 编码回顾 除了了ASCII码以外, 其他信息不能直接转换 编码和解码的时 ...

  3. Python闲谈(二)聊聊最小二乘法以及leastsq函数

    1 最小二乘法概述 自从开始做毕设以来,发现自己无时无刻不在接触最小二乘法.从求解线性透视图中的消失点,m元n次函数的拟合,包括后来学到的神经网络,其思想归根结底全都是最小二乘法. 1-1 “多线→一 ...

  4. 九度OJ 1197:奇偶校验 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3590 解决:1511 题目描述: 输入一个字符串,然后对每个字符进行奇校验,最后输出校验后的二进制数(如'3',输出:10110011). ...

  5. 搭建Cat笔记01

    昨天晚上搭建Cat 时候那叫一个坑b,宝宝心里苦呀! 准备工作: 1.先大众点评Cat的项目源码,https://github.com/dianping/cat.git 2.打包编译: mvn cle ...

  6. iOS Base64加密

    1.Base64编码说明 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个0,形成8位一个字节的形式. 如果剩下的字符不足3个字节,则用0 ...

  7. Android系统字体规范

    我们在做Android移动APP设计的时候,字号的选择也是很让人头疼,转载一份有关Android系统字体规范,如果在做Android项目的用户应该看看,如果有任何建议欢迎在留言处与我们交流探讨. 主要 ...

  8. Bootstrap学习1--响应式导航栏

    备注:最新Bootstrap手册:http://www.jqhtml.com/bootstraps-syntaxhigh/index.html <nav class="navbar n ...

  9. 第2条:遵循PEP8风格指南

    <Python Enhancement Proposal #8>(8号Python增强提案)又叫PEP8,它是针对Python代码格式而编订的风格指南. 尽管可以在保证语法正确的前提下随意 ...

  10. autoplay移动端不能自动播放

    本文总结自:https://stackoverflow.com/questions/12496144/can-you-autoplay-html5-videos-on-the-ipad 首先,自动播放 ...