一、题目

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互质的数的数目。题意就是欧拉函数的定义。

三、AC代码

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
const int MAXN = 1e5+3;
int Prime[MAXN], nPrime;
bool isPrime[MAXN]; void getPrime()
{
memset(isPrime, 1, sizeof(isPrime));
isPrime[0] = isPrime[1] = 0;
nPrime = 0;
for(int i = 2; i < MAXN; i++)
{
if(isPrime[i])
Prime[nPrime++] = i;
for(int j = 0; j < nPrime && (long long)Prime[j]*i < MAXN; j++)
{
isPrime[Prime[j]*i] = 0;
if(i%Prime[j] == 0)
break;
}
}
} int Euler(int n)
{
int ans = n;
for(int i = 0; Prime[i]*Prime[i] <= n; i++)
{
if(n%Prime[i] == 0)
{
ans = ans - ans/Prime[i];
do
{
n/=Prime[i];
}
while(n%Prime[i] == 0);
}
}
if(n > 1)
ans = ans - ans/n;
return ans;
} int main()
{
int N;
getPrime();
while(cin >> N && N)
{
cout << Euler(N) << endl;
}
return 0;
}

  

POJ_2407 Relatives 【欧拉函数裸题】的更多相关文章

  1. HDU3501——欧拉函数裸题

    给整数N(1 ≤ N ≤ 1000000000),求小于N的与N不互素的所有正整数的和. 思路:1.用欧拉函数求出小于N的与N互素的正整数的个数: 2.若 p 与 N 互素,则 N-p 必与 N 互素 ...

  2. poj 2407 欧拉函数裸题

    http://poj.org/problem?id=2407 题意:多组数据,每次输入一个数 ,求这个数的欧拉函数 int euler_phi(int n){//单个欧拉函数 int m=(int)s ...

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

    Relatives Given n, a positive integer, how many positive integers less than n are relatively prime t ...

  4. (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 ...

  5. POJ2407–Relatives(欧拉函数)

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

  6. UVA 10820 欧拉函数模板题

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

  7. hdu 1286 找新朋友 欧拉函数模版题

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Des ...

  8. poj2407(欧拉函数模板题)

    题目链接:https://vjudge.net/problem/POJ-2407 题意:给出n,求0..n-1中与n互质的数的个数. 思路:欧拉函数板子题,先根据唯一分解定理求出n的所有质因数p1,p ...

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

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

随机推荐

  1. 345. Reverse Vowels of a String翻转字符串中的元音字母

    [抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

  2. Mock Server实践

    转载自 https://tech.meituan.com/mock-server-in-action.html 背景 在美团服务端测试中,被测服务通常依赖于一系列的外部模块,被测服务与外部模块间通过R ...

  3. Solidity oraclize query apikey加密

    solidity 程序中如果用到oraclize query,api调用需要apikey,则最好加密apikey,否则公开solidity代码时会连同apikey一起公开. 加密方法: https:/ ...

  4. c++虚析构函数的使用及其注意点

    // ConsoleApplication33.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...

  5. Visual Studio 2010调试本地 IIS 站点

    点击vs的Debug-Attach to Process选中 w3wp.exe,然后点击Attach, vs便进入debug模式.

  6. css总结19:HTML5 Canvas(画布)

    1  <canvas> 标签定义图形,比如图表和其他图像. 例1:简单使用: <canvas id="Canva" width="200" h ...

  7. UIView 和 CALayer区别 为啥有UIView还要CALayer?

    今天,被坑了,面试的时候没回答出来,特此记录一下 一.继承结构 1: UIView的继承结构为: UIResponder : NSObject UIResponder是用来响应事件的,也就是UIVie ...

  8. ios7适配--navgationbar遮住下面view的处理

    3down votefavorite   Have you guys stumbled up on this issue ? Basically in iOS 7 Navigation Control ...

  9. 【2008nmj】GDA二元分类.docx

  10. 编写高质量代码改善C#程序的157个建议——建议30:使用LINQ取代集合中的比较器和迭代器

    建议30:使用LINQ取代集合中的比较器和迭代器 LINQ提供了类似于SQL的语法来实现遍历.筛选与投影集合的功能. static void Main(string[] args) { List< ...