POJ2407 ---- Relatives
Time Limit: 1000MS Memory Limit: 65536K
Description
Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.
Input
There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.
Output
For each test case there should be single line of output answering the question posed above.
Sample Input
7
12
0
Sample Output
6
4
说白了这题就是求欧拉函数
φ
(
n
)
φ(n)
φ(n)
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
typedef long long ll;
ll phi[1000005];
void euler_table(int n)
{
int i,j;
memset(phi,0,sizeof(phi));
for (i=1;i<=n;i++)
phi[i]=i;
for (i=2;i<=n;i++)
{
if (phi[i]==i)
{
for (j=i;j<=n;j+=i)
{
phi[j]=phi[j]/i*(i-1);
}
}
}
}
int main()
{
int n;
//打表
euler_table(1000000);
int i;
ll sum; //一定要用ll类型,不然会WA
while(scanf("%d",&n) && n)
{
sum=0;
for(i=2 ; i<=n ; i++)
sum+=phi[i];
printf("%lld\n",sum);
}
return 0;
}
POJ2478 ---- Farey Sequence
Time Limit: 1000MS Memory Limit: 65536K
Description
The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few are
F2 = {1/2}
F3 = {1/3, 1/2, 2/3}
F4 = {1/4, 1/3, 1/2, 2/3, 3/4}
F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}
You task is to calculate the number of terms in the Farey sequence Fn.
Input
There are several test cases. Each test case has only one line, which contains a positive integer n (2 <= n <= 106). There are no blank lines between cases. A line with a single 0 terminates the input.
Output
For each test case, you should output one line, which contains N(n) ---- the number of terms in the Farey sequence Fn.
Sample Input
2
3
4
5
0
Sample Output
1
3
5
9
本题求 F ( n ) F(n) F(n),实际上 F ( n ) F(n) F(n)就是欧拉函数从 φ ( 2 ) φ(2) φ(2)加到 φ ( n ) φ(n) φ(n)
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
typedef long long ll;
ll phi[1000005];
void euler_table(int n)
{
int i,j;
memset(phi,0,sizeof(phi));
for (i=1;i<=n;i++)
phi[i]=i;
for (i=2;i<=n;i++)
{
if (phi[i]==i)
{
for (j=i;j<=n;j+=i)
{
phi[j]=phi[j]/i*(i-1);
}
}
}
}
int main()
{
int n;
//打表
euler_table(1000000);
int i;
ll sum; //一定要用ll类型,不然会WA
while(scanf("%d",&n) && n)
{
sum=0;
for(i=2 ; i<=n ; i++)
sum+=phi[i];
printf("%lld\n",sum);
}
return 0;
}
## POJ3090 ---- Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K
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
本题为POJ2478的变式,设欧拉函数从 φ ( 2 ) φ(2) φ(2)加到 φ ( n ) φ(n) φ(n)的和为sum,visible points的总个数就等于 s u m ∗ 2 + 3 sum*2+3 sum∗2+3 (+3是加上了(1,0), (0,1), (1,1) 三个点)
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int phi[1005];
void euler(int n)
{
int i,j;
for(i=1 ; i<=n ; i++)
phi[i]=i;
for(i=2 ; i<=n ; i++)
{
if(phi[i]==i)
{
for(j=i ; j<=n ; j+=i)
{
phi[j]=phi[j]/i*(i-1);
}
}
}
}
int main()
{
euler(1005);
int c,n;
scanf("%d",&c);
int i,cnt=0;
ll sum;
while(c--)
{
sum=0;
scanf("%d",&n);
cnt++;
if(n>1)
for(i=2 ; i<=n ; i++)
sum+=phi[i];
printf("%d %d %lld\n",cnt,n,sum*2+3);
}
return 0;
}