(hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)

题目:

GCD Again

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

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

题目分析:

欧拉函数的简单应用。本体先使用phi(n)求出[1,n]中与n互质的元素的个数,然后再使用n-phi(n)求出[1,n]中与

n不互质的元素的个数就可以。最后还须要把它自己给减掉。也就是n-phi(n)-1.

这道题须要的须要注意的是:

1、在这里,我们还回想一下"互质"的定义:

互质,公约数仅仅有1的两个整数,叫做互质整数·公约数仅仅有1的两个自然数,叫做互质自然数,后者是前者的特殊情形·。

2、关于使用预处理的方式来求欧拉值  和  使用phi(n)来求欧拉值得两种方式的选择的个人考虑:

1)当n比較小 。同一个输入例子须要多次用到phi[i]时,这时能够考虑使用预处理的方式。假设当n比較大的时候仍使用这样的方式,非常可能会直接MLE,如这道题。

2)当n比較大,同一个输入例子仅仅须要使用一个phi[i]时,这是我们能够考虑使用调用phi(i)的方式。

代码例如以下:

#include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

typedef unsigned long long int longint;

longint phi(longint num) {
	longint sum = 1;
	for (long int i = 2; i <= sqrt((double long) num); i++) {
		if (num % i == 0) {
			while (num % i == 0) {
				sum *= i;
				num /= i;
			}
			sum /= i;
			sum *= (i - 1);
		}
	}

	if (num != 1) {
		sum *= (num - 1);
	}

	return sum;
}

int main(){
	int n;
	while(scanf("%d",&n)!=EOF,n){
		/**
		 * 最后为什么要减1呢?
		 * 由于这道题要求的是[1,n)中与n不互质的元素的个数,
		 * 须要把n自己给减掉.
		 */
		printf("%lld\n",n - phi(n) - 1);
	}

	return 0;
}
时间: 2024-12-09 20:10:01

(hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)的相关文章

(hdu step 2.1.6)找新朋友(欧拉函数的简单使用:求与n互质的元素的个数)

题目: 找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2788 Accepted Submission(s): 1307   Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有大

HDU 2588 GCD【欧拉函数的运用】

http://acm.hdu.edu.cn/showproblem.php?pid=2588 题意:输入s个数 输入n  m  表示从1到n的数与n的公约数大于m的数的个数 思路: 首先找出n的所有大于m的公约数k,然后求出每个对应的n/k的euler(欧拉函数)即小于n/k的数与n/k互质的个数,那么这些数与n/k互质且小于n/k,那么这些与n/k互质的数  乘以k之后那么就变成了与n公约数为k的数(k>m)   把所有的euler(n/k)相加即是答案    这是参考别人的思路的. #inc

bzoj 2818 GCD 数论 欧拉函数

bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sample Output 4 HINT hint对于样例(2,2),(2,4),(3,3),(4,2) 1<=N<=10^7 题解一(自己yy) phi[i]表示与x互质的数的个数 即gcd(x,y)=1 1<=y<x ∴对于x,y 若a为素数 则gcd(xa,

HDU 1695 GCD(欧拉函数+容斥原理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, y)有多少组,不考虑顺序. 思路:a = c = 1简化了问题,原问题可以转化为在[1, b/k]和[1, d/k]这两个区间各取一个数,组成的数对是互质的数量,不考虑顺序.我们让d > b,我们枚举区间[1, d/k]的数i作为二元组的第二位,因为不考虑顺序我们考虑第一位的值时,只用考虑小于i的情

hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 852 Accepted Submission(s): 259 Problem Descriptionan = X*an-1 + Y and Y mod (X-1) = 0.Your task is to calculate th

HDU 4002 Find the maximum(数论-欧拉函数)

Find the maximum Problem Description Euler's Totient function, φ (n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n . For example, as 1, 2, 4, 5, 7, and 8, are all less than

uva 11317 - GCD+LCM(欧拉函数+log)

题目链接:uva 11317 - GCD+LCM 题目大意:给定n,求出1~n里面两两的最大公约的积GCD和最小公倍数的积LCM,在10100进制下的位数. 解题思路:在n的情况下,对于最大公约数为i的情况又phi[n/i]次.求LCM就用两两乘积除以GCD即可. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; ty

HDU1695 GCD (欧拉函数+容斥原理)

求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). b和d分别除以k之后的区间里面,只需要求gcd(x, y) = 1就可以了,这样子求出的数的对数不变. 这道题目还要求1-3 和 3-1 这种情况算成一种,因此只需要限制x<y就可以了 只需要枚举x,然后确定另一个区间里面有多少个y就可以了.因此问题转化成为区间(1, d)里面与x互素的数的个数 先求出x的所有质因数,因此(1,d)区间里面是x的质因数倍数的

hdu 5152 A Strange Problem线段树+欧拉函数

*****************************************BC题解**********************************************************************1003 A Strange Problem 对于操作二,利用公式 当x >= Phi(C), A^x = A ^ (x%Phi(C) + Phi(C)) (mod C) 对于2333333这个模数来说,求18次欧拉函数后就变成了1,所以只需要保存19层第三次操作的加数