数论 - 欧拉函数的运用 --- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // 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; } |
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3969260.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?