poj 2407 Relatives(简单欧拉函数)

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

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<algorithm>
 6 #include<stdlib.h>
 7 using namespace std;
 8 #define ll long long
 9 ll eular(ll n){
10     ll ans=1;
11     for(ll i=2;i*i<=n;i++){
12         if(n%i==0){
13             ans*=i-1,n/=i;
14             while(n%i==0){
15                 ans*=i;
16                 n/=i;
17             }
18         }
19     }
20     if(n>1) ans*=n-1;
21     return ans;
22 }
23 int main()
24 {
25     ll n;
26     while(scanf("%I64d",&n)==1){
27         if(n==0) break;
28         ll ans=eular(n);
29         printf("%I64d\n",ans);
30     }
31     return 0;
32 }

时间: 2024-08-26 18:06:44

poj 2407 Relatives(简单欧拉函数)的相关文章

POJ 2407 Relatives(欧拉函数入门题)

Relatives 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 t

POJ 2407 Relatives(欧拉函数)

Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11801   Accepted: 5780 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 ther

POJ 2478 Farey Sequence( 欧拉函数 + 法雷数列 )

POJ 2478 Farey Sequence ( 欧拉函数 + 法雷数列 ) #include <cstdio> #include <cstring> using namespace std; #define MAXN 1000005 typedef long long LL; int vis[ MAXN ], prime[ MAXN ], cnt, n; LL phi[ MAXN ]; void get_phi_prime( int N ) { phi[1] = 1; cnt

POJ 2407 Relatives &amp;&amp; UVA 10299 Relatives(欧拉函数)

[题目链接]:click here~~ [题目大意]:欧拉函数:求少于或等于n的数中与n互素的数的个数:n <= 1,000,000,000. [思路]:裸欧拉函数,注意特判n==1的情况,n==1的情况下,应该输出0,poj依然判断1也可以过,但是老牌ojUVA必须是0才过,注意一下. 代码: #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>

POJ2407 Relatives(欧拉函数)

题目问有多少个小于n的正整数与n互质. 这个可以用容斥原理来解HDU4135.事实上这道题就是求欧拉函数$φ(n)$. $$φ(n)=n(1-1/p_1)(1-1/p_2)\dots(1-1/p_m)\tag{p为n的质因子}$$ 这个通项公式可以通过容斥原理的解法来验证.那么利用这个通项就能在$O(\sqrt[]n)$下计算出φ(n). 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int phi(

HDU 2824 简单欧拉函数

1.HDU 2824   The Euler function 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=2824 3.总结:欧拉函数 题意:求(a,b)间的欧拉函数值的和. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio>

POJ 2154 Color (ploya欧拉函数)

ploya定理,然后公式利用欧拉函数优化,gcd必然是因子,这样只要枚举因子,每个因子利用欧拉函数计算出现次数 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int t, n, p; int pow_mod(int x, int k) { x %= p; int ans = 1; while (k) { if (k&1) ans = ans *

POJ_2407 Relatives 【欧拉函数裸题】

一.题目 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 c

POJ 1284 Primitive Roots 欧拉函数模板题

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #include <ctime> #include <io