poj 3060 Visible Lattice Points
http://poj.org/problem?id=3090
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6153 | Accepted: 3662 |
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
隐藏很深的一道欧拉函数题,观察下面这个图,将图分成上下两个三角形,观察下三角,横坐标从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的更多相关文章
- 数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points
Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5636 Accepted: ...
- POJ 3090 Visible Lattice Points 欧拉函数
链接:http://poj.org/problem?id=3090 题意:在坐标系中,从横纵坐标 0 ≤ x, y ≤ N中的点中选择点,而且这些点与(0,0)的连点不经过其它的点. 思路:显而易见, ...
- POJ 3090 Visible Lattice Points (ZOJ 2777)
http://poj.org/problem?id=3090 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1777 题目大意: ...
- poj 3090 Visible Lattice Points(离线打表)
这是好久之前做过的题,算是在考察欧拉函数的定义吧. 先把欧拉函数讲好:其实欧拉函数还是有很多解读的.emmm,最基础同时最重要的算是,¢(n)表示范围(1, n-1)中与n互质的数的个数 好了,我把规 ...
- POJ 3090 Visible Lattice Points 【欧拉函数】
<题目链接> 题目大意: 给出范围为(0, 0)到(n, n)的整点,你站在(0,0)处,问能够看见几个点. 解题分析:很明显,因为 N (1 ≤ N ≤ 1000) ,所以无论 N 为多 ...
- [poj] 3090 Visible Lattice Points
原题 欧拉函数 我们发现,对于每一个斜率来说,这条直线上的点,只有gcd(x,y)=1时可行,所以求欧拉函数的前缀和.2*f[n]+1即为答案. #include<cstdio> #def ...
- POJ 3090 Visible Lattice Points | 其实是欧拉函数
题目: 给一个n,n的网格,点可以遮挡视线,问从0,0看能看到多少点 题解: 根据对称性,我们可以把网格按y=x为对称轴划分成两半,求一半的就可以了,可以想到的是应该每种斜率只能看到一个点 因为斜率表 ...
- poj 3090 Visible Lattice Points 法利系列||通过计
因为图像关于对角线对称.所以我们仅仅看下三角区域. 将x轴看做分母,被圈的点看成分子 依次是{1/2},{1/3,1/2},{1/4,3/4},{1/5,2/5,3/5,4/5} 写成前缀和的形式就是 ...
- 【POJ】3090 Visible Lattice Points(欧拉函数)
Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7705 Accepted: ...
随机推荐
- LA 3882 And Then There Was One
解题思路:分析要好久,懒得分析了,贴了某大牛的的分析,代码就是我自己写的. N个数排成一圈,第一次删除m,以后每k个数删除一次,求最后一被删除的数. 如果这题用链表或者数组模拟整个过程的话,时间复杂度 ...
- 文件IO一些注意的地方
两个各自独立的进程各自打开同一个文件,则每个进程都有各自的文件表项.这是因为每个进程都有它自己对该文件的当前偏移量.但是对一个给定的文件只有一个v节点表项.lseek()只修改文件表项中的当前文件偏移 ...
- MAC OSX 下安装Cscope
续前文,搞定CTAGS之后,需要被搞定的是cscope,依旧是上网拖一把,具体过程如下 #1 下载cscope最新版本 http://cscope.sourceforge.net/#downloa ...
- LA 2995 Image Is Everything 立方体成像 World Final 2004
有一个 n * n * n 的立方体,其中一些单位立方体已经缺失(剩下部分不一定连通).每个单位立方体重 1 克,且被涂上单一的颜色(即 6 个面的一颜色相同).给出前.左.后.右.顶.底 6 个视图 ...
- 【转】NSArray排序方法
原文网址:http://www.cnblogs.com/xiaobaizhu/archive/2013/06/05/3119983.html 从网上查的,非常方便的排序api,功能也很强大 1.sor ...
- xcode的ios工程目录结构
目录结构: a.supporting files: main.m和资源文件 xxx-info.plist:包含应用程序相关属性列表,如版本,程序名等 .pch文件:预编译头文件,相当于MFC里的std ...
- C++编程常见错误
1.成员变量要记得在构造函数中初始化 2.还是初始化!初始化!初始化!
- Velocity+Java较全教程
一.安装myEclipse 二.安装velocity的eclipse插件: http://www.oschina.net/p/veloeclipse(介绍) 方法1(现在基本上非常慢)http://p ...
- datawindow.net 动态按条件汇总字段值
string xblx = dw1.GetItemString(row, "c_xblx"); string xbid = dw1.GetItemString(row, " ...
- jQuery Mobile中文手册:开发入门
jQuery Mobile 以“Write Less, Do More”作为目标,为所有的主流移动操作系统平台提供了高度统一的 UI 框架:jQuery 的移动框架可以让你为所有流行的移动平台设计一个 ...