标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6379 | Accepted: 3803 |
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
思路:欧拉函数;有一个点的坐标为(x,y)那么可以写成gcd(x,y)(x/gcd,y/gcd)
那么这个点必定和(x/gcd,y/gcd)在同一直线上,那么x/gcd,与y/gcd互素所以求欧拉函数就行。
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<stdlib.h> 5 #include<string.h> 6 #include<queue> 7 #include<stack> 8 #include<math.h> 9 #include<vector> 10 using namespace std; 11 typedef long long LL; 12 int gcd(int n,int m); 13 int oula[2000]; 14 bool prime[2000]; 15 int ans[1000]; 16 int main(void) 17 { 18 int i,j,k; 19 int n,m; 20 scanf("%d",&k); 21 int an=0; 22 for(i=2; i<=100; i++) 23 { 24 if(!prime[i]) 25 { 26 for(j=i; (i*j)<=1000; j++) 27 { 28 prime[i*j]=true; 29 } 30 } 31 } 32 int ak=0; 33 for(i=2; i<=1000; i++) 34 { 35 if(!prime[i]) 36 { 37 ans[ak++]=i; 38 } 39 } 40 for(i=0; i<=1000; i++) 41 { 42 oula[i]=i; 43 } 44 for(i=0; i<ak; i++) 45 { 46 for(j=1; j*ans[i]<=1000; j++) 47 { 48 oula[ans[i]*j]/=ans[i]; 49 oula[ans[i]*j]*=ans[i]-1; 50 } 51 } 52 while(k--) 53 { 54 an++; 55 scanf("%d",&n); 56 int sum=0; 57 for(i=1; i<=n; i++) 58 { 59 sum+=oula[i]; 60 } 61 sum*=2;printf("%d %d ",an,n); 62 printf("%d\n",sum+1); 63 } 64 } 65 int gcd(int n,int m) 66 { 67 if(m==0) 68 { 69 return n; 70 71 } 72 else if(n%m==0) 73 { 74 return m; 75 } 76 else return gcd(m,n%m); 77 }
标签:
原文地址:http://www.cnblogs.com/zzuli2sjy/p/5534292.html