题目链接

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

7

12

0

Sample Output

6

4

分析:

求一个数的所有的小于该数且与该数互质的数的个数,也就是欧拉函数的应用。

直接套欧拉函数公式,即将n素分解后有n=p1k1*p2k2…pm^km,则euler(n)=n(1-1/p1)(1-1/p2)…(1-1/pm) 。

代码:

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
ll get_euler(ll n)//欧拉函数的应用
{
ll ans=n;
for(ll i=2; i*i<=n; i++)
{
if(n%i==0)
{
ans=ans/i*(i-1);
while(n%i==0)
n/=i;
}
}
if(n>1)
ans=ans/n*(n-1);
return ans;
}
int main()
{
ll n;
while(scanf("%lld",&n),n)
printf("%lld\n",get_euler(n));
return 0;
}

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. POJ 2480 (约数+欧拉函数)

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

  4. Poj 2478-Farey Sequence 欧拉函数,素数,线性筛

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14291   Accepted: 5647 D ...

  5. poj2407 Relatives 欧拉函数基本应用

    题意很简单 就是欧拉函数的定义: 欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) .题目求的就是φ(n) 根据 通式:φ(x)=x*(1-1/p1)*(1-1/ ...

  6. POJ2407–Relatives(欧拉函数)

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

  7. poj 2773 利用欧拉函数求互质数

    题意:找到与n互质的第 k个数 开始一看n是1e6 敲了个暴力结果tle了,后来发现k达到了 1e8 所以需要用到欧拉函数. 我们设小于n的 ,与n互质的数为  (a1,a2,a3.......a(p ...

  8. poj 2480 (欧拉函数应用)

    点击打开链接 //求SUM(gcd(i,n), 1<=i<=n) /* g(n)=gcd(i,n),根据积性定义g(mn)=g(m)*g(n)(gcd(m,n)==1) 所以gcd(i,n ...

  9. poj 2154 Color 欧拉函数优化的ploya计数

    枚举位移肯定超时,对于一个位移i.我们须要的是它的循环个数,也就是gcd(i,n),gcd(i,n)个数肯定不会非常多,由于等价于n的约数的个数. 所以我们枚举n的约数.对于一个约数k,也就是循环个数 ...

  10. POJ 2407 Relatives(欧拉函数)

    http://poj.org/problem?id=2407 题意: 给出一个n,求小于等于的n的数中与n互质的数有几个. 思路: 欧拉函数的作用就是用来求这个的. #include<iostr ...

随机推荐

  1. 可视化数据matplotlib之安装与简单折线图

    matplotlib是一个可视化数据的模块,安装前需要先安装Visual Studio Community:然后去https://pypi.python.org/pypi上查找matplotlib并下 ...

  2. c#异步编程(三)—ASP.NET MVC 异步控制器及EF异步操作

    ASP.NET MVC 异步控制器及EF异步操作 异步控制器 ASP.NET MVC2后开始了对异步请求管道的支持,异步请求管道的作用是允许web服务器处理长时间运行的请求,比如 那些花费大量时间等待 ...

  3. BZOJ2793[Poi2012]Vouchers——枚举

    题目描述 考虑正整数集合,现在有n组人依次来取数,假设第i组来了x人,他们每个取的数一定是x的倍数,并且是还剩下的最小的x个.正整数中有m个数被标成了幸运数,问有哪些人取到了幸运数. 输入 第一行一个 ...

  4. BZOJ3697采药人的路径——点分治

    题目描述 采药人的药田是一个树状结构,每条路径上都种植着同种药材.采药人以自己对药材独到的见解,对每种药材进行了分类.大致分为两类,一种是阴性的,一种是阳性的.采药人每天都要进行采药活动.他选择的路径 ...

  5. 自学Aruba3.2-Aruba配置架构-Virtual AP配置要点

    点击返回:自学Aruba之路 自学Aruba3.2-Aruba配置架构-Virtual AP配置要点  1. AP.AP-Group和Virtual-AP的关系 解析列举:      AP1.AP3, ...

  6. [poj2528]Mayor's posters

    题目描述 The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campa ...

  7. 洛谷 P2300 合并神犇 解题报告

    P2300 合并神犇 题目背景 loidc来到了NOI的赛场上,他在那里看到了好多神犇. 题目描述 神犇们现在正排成一排在刷题.每个神犇都有一个能力值p[i].loidc认为坐在附近的金牌爷能力参差不 ...

  8. luogu4267 TamingtheHerd (dp)

    设f[i][j]为让前i天发生j次暴动需要改变的最少的值 则f[i][j]=min{f[k][j-1]+(x[k+1]!=0)+(x[k+2]!=1)+...+(x[i]!=(i-k-1))} $O( ...

  9. 压缩和解压缩文件tar, tar.gz and tar.bz2

    1, 对于tar.gz 压缩:tar -zcvf archive-name.tar.gz directory-name 解压:tar -zxvf prog-1-jan-2005.tar.gz -C / ...

  10. POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)

    POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...