Relatives
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 11422
Accepted: 5571

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

Source









Waterloo local 2002.07.01

题目大意:给你一个正整数N。求在小于N的范围内。有多少个正整数与N互质?

思路:典型的欧拉phi函数

欧拉函数(摘自百度百科):

在数论,对正整数n,欧拉函数φ(n)是少于或等于n的数中与n互质的数的数目。

φ(n) = n(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),当中p1, p2……pn为x的所

有质因数,x是不为0的整数。φ(1)=1(唯一和1互质的数(小于等于1)就是1本身)。

(注意:每种质因数仅仅一个。比方12=2*2*3那么φ(12)=12*(1-1/2)*(1-1/3)=4

一些定理:

1>若n是质数p的k次幂。φ(n) = p^k-p^(k-1) =(p-1)p^(k-1),由于除了p的倍数外,

其它数都跟n互质。

2>若n为素数,φ(n) 
= n - 1;(同第6条)

3>当n为奇数时,φ(2n)=φ(n)。

4>欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n)。

5>欧拉定理:对不论什么两个互质的正整数a, m, m>=2有 a^φ(m) ≡ 1(mod m)。

6>费马小定理:当m是质数p时,此式则为:a^(p-1)≡1(mod m)。

#include <stdio.h>
#include <math.h> int Euler(int n)
{
int i,ret = n;
for(i = 2; i <= sqrt(1.0*n); i++)
{
if(n % i == 0)
{
ret = ret - ret/i;
}
while(n % i == 0)
n /= i;
}
if(n > 1)
ret = ret - ret/n;
return ret;
}
int main()
{
int p;
while(~scanf("%d",&p) && p)
printf("%d\n",Euler(p));
return 0;
}

POJ2407_Relatives【欧拉phi函数】【基本】的更多相关文章

  1. HDU 1286 找新朋友 (欧拉phi函数打表)

    题意:你懂得. 析:一看这个题应该是欧拉phi函数,也就说欧拉phi函数是指求从 1 到 n 中与 n 互素的数的个数,这个题很明显是这个意思嘛,不多说了. 代码如下: #include <io ...

  2. 积性函数初步(欧拉$\varphi$函数)

    updata on 2020.4.3 添加了欧拉\(\varphi\)函数为积性函数的证明和它的计算方式 1.积性函数 设\(f(n)\)为定义在正整数上的函数,若\(f(1)=1\),且对于任意正整 ...

  3. 容斥原理、欧拉函数、phi

    容斥原理: 直接摘用百度词条: 也可表示为 设S为有限集, ,则 两个集合的容斥关系公式:A∪B = A+B - A∩B (∩:重合的部分) 三个集合的容斥关系公式:A∪B∪C = A+B+C - A ...

  4. hdu 5279 Reflect phi 欧拉函数

    Reflect Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest_chi ...

  5. (Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))

    /* * POJ_2407.cpp * * Created on: 2013年11月19日 * Author: Administrator */ #include <iostream> # ...

  6. 求一个极大数的欧拉函数 phi(i)

    思路: 因为当n>=1e10的时候,线性筛就不好使啦.所以要用一个公式 φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn) 证明详见:<公式 ...

  7. POJ3090 巧用欧拉函数 phi(x)

    POJ3090 给定一个坐标系范围 求不同的整数方向个数 分析: 除了三个特殊方向(y轴方向 x轴方向 (1,1)方向)其他方向的最小向量表示(x,y)必然互质 所以对欧拉函数前N项求和 乘2(关于( ...

  8. (hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)

    题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...

  9. Master of Phi (欧拉函数 + 积性函数的性质 + 狄利克雷卷积)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6265 题目大意:首先T是测试组数,n代表当前这个数的因子的种类,然后接下来的p和q,代表当前这个数的因 ...

随机推荐

  1. unused function warning message

    這篇的對象是 static function, static function 若沒有其它 function 去存取的話, 在 compile 時,會發生 unused error, 可以在 func ...

  2. 复制View对象

    
Mark一下 - (UIView*)duplicate:(UIView*)view { NSData * tempArchive = [NSKeyedArchiver archivedDataWit ...

  3. java实现udp发送端和接收端

    发送端: package demo02; import java.io.IOException; import java.net.DatagramPacket; import java.net.Dat ...

  4. (5)centos图形界面安装

    1.登录 2.先安装MATE可视化桌面 yum groups install "MATE Desktop" 选择y 3.安装X Window System:图形接口 yum gro ...

  5. FZU 1075 分解素因子【数论/唯一分解定理/分解素因子裸模板】

    [唯一分解定理]:https://www.cnblogs.com/mjtcn/p/6743624.html 假设x是一个正整数,它的值不超过65535(即1<x<=65535),请编写一个 ...

  6. google搜索打不开?提供 国内几个给力的服务器

    http://203.208.46.145/ 这是北京的机器,快到飞起来. http://74.125.224.232/, 屡试不爽 用编辑器打开C:\WINDOWS\system32\drivers ...

  7. 盘点支持Orchard的.NET 4.5虚拟主机(虚拟空间)

    Orchard作为.NET社区最为先进的开源项目之一,已经被越来越多的人使用, 由于它使用了最时髦的微软技术栈(.NET4.5),因此在 Win2008+和IIS7+ 的环境上运行最为完美, 而win ...

  8. Using Single Alert For Messages And Confirmation Messages In Oracle Forms With Set_Alert_Button_Property

    Learn how to use single Oracle Form's Alert object for warning/information messages and confirmation ...

  9. python GIL

    https://www.cnblogs.com/MnCu8261/p/6357633.html 全局解释器锁,同一时间只有一个线程获得GIL,

  10. python遍历当前目录并删除某文件

    #coding: utf-8 """ this programe is to clear driverlog below this dir __author__:the_ ...