Visible Lattice Points
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8397   Accepted: 5144

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

刚开始脑子简单去暴力循环。。当然是T掉啦!

原来一直不明白欧拉函数,f(n)代表小于n的正整数中与n互质的数的数目。。就是这个题嘛。。记住了。

 #include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include<iostream>
#include <cmath>
#include<string>
#define ll long long
#define dscan(a) scanf("%d",&a)
#define mem(a,b) memset(a,b,sizeof a)
using namespace std;
#define MAXL 1105
#define maxn 1000005
int f[MAXL];
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>'') {if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<='') {x=*x+ch-'';ch=getchar();}
return x*f;
}
int p[MAXL], pNum, phi[MAXL];
void ola()
{
int i, j;
for(i = ; i < MAXL; i++) {
if(!f[i]) { p[pNum++] = i; phi[i] = i-;}
for(j = ; j < pNum && p[j] * i < MAXL; j++ ) {
f[p[j]*i] = ;
if(i % p[j] == ){
phi[i*p[j]] = phi[i] * p[j];
break;
}
else phi[i*p[j]] = phi[i] *(p[j] - );
}
}
}
int main()
{
int n,c;
cin>>n;int hh=;
ola();
while(n--)
{
int c=read();
phi[]=;
cout<<hh++<<" "<<c<<" ";
ll ans=;
for(int i=;i<=c;++i) ans+=phi[i];
cout<<ans*+<<endl;
}
}

POJ 3090的更多相关文章

  1. POJ 3090 Visible Lattice Points 欧拉函数

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

  2. poj 3090 &amp;&amp; poj 2478(法雷级数,欧拉函数)

    http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式非常easy:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2 ...

  3. 【POJ 3090】 Visible Lattice Points

    [题目链接] http://poj.org/problem?id=3090 [算法] 通过观察发现,在这个平面直角坐标系中,除了(1,1),(1,0)和(0,1),所有可见点的横纵坐标互质 那么,问题 ...

  4. POJ 3090 Visible Lattice Points (ZOJ 2777)

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

  5. 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)

    题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...

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

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

  7. POJ 3090 (欧拉函数) Visible Lattice Points

    题意: UVa 10820 这两个题是同一道题目,只是公式有点区别. 给出范围为(0, 0)到(n, n)的整点,你站在原点处,问有多少个整点可见. 对于点(x, y), 若g = gcd(x, y) ...

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

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

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

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

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

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

随机推荐

  1. C++大数问题

    1.大数的加法 语法:add(char a[],char b[],char s[]); 参数: a[]:被加数,用字符串表示,位数不限 b[]:加数,用字符串表示,位数不限 s[]:结果,用字符串表示 ...

  2. <转载>一般筛法和快速线性筛法求素数

    素数总是一个比较常涉及到的内容,掌握求素数的方法是一项基本功. 基本原则就是题目如果只需要判断少量数字是否为素数,直接枚举因子2 ..N^(0.5) ,看看能否整除N. 如果需要判断的次数较多,则先用 ...

  3. CF873B Balanced Substring (前缀和)

    CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...

  4. 题解 P3367 【【模板】并查集】

    #include<iostream> #include<cstdio> using namespace std; int n,m,x,y,z; ]; //f[i]表示i的祖先 ...

  5. spring data事务

    事务在spring data中的使用 1:事务一般在service层.因为一个service方法可能会多次调用不同的dao,为了保证事务的完整性,那么多次的dao都放到一个方法里面 2:读的时候可以不 ...

  6. node 中的redis使用

    1.创建sql.config.js 配置文件 : var redis_db = { ", "URL":"127.0.0.1", "OPTIO ...

  7. session在C#一般处理程序的调用方式

    在C#中有一个一般处理程序,可以快速地进行一些逻辑运算等功能,但在这个页面上,不能直接选择使用session进行页面间的值的传递,如何使得页面可以使用session呢 在页面开头写上 using Sy ...

  8. tp5查询

    TP5的EXP.批量查询.聚合查询等. <!--more--> //使用EXP条件表达式,表示后面是原生的SQL表达式 $result = Db::table('think_inno')- ...

  9. Ecshop之ajax修改表里的状态(函数化处理)

    目录 功能: 效果: 思路: 页面里 控制器里 功能: `点击图片,修改表里的状态值` 效果: 思路: 页面里在img里点绑定onclick件事,调用js函数listTable.toggle oncl ...

  10. Python知识点入门笔记——特色数据类型(元组)

    元组(tuple)是Python的另一种特色数据类型,元组和列表是相似的,可以存储不同类型的数据,但是元组是不可改变的,创建后就不能做任何修改操作. 创建元组 用逗号隔开的就是元组,但是为了美观和代码 ...