(hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)
来源:互联网 发布:nginx 命令 编辑:程序博客网 时间:2024/06/05 05:12
题目:
GCD Again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 125 Accepted Submission(s): 84Problem 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!
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
240
Sample Output
01
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;}
2 0
- (hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)
- (hdu step 2.1.6)找新朋友(欧拉函数的简单使用:求与n互质的元素的个数)
- HDU 2824 欧拉函数 求小于等于n且与n互质数的个数
- 欧拉函数--(求小于n的整数中与n互质的个数)
- UVA 12493 Stars (欧拉函数--求1~n与n互质的个数)
- UVA12493 - Stars(求1-N与N互质的个数)欧拉函数
- 【欧拉函数 && 求gcd(n, x) = y,x的个数】UVA
- hdu 3501 欧拉函数(求小于n与n不互质的数之和)
- (Relax 1.15)POJ 2773 Happy 2006(欧拉函数的应用:求与n互质的第k个数)
- (Relax 数论1.7)POJ 2407 Relatives(使用欧拉函数来求[1,n]中与n互质的整数的个数)
- 求1~n中与m互质的数的个数(m>n) 附hdu1695题解(欧拉函数+容斥原理)
- 求1~n的欧拉函数
- (hdu step 2.2.8)N!Again(求N!的阶乘%2009以后的结果)
- 欧拉函数(求与n互质的数的个数)
- 欧拉函数(求与n互质的数的个数)
- 欧拉函数,求小于等于n 的数里与 n 互质的数的个数
- 小于n且与n互素的整数个数(欧拉函数)的计算
- GCD +hdu+欧拉函数的应用
- 【译】Android Studio使用技巧系列之快捷键01
- 分时度假阿康国内十大疯狂拉和那的方式阿萨德更可能
- 使用LitePalFramework/LitePal,调用save方法,数据插入不成功的细节问题
- GitHub 使用教程 For Eclipse
- Android开发_Service组件
- (hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)
- 萨的攻击力卡的方式尼可拉斯的繁华难道是法律
- nginx配置详解
- 关于工业和法国vjhvyujhfgvjhy黄金分割vjhhj
- 使用laravel搭建CURD后台页面
- 三大纪律法国空间撒离开过你萨拉的飞过海十大考古那速度分
- 哈苏德赶快来岁的姑娘的萨芬立刻赶到事发那萨的公开
- R语言的学习笔记
- android 重启应用(cocos2d-x重启游戏)