HDOJ GCD Again 1787【欧拉函数】

GCD Again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2625    Accepted Submission(s): 1099

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

Author

lcy

Source

2007省赛集训队练习赛(10)_以此感谢DOOMIII

Recommend

lcy   |   We have carefully selected several similar problems for you:  1788 1695 1573 2824 1286

题意:

求小于n的gcd(i,n)大于1的个数

思路 : 欧拉函数直接求gcd(i,n)==1的个数  用n减即可,注意小于n,故再减去1.

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>

using namespace std;

int p(int n)
{
	int ans=n;
	for(int i=2;i*i<=n;i++){
		if(n%i==0){
			ans-=ans/i;
			while(n%i==0){
				n/=i;
			}
		}
	}
	if(n>1)ans-=ans/n;
	return ans;
}
int main()
{
	int n;
	while(scanf("%d",&n),n)
	printf("%d\n",n-p(n)-1);
    return 0;
}

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-10-16 03:04:31

HDOJ GCD Again 1787【欧拉函数】的相关文章

hdoj 1787 GCD Again【欧拉函数】

GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2673    Accepted Submission(s): 1123 Problem Description Do you have spent some time to think and try to solve those unsolved problem af

HDOJ 1787 GCD Again(欧拉函数)

GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2611    Accepted Submission(s): 1090 Problem Description Do you have spent some time to think and try to solve those unsolved problem a

UVA 11426 - GCD - Extreme (II) 欧拉函数-数学

Given the value of N, you will have to ?nd the value of G. The de?nition of G is given below:G =i<N∑i=1j∑≤Nj=i+1GCD(i, j)Here GCD(i, j) means the greatest common divisor of integer i and integer j.For those who have trouble understanding summation no

UVA11426 GCD - Extreme (II)---欧拉函数的运用

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=473&problem=2421&mosmsg=Submission+received+with+ID+13800900 Given the value of N, you will have to ?nd the value of G. The de?nition

UVa 11426 GCD - Extreme (II) (欧拉函数应用&#183;O(N*logN))

题意  令  G(n) = sum{gcd(i, j) | 0 < i < n, i < j <= n}  给你一个n  输出G(n) 令 F(n) = sum{gcd(i, n) | 0 < i < n}  那么有递推式 G(n) = G(n-1) + F(n) , G(0)  = 0  也就是说只用求出F(n) 就能递推求出 G(n)了  而求F(n)就比较容易了 对于i  设 x < i , gcd(x,i) = 1 即x, n 互质 则  gcd(2*x,

HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题目解析: Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. 题目又说a==c==1,所以就是求[1,b]与[1,d]中gcd等于k的个数,因为若gcd(x,y)==z,那么gcd(x/z,y/z)==1,又因为不是z的倍数的肯定不是,所以不是z的倍数的可以直接去

HDU1787 GCD Again【欧拉函数】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1787 题目大意: 给你一个整数N,求范围小于N中的整数中,与N的最大公约数大于1的整数的个数. 思路: 典型的欧拉函数变形.欧拉函数φ(N)是用来求小于N的整数中,与N的最大公约数为1的数的个数. 那么此题的答案ans = N - φ(N) - 1. AC代码: #include<iostream> #include<algorithm> #include<cstdio>

luogu2658 GCD(莫比乌斯反演/欧拉函数)

link 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 1<=N<=10^7 (1)莫比乌斯反演法 发现就是YY的GCD,左转YY的GCD粘过来就行 代码太丑,没开O2 TLE5个点 #include <cstdio> #include <functional> using namespace std; const int fuck = 10000000; int prime[10000010], tot; bool v

UVA 11426 GCD - Extreme (II) 欧拉函数

分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include <vector> #include <cmath> #include <map> #include <queue> #include <algorithm> #include <cstring> using namespace std;

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 af