数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5636 | Accepted: 3317 |
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
Source
Mean:
在第一象限中,输入一个n,然后要你统计在(0<=x<=n,0<=y<=n)的范围内,有多少可视点。
所谓的可视点,即:从(0,0)出发到达(x1,y1),中间未与任何整点相交的点。
analyse:
通过分析,我们会发现:只要x和y互质,那么(x,y)就是可视点。我们只要求得[0,0]~[x,y]内满足x和y互质的点(x,y)的个数,那么问题就可迎刃而解。欧拉函数就是用来解决小于n的数中有多少个数与n互质。
Time complexity:O(n)
Source code:
// Memory Time
// 1347K 0MS
// by : Snarl_jsb
// 2014-09-12-22.35
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1000010
#define LL long long
using namespace std; int gcd(int a,int b){
return b?gcd(b,a%b):a;
} inline int lcm(int a,int b){
return a/gcd(a,b)*b;
} int eular(int n) ////求1..n-1中与n互质的数的个数
{
int ret=1,i;
for (i=2;i*i<=n;i++)
if (n%i==0){
n/=i,ret*=i-1;
while (n%i==0)
n/=i,ret*=i;
}
if (n>1)
ret*=n-1;
return ret;
} int main()
{
// freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
// freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
int t,Cas=1;
cin>>t;
while(t--)
{
int n;
cin>>n;
LL ans=0;
for(int i=1;i<=n;i++)
{
ans+=eular(i);
}
printf("%d %d %d\n",Cas++,n,ans*2+1);
}
return 0;
}
数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points的更多相关文章
- 数论 - 欧拉函数模板题 --- poj 2407 : Relatives
Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11372 Accepted: 5544 Descri ...
- POJ 3090 Visible Lattice Points 【欧拉函数】
<题目链接> 题目大意: 给出范围为(0, 0)到(n, n)的整点,你站在(0,0)处,问能够看见几个点. 解题分析:很明显,因为 N (1 ≤ N ≤ 1000) ,所以无论 N 为多 ...
- POJ 3090 Visible Lattice Points | 其实是欧拉函数
题目: 给一个n,n的网格,点可以遮挡视线,问从0,0看能看到多少点 题解: 根据对称性,我们可以把网格按y=x为对称轴划分成两半,求一半的就可以了,可以想到的是应该每种斜率只能看到一个点 因为斜率表 ...
- poj 3090 Visible Lattice Points(离线打表)
这是好久之前做过的题,算是在考察欧拉函数的定义吧. 先把欧拉函数讲好:其实欧拉函数还是有很多解读的.emmm,最基础同时最重要的算是,¢(n)表示范围(1, n-1)中与n互质的数的个数 好了,我把规 ...
- [poj] 3090 Visible Lattice Points
原题 欧拉函数 我们发现,对于每一个斜率来说,这条直线上的点,只有gcd(x,y)=1时可行,所以求欧拉函数的前缀和.2*f[n]+1即为答案. #include<cstdio> #def ...
- 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 法利系列||通过计
因为图像关于对角线对称.所以我们仅仅看下三角区域. 将x轴看做分母,被圈的点看成分子 依次是{1/2},{1/3,1/2},{1/4,3/4},{1/5,2/5,3/5,4/5} 写成前缀和的形式就是 ...
- 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)
题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...
随机推荐
- Oracle Essbase入门系列(二)
本篇开始会一个三口之家的家庭财务数据库为例,讲述Essbase的功能和开发.为了说明EPM应用程序的管理和开发过程,会绕一些弯路,不使用EAS,而尽量用EPMA. 创建应用程序 首先登陆到Worksp ...
- Codeforces Round #384 (Div. 2) A. Vladik and flights 水题
A. Vladik and flights 题目链接 http://codeforces.com/contest/743/problem/A 题面 Vladik is a competitive pr ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- C# 个人常用代码积累
/// <summary> /// TextBox限制只能输入十六进制,且只能输入6个 /// </summary> /// <param name="send ...
- 定时从远程的数据库中取数据,然后把取出来的数据插入或更新本地的oracle数据库的表
最近项目中有一种需求: 大致需求是这样的 通过给定的 用户名和密码 要定时从远程的数据库中取数据,然后把取出来的数据插入或更新本地的oracle数据库的表 项目的结构式struts1 hibernat ...
- 深入剖析 redis AOF 持久化策略
本篇主要讲的是 AOF 持久化,了解 AOF 的数据组织方式和运作机制.redis 主要在 aof.c 中实现 AOF 的操作. 数据结构 rio redis AOF 持久化同样借助了 struct ...
- SQL语句调优-基础知识准备
当确定了应用性能问题可以归结到某一个,或者几个耗时资源的语句后,对这些语句进行调优,就是数据库管理员或者数据库应用程序开发者当仁不让的职责了.语句调优是和数据库打交道的必备基本功之一. 当你面对一个“ ...
- C#利用NOPI处理Excel的代码
using System; using System.Data; using System.IO; using System.Text; using System.Web; using NPOI.HP ...
- Git回滚远程版本
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “房子是租的 但生活不是” 1.故事的开始 远程master分支下代码被不小心提交了很多垃圾代码 ...
- Guardian of Decency(二分图)
Guardian of Decency Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...