题目:

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;
}

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

  1. bzoj 2818 GCD 数论 欧拉函数

    bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

  3. hdu 1695 GCD(欧拉函数+容斥)

    Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...

  4. HDU 2588 GCD(欧拉函数)

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

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

    F - GCD Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Stat ...

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

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. HDU 1787 GCD Again(欧拉函数,水题)

    GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. hdu 4983 Goffi and GCD(欧拉函数)

    Problem Description Goffi is doing his math homework and he finds an equality on his text book: gcd( ...

  9. 题解报告:hdu 2588 GCD(欧拉函数)

    Description The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written ...

随机推荐

  1. 杂项:ESB接口

    ylbtech-杂项:ESB接口 ESB全称为Enterprise Service Bus,即企业服务总线.它是传统中间件技术与XML.Web服务等技术结合的产物.ESB提供了网络中最基本的连接中枢, ...

  2. [Apple开发者帐户帮助]三、创建证书(3)创建企业分发证书

    作为Apple Developer Enterprise Program的成员,您可以创建多个企业分发证书. 所需角色:帐户持有人或管理员. 在证书,标识符和配置文件中,从左侧的弹出菜单中选择iOS, ...

  3. Redis(六)-数据类型

    Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) string是redis最 ...

  4. jQuery获取及设置单选框、多选框、文本框

    获取一组radio被选中项的值 var item = $("input[@name=items][@checked]").val(); 获取select被选中项的文本 var it ...

  5. MyBatis动态条件、一对多、整合spring(二)

    输入映射和输出映射 Mapper.xml映射文件定义了操作数据库的sql,每一个sql是一个statement,映射文件是mybatis的核心. parameterType输入类型 1.传递简单类型 ...

  6. Android几种常见的多渠道(批量)打包方式介绍

    多渠道打包,主要是为了统计不同的渠道上包的下载数量,渠道越多,我们需要打的包数量越多,这个时候,我们没法去使用单纯的手动打包去一个一个的生成不同的渠道包,我们需要更高效的打包方式. 声明渠道方式一: ...

  7. VMware中linux安装jdk

    首先安装linux系统 如何将jdk安装包复制到linux中不做概述,可以使用xftp工具,或者Xshell,或者其他方式. 1.下载jdk包:本章使用的为后缀为tar.gz的文件(不需要安装),如j ...

  8. ajax 三级联动写法

    主页面代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  9. VMWare 安装Centos 6.9

    1.新建虚拟机 (1)点击文件-->新建虚拟机 (2)选择 自定义(高级)--> 下一步 (3)选择Workstation 12.0 --> 下一步 (4)选择 稍后安装操作系统 - ...

  10. C#获取窗口大小和位置坐标 GetWindowRect用法

    [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWi ...