Relatives

AC代码

Relatives

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16186   Accepted: 8208

Description

Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.

Input

There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.

Output

For each test case there should be single line of output answering the question posed above.

Sample Input

  1. 7
  2. 12
  3. 0

Sample Output

  1. 6
  2. 4

Source

欧拉函数模板题

AC代码

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <math.h>
  6. #include <limits.h>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <vector>
  11. #include <set>
  12. #include <string>
  13. #define ll long long
  14. #define ms(a) memset(a,0,sizeof(a))
  15. #define pi acos(-1.0)
  16. #define INF 0x3f3f3f3f
  17. const double E=exp(1);
  18. using namespace std;
  19. const int maxn=1e6+10;
  20. int a[maxn];
  21. int b[maxn];
  22. //欧拉函数公式:
  23. //φ(N)=N*(1-1/P1)*(1-1/P2)*...*(1-1/Pn).
  24. int main(int argc, char const *argv[])
  25. {
  26. int n;
  27. while(cin>>n&&n)
  28. {
  29. ms(b);
  30. ll ans=n;
  31. int vis=n;
  32. for(int i=2;i*i<=vis;i++)
  33. {
  34. if(vis%i==0)
  35. {
  36. ans=ans/i*(i-1);
  37. while(vis%i==0)
  38. {
  39. vis/=i;
  40. }
  41. }
  42. }
  43. if(vis>1)
  44. ans=ans/vis*(vis-1);
  45. cout<<ans<<endl;
  46. }
  47. return 0;
  48. }
  49. // 两种求欧拉函数的方法:
  50. //
  51. // ******************第一种直接求解****************
  52. // long long Euler(long long x)
  53. // {
  54. // int res = x,a = x;
  55. // for(int i=2;i*i<=a;i++)
  56. // {
  57. // if(a%i==0)
  58. // {
  59. // res = res/i*(i-1);//res -= res/i;
  60. // while(a%i==0)a/=i;
  61. // }
  62. // }
  63. // if(a>1)res =res/a*(a-1);//res -= res/a;
  64. // return res;
  65. // }
  66. // ******************第二种打表求解****************
  67. // #define Max 1000001
  68. // int euler[Max];
  69. // void Euler()
  70. // {
  71. // euler[1]=1;
  72. // for(int i=2;i<Max;i++)
  73. // euler[i]=i;
  74. // for(int i=2;i<Max;i++)
  75. // if(euler[i]==i)
  76. // for(int j=i;j<Max;j+=i)
  77. // euler[j]=euler[j]/i*(i-1);//先进行除法是为了防止中间数据的溢出
  78. // }

POJ 2407:Relatives(欧拉函数模板)的更多相关文章

  1. POJ 2407 Relatives(欧拉函数)

    题目链接 题意 : 求小于等于n中与n互质的数的个数. 思路 : 看数学的时候有一部分是将欧拉函数的,虽然我没怎么看懂,但是模板我记得了,所以直接套了一下模板. 这里是欧拉函数的简介. #includ ...

  2. POJ 2407 Relatives 欧拉函数题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  3. POJ2407–Relatives(欧拉函数)

    题目大意 给定一个正整数n,要求你求出所有小于n的正整数当中与n互质的数的个数 题解 欧拉函数模板题~~~因为n过大~~~所以直接用公式求 代码: #include<iostream> # ...

  4. UVA 10820 欧拉函数模板题

    这道题就是一道简单的欧拉函数模板题,需要注意的是,当(1,1)时只有一个,其他的都有一对.应该对欧拉函数做预处理,显然不会超时. #include<iostream> #include&l ...

  5. hdu3501Calculation 2——欧拉函数模板

    题目: Problem Description Given a positive integer N, your task is to calculate the sum of the positiv ...

  6. poj2407(欧拉函数模板)

    sqrt(n)复杂度 欧拉函数模板 #include <iostream> #include <cstdio> #include <queue> #include ...

  7. 数论 - 欧拉函数模板题 --- poj 2407 : Relatives

    Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11372   Accepted: 5544 Descri ...

  8. 找新朋友 HDU - 1286 欧拉函数模板题

    题意: 求出来区间[1,n]内与n互质的数的数量 题解: 典型的欧拉函数应用,具体见这里:Relatives POJ - 2407 欧拉函数 代码: 1 #include<stdio.h> ...

  9. POJ 2480 (约数+欧拉函数)

    题目链接: http://poj.org/problem?id=2480 题目大意:求Σgcd(i,n). 解题思路: 如果i与n互质,gcd(i,n)=1,且总和=欧拉函数phi(n). 如果i与n ...

随机推荐

  1. 使用SFTP工具相关问题

    1.使用SFTP工具,填写ip,端口都正确但是连接不上?         答:请统一使用 filezilla工具进行连接,环境搭建使用该工具进行测试和使用.           2.使用SFTP工具访 ...

  2. C#将集合和Json格式互相转换的几种方式

    1.使用微软自带的System.Web.Extensions.dll转换,该DLL文件一般存在于如下路径:c:\Program Files\Reference Assemblies\Microsoft ...

  3. TestNG 101

    最近看了点TestNG,做个入门笔记 0.Maven + TestNG 0a. 创建Maven 项目,pom中添加依赖(可能还需要安装TestNG插件 <dependencies> < ...

  4. Myeclipse2016安装Aptana

    Myeclipse2016安装Aptana 想装个Aptana,装了半天,网上说的什么links方式啊,在线方式啊,都是什么的浮云. 所以自己来写个安装教程. 一.Aptana简要介绍 Aptana有 ...

  5. Java成员变量和局部变量

    Java成员变量和局部变量 一.成员变量和局部变量 二.static关键字 三.成员变量和静态变量区别 四.main函数 五.静态函数什么时候用 六.静态代码块 七.构造代码块 构造代码块先于构造函数 ...

  6. mysql 视图,事务,存储过程,触发器

    一 视图 视图是一个虚拟表(非真实存在),是跑到内存中的表,真实表是硬盘上的表.使用视图我们可以把查询过程中的临时表摘出来,保存下来,用视图去实现,这样以后再想操作该临时表的数据时就无需重写复杂的sq ...

  7. laravel 异常深度解析

    一.前言 做一件事,不仅要知其然,更要知其所以然.大部分的人都生活在别人设计的世界里,不是没有能力去更深一层,更进一步,而是因为自己懒得去思考.也许自己现在要做的就是:不要让自己舒服吧. 二.正题 1 ...

  8. 浅谈Linux

    Linux系统最初由芬兰赫尔辛基大学的Andrew S.Tanenbaum写的MINIX操作系统演变而来,这是一个小型操作系统,主要用于教学,1991年1月,Tanenbaum的学生Linus Tor ...

  9. IOS8-人机界面指南

    [ISUX转译]iOS 8人机界面指南(一):UI设计基础 糖箔糊2014.09.23 文章索引 1.1 为iOS而设计(Designing for iOS) 1.1.1 以内容为核心(Defer t ...

  10. JavaScript学习总结(九)——Javascript面向(基于)对象编程

    一.澄清概念 1.JS中"基于对象=面向对象" 2.JS中没有类(Class),但是它取了一个新的名字叫“原型对象”,因此"类=原型对象" 二.类(原型对象)和 ...