GCD Again 欧拉函数
2017-04-17 13:02
253 查看
Problem Description
Do you have spent some time to think and try to solve those unsolved problem after one ACM contest?
No? Oh, you must do this when you want to become a "Big Cattle".
Now you will find that this problem is so familiar:
The greatest common divisor GCD (a, b) of two positive integers a and b, sometimes written (a, b), is the largest divisor common to a and b. For example, (1, 2) =1, (12, 18) =6. (a, b) can be easily found by the Euclidean algorithm. Now I am considering a little more difficult problem:
Given an integer N, please count the number of the integers M (0<M<N) which satisfies (N,M)>1.
This is a simple version of problem “GCD” which you have done in a contest recently,so I name this problem “GCD Again”.If you cannot solve it still,please take a good think about your method of study.
Good Luck!
Input
Input contains multiple test cases. Each test case contains an integers N (1<N<100000000). A test case containing 0 terminates the input and this test case is not to be processed.
Output
For each integers N you should output the number of integers M in one line, and with one line of output for each line in input.
Sample Input
2
4
0
Sample Output
0
1
题意:算出1-n-1中与n不互素的数字的个数有多少
思路:欧拉函数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll eular(int n)
{
int N=sqrt(n+0.5);
ll re=n;
for(int i=2;i<=N;i++){
if(n%i==0){
while(n%i==0)
n=n/i;
}
re=re*(i-1)/i;
}
if(n>1)
re=re*(n-1)/n;
return re;
}
int main()
{
int n;
while(scanf("%d",&n)==1&&n>0)
printf("%lld\n",n-eular(n)-1);
}
Do you have spent some time to think and try to solve those unsolved problem after one ACM contest?
No? Oh, you must do this when you want to become a "Big Cattle".
Now you will find that this problem is so familiar:
The greatest common divisor GCD (a, b) of two positive integers a and b, sometimes written (a, b), is the largest divisor common to a and b. For example, (1, 2) =1, (12, 18) =6. (a, b) can be easily found by the Euclidean algorithm. Now I am considering a little more difficult problem:
Given an integer N, please count the number of the integers M (0<M<N) which satisfies (N,M)>1.
This is a simple version of problem “GCD” which you have done in a contest recently,so I name this problem “GCD Again”.If you cannot solve it still,please take a good think about your method of study.
Good Luck!
Input
Input contains multiple test cases. Each test case contains an integers N (1<N<100000000). A test case containing 0 terminates the input and this test case is not to be processed.
Output
For each integers N you should output the number of integers M in one line, and with one line of output for each line in input.
Sample Input
2
4
0
Sample Output
0
1
题意:算出1-n-1中与n不互素的数字的个数有多少
思路:欧拉函数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll eular(int n)
{
int N=sqrt(n+0.5);
ll re=n;
for(int i=2;i<=N;i++){
if(n%i==0){
while(n%i==0)
n=n/i;
}
re=re*(i-1)/i;
}
if(n>1)
re=re*(n-1)/n;
return re;
}
int main()
{
int n;
while(scanf("%d",&n)==1&&n>0)
printf("%lld\n",n-eular(n)-1);
}
相关文章推荐
- HDU 1787 GCD Again (欧拉函数)
- Relatives 和 GCD Again 【欧拉函数】
- GCD Again 欧拉函数
- GCD Again 欧拉函数
- GCD Again 欧拉函数
- GCD Again 欧拉函数
- GCD Again 欧拉函数
- GCD Again 欧拉函数
- hdoj 1787 GCD Again (欧拉函数模板 )
- 欧拉函数
- poj2154质因数分解+快速幂+欧拉函数解决
- 数论--欧拉函数的整理(悔惜晟)
- POJ 2407 Relatives 欧拉函数
- pku 2478 Farey Sequence 欧拉函数~~~
- poj 2773(欧拉函数)
- 欧拉函数
- (转)快速求欧拉函数
- 欧拉函数
- HDU2588_大区间_GCD_欧拉函数应用
- poj 2407 求欧拉函数值