http://poj.org/problem?id=3090

Visible Lattice Points
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6153   Accepted: 3662

Description

A lattice point (xy) 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 (xy) 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 (xy) with 0 ≤ xy ≤ 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 (xy) with 0 ≤ xy ≤ 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

隐藏很深的一道欧拉函数题,观察下面这个图,将图分成上下两个三角形,观察下三角,横坐标从2开始,发现可以看到的点有(2,1),  (3,1),(3,2),  (4,1),(4,3),  (5,1),(5,2),(5,3),(5,4)

从中可以发先一个规律可以看到的点的横坐标与纵坐标的关系是互质的,由此可以求出下三角横坐标互质的数的和ans,上三角和下三角同理,我们讨论时横坐标是从2开始的,我们忽略了三个点

(0,1),(1,0),(1,1)最后加上即可,最终总个数ans = ans * 2 + 3;

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm> using namespace std; int solve(int n)
{
int ans = n;
for(int i = ; i * i <= n ; i++)
{
if(n % i == )
{
ans = ans - ans / i;
while(n % i == )
n /= i;
}
}
if(n > )
ans = ans - ans / n;
return ans;
} int main()
{
int t, i, n, x = ;
scanf("%d", &t);
while(t--)
{
x++;
int ans = ;
scanf("%d", &n);
int a = n;
for(i = ; i <= n ; i++)
ans += solve(i);
ans = ans * + ;
printf("%d %d %d\n", x, a, ans);
}
return ;
}

poj 3060 Visible Lattice Points的更多相关文章

  1. 数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points

    Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5636   Accepted: ...

  2. POJ 3090 Visible Lattice Points 欧拉函数

    链接:http://poj.org/problem?id=3090 题意:在坐标系中,从横纵坐标 0 ≤ x, y ≤ N中的点中选择点,而且这些点与(0,0)的连点不经过其它的点. 思路:显而易见, ...

  3. POJ 3090 Visible Lattice Points (ZOJ 2777)

    http://poj.org/problem?id=3090 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1777 题目大意: ...

  4. poj 3090 Visible Lattice Points(离线打表)

    这是好久之前做过的题,算是在考察欧拉函数的定义吧. 先把欧拉函数讲好:其实欧拉函数还是有很多解读的.emmm,最基础同时最重要的算是,¢(n)表示范围(1, n-1)中与n互质的数的个数 好了,我把规 ...

  5. POJ 3090 Visible Lattice Points 【欧拉函数】

    <题目链接> 题目大意: 给出范围为(0, 0)到(n, n)的整点,你站在(0,0)处,问能够看见几个点. 解题分析:很明显,因为 N (1 ≤ N ≤ 1000) ,所以无论 N 为多 ...

  6. [poj] 3090 Visible Lattice Points

    原题 欧拉函数 我们发现,对于每一个斜率来说,这条直线上的点,只有gcd(x,y)=1时可行,所以求欧拉函数的前缀和.2*f[n]+1即为答案. #include<cstdio> #def ...

  7. POJ 3090 Visible Lattice Points | 其实是欧拉函数

    题目: 给一个n,n的网格,点可以遮挡视线,问从0,0看能看到多少点 题解: 根据对称性,我们可以把网格按y=x为对称轴划分成两半,求一半的就可以了,可以想到的是应该每种斜率只能看到一个点 因为斜率表 ...

  8. poj 3090 Visible Lattice Points 法利系列||通过计

    因为图像关于对角线对称.所以我们仅仅看下三角区域. 将x轴看做分母,被圈的点看成分子 依次是{1/2},{1/3,1/2},{1/4,3/4},{1/5,2/5,3/5,4/5} 写成前缀和的形式就是 ...

  9. 【POJ】3090 Visible Lattice Points(欧拉函数)

    Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7705   Accepted: ...

随机推荐

  1. POJ 2492 (简单并查集) A Bug's Life

    题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋.然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋 这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的 ...

  2. 51nod1086 背包问题 V2

    我都快不会写二进制优化多重背包了...卡了一下常数从rank100+到20+... #include<cstdio> #include<cstring> #include< ...

  3. [转载] mysql5.6 删除之前的ibdata1文件后再重新生成,遇到[Warning] Info table is not ready to be used. Table 'mysql.slave_master_info' cannot be opened.问题

    [转载] mysql5.6 删除之前的ibdata1文件后再重新生成,遇到[Warning] Info table is not ready to be used. Table 'mysql.slav ...

  4. [转][TFS] 禁止默认允许多人签出和强制解除签入签出锁

    转自:http://blog.xieyc.com/tfs-disable-multiple-check-out-and-force-to-undo-locking/ | 小谢的小站 [TFS] 禁止默 ...

  5. (转载)目前最细致清晰的NSDictionary以及NSMutableDictionary用法总结

    文章转载自:http://www.cnblogs.com/wengzilin/archive/2012/03/15/2397712.html 做过Java语言 或者 C语言 开发的朋友应该很清楚 关键 ...

  6. Java笔记之数组

    1.int flags[] = new int[10];数组中的每个元素初始化为0. Arrays.fill(flags, 0);将数组中每个元素置为0.

  7. xcode安装app

    安装 xcode 安装 xcode command line tool 检查是否安装 在终端中运行: xcrun simctl list 如果出现所有的 Device Types,则可以进行第3步 如 ...

  8. Mysql自定义函数之------------This function has none of DETERMINISTIC, NO SQL解决办法

    This function has none of DETERMINISTIC, NO SQL解决办法 创建存储过程时 出错信息: ERROR 1418 (HY000): This function ...

  9. 使用过的Linux命令

    1 在vim中编辑python,由于tab是8个空格,然而python中是4个,需要替换 :%s/\t/    /g 2 tar tar -czvf topic_dt_poi.0801.0818.ta ...

  10. httpclient介绍

    前言 超文本传输协议(HTTP)也许是当今互联网上使用的最重要的协议了.Web服务,有网络功能的设备和网络计算的发展,都持续扩展了HTTP协议的角色,超越了用户使用的Web浏览器范畴,同时,也增加了需 ...