hdu 1787(欧拉函数)

GCD Again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2874    Accepted Submission(s): 1240

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

水题一枚

#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long LL;
LL phi(LL x)
{
    LL ans=x;
    for(LL i=2; i*i<=x; i++)
        if(x%i==0)
        {
            ans=ans/i*(i-1);
            while(x%i==0) x/=i;
        }
    if(x>1)
        ans=ans/x*(x-1);
    return ans;
}

int main(){
    LL n;
    while(scanf("%lld",&n)!=EOF,n){
        printf("%lld\n",n-phi(n)-1);
    }
}
时间: 2024-10-13 03:25:09

hdu 1787(欧拉函数)的相关文章

hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不会 就自己写了个容斥搞一下(才能维持现在的生活) //别人的题解https://blog.csdn.net/luyehao1/article/details/81672837 #include <iostream> #include <cstdio> #include <cstr

hdu 3307(欧拉函数+好题)

Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1071    Accepted Submission(s): 323 Problem Description an = X*an-1 + Y and Y mod (X-1) = 0.Your task is to cal

HDU 1286 欧拉函数。

[科普]什么是BestCoder?如何参加? 找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8077    Accepted Submission(s): 4250 Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是

hdu 4983(欧拉函数)

Goffi and GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 992    Accepted Submission(s): 336 Problem Description Goffi is doing his math homework and he finds an equality on his text book: g

hdu 2824(欧拉函数)

The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5235    Accepted Submission(s): 2225 Problem Description The Euler function phi is an important kind of function in number theo

hdu 2588 欧拉函数

两个数的gcd为d,其实就是将这两个数同除以d后互质.本题中n是固定的,x是小于等于n的数,很容易想到可以枚举n的约数求出(n除以约数)的欧拉函数的和即是答案. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 typedef long long ll; 7 8 int euler_phi( int n ) 9 { 10 int ans =

hdu 3501 欧拉函数

容易想到容斥原理,但是结合欧拉函数的公式,我们得到: 小于n且与n互质的数的和为:n * phi(n) / 2 于是问题迎刃而解. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 typedef long long ll; 7 const int MOD = 1000000007; 8 9 int euler_phi( int n ) 10

hdu 4002 欧拉函数

题意:求1-n内最大的x/phi(x) 通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数.φ(1)=1(唯一和1互质的数就是1本身). 因此含质因数最多的即为所求,打表求出前n个积,之后找到比自己小的最大积 大数打表 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #i

hdu 4002 欧拉函数 2011大连赛区网络赛B

题意:求1-n内最大的x/phi(x) 通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数.φ(1)=1(唯一和1互质的数就是1本身). 因此含质因数最多的即为所求,打表求出前n个积,之后找到比自己小的最大积 大数打表 2015-07-27:到此一游 1 #include<cstdio> 2 #include<iostream> 3 #include<al