一、题目

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

二、题意分析

这题题意比较好懂,给定一个数N,代表这个二维平面里在第一象限的一个正方形的边长,就可以得到(N+1)*(N+1)个整点。然后问在除原点的外的(N+1)^2-1个点中,有多少个点与原点相连后,两点连成的线段之间是木有整点的。

补充个营养:看过《挑战程序设计竞赛》的同学肯定知道,给定一个直角三角形的两条边的边长a,b,那么gcd(a,b)+1就代表这条边上的整点数目。除去两个端点那么gcd(a,b)-1=0不就是这题所要满足的吗,也就是gcd(a,b)=1。

转换:这题通过上面的知识就可以转换成求一个数N的欧拉函数值。然后我们分析一下,N=1的时候一共4个点中,除去原点有3个点满足,结果为F[1] = 3。N=2的时候一共9个点,N=1时满足的点在此时也必然满足。这里需要注意的是,因为是在二维平面,那么最外面一条边上有φ(2)个点满足的话,那么另外一条边上也有φ(2)个点满足,那么就是 F[1] + 2*φ(2)。后面的原理相同,就得到了递推式

F[N] = F[N-1] + 2*φ(N),其中F[1] = 3

然后先线性筛法打表求欧拉函数的值,再用另外一个数组递推即可。

三、代码

#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1e3+5;
int Prime[MAXN], Phi[MAXN], nPrime;
long long Ans[MAXN]; void Euler()
{
memset(Phi, 0, sizeof(Phi));
Phi[1] = 1;
nPrime = 0;
for(int i = 2; i < MAXN; i++)
{
if(!Phi[i])
{
Phi[i] = i-1;
Prime[nPrime++] = i;
}
for(int j = 0; j < nPrime && i*Prime[j] < MAXN; j++)
{
if(i%Prime[j])
{
Phi[i*Prime[j]] = Phi[i]*(Prime[j] - 1);
}
else
{
Phi[i*Prime[j]] = Phi[i]*Prime[j];
break;
}
}
}
} void solve()
{
Euler();
Ans[1] = 3;
for(int i = 2; i < MAXN; i++)
{
Ans[i] = Ans[i-1] + Phi[i]*2;
}
} int main()
{
int T, N;
cin >> T;
solve();
for(int i = 1; i <= T; i++)
{
cin >> N;
cout << i << ' ' << N << ' ' << Ans[N] << endl;
}
return 0;
}

  

POJ_3090 Visible Lattice Points 【欧拉函数 + 递推】的更多相关文章

  1. POJ3090 Visible Lattice Points 欧拉函数

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

  2. POJ 3090 Visible Lattice Points 欧拉函数

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

  3. UVA 11426 (欧拉函数&&递推)

    题意:给你一个数N,求N以内和N的最大公约数的和 解题思路: 一开始直接想暴力做,4000000的数据量肯定超时.之后学习了一些新的操作. 题目中所要我们求的是N内gcd之和,设s[n]=s[n-1] ...

  4. [poj 3090]Visible Lattice Point[欧拉函数]

    找出N*N范围内可见格点的个数. 只考虑下半三角形区域,可以从可见格点的生成过程发现如下规律: 若横纵坐标c,r均从0开始标号,则 (c,r)为可见格点 <=>r与c互质 证明: 若r与c ...

  5. POJ3090 Visible Lattice Points 欧拉筛

    题目大意:给出范围为(0, 0)到(n, n)的整点,你站在原点处,问有多少个整点可见. 线y=x和坐标轴上的点都被(1,0)(0,1)(1,1)挡住了.除这三个钉子外,如果一个点(x,y)不互质,则 ...

  6. BNU 12846 LCM Extreme 最小公倍数之和(线性欧拉筛选+递推)

    LCM Extreme Time Limit: 3000ms Memory Limit: 131072KB   This problem will be judged on UVALive. Orig ...

  7. POJ3090 Visible Lattice Points

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

  8. hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数

    hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...

  9. 【转】UVALive 5964 LCM Extreme --欧拉函数

    题目大意:求lcm(1,2)+lcm(1,3)+lcm(2,3)+....+lcm(1,n)+....+lcm(n-2,n)+lcm(n-1,n)解法:设sum(n)为sum(lcm(i,j))(1& ...

随机推荐

  1. 张超超OC基础回顾02_成员变量(属性),局部变量,全局变量的区别

    成员变量: 写在类声明的大括号中的变量, 我们称之为 成员变量(属性, 实例变量) 成员变量只能通过对象来访问 注意: 成员变量不能离开类, 离开类之后就不是成员变量 成员变量不能在定义的同时进行初始 ...

  2. github如何上传代码

    别人写的太好了,没必要重写.备份给自己参看. 1.https://www.cnblogs.com/zlxbky/p/7727895.html 2.https://blog.csdn.net/pql92 ...

  3. Browsersync 简介 and 使用

    简介 省时的浏览器同步测试工具,Browsersync能让浏览器实时.快速响应您的文件更改(html.js.css.sass.less等)并自动刷新页面. 曾经我们每改一次的代码,都需要手动去刷新一次 ...

  4. cbv+resful+APIView源码分析

    CBV源码分析 1概念:什么是cbv和fbv 已经什么是API class bass View ---基于类的视图 function bass View ---基于函数的视图 API(Applicat ...

  5. 设计模式04: Factory Methord 工厂方法模式(创建型模式)

    Factory Methord 工厂方法模式(创建型模式) 从耦合关系谈起耦合关系直接决定着软件面对变化时的行为 -模块与模块之间的紧耦合使得软件面对变化时,相关的模块都要随之变更 -模块与模块之间的 ...

  6. 系统架构一:snmp+mrtg服务器监控

    //@author:yuan<turing_zhy@163.com> 码字不易,转载请注明出处 #================================== 开始,服务器准备   ...

  7. ICallbackEventHandler使用

    后端:页面需继承ICallbackEventHandler protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack ...

  8. swagger接口文档

    1 在Visual Studio 中创建一个Asp.NET  WebApi 项目,项目名:Com.App.SysApi(本例创建的是 .net 4.5 框架程序) 2  打开Nuget 包管理软件,查 ...

  9. NSSet集合

    前言 NSSet:集合 NSSet 集合跟数组差不多,但 Set 集合不能存放相同的对象,它是一组单值对象的集合,被存放进集合中的数据是无序的,它可以是可变的,也可以是不变的. Xcode 7 对系统 ...

  10. 计算机基础知识和tcp详解

    计算机基础知识 作为应用软件开发程序员是写应用软件的,而应用软件必须应用在操作系统之上,调用操作系统接口,由操作系统控制硬件 比如客户端软件想要基于网络发送一条消息给服务端软件,流程是: 1.客户端软 ...