一、题目

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. mysql中如何不重复插入满足某些条件的重复的记录的问题

    最近在项目中遇到了这样的一个问题“: 在mysql数据库中需要每次插入的时候不能插入三个字段都相同的记录.在这里使用到了 insert into if not exists  和insert igno ...

  2. cocos2d-x 在vs2010下的环境配置

    cocos2d-x编译完成之后生成了一堆动态库,在window下分别为.dll , .lib ,其中*lib是在编译的时候需要链接的,*.dll是在运行的时候依赖的.所以当我们创建一个cocos2d- ...

  3. 反射01 Class类的使用、动态加载类、类类型说明、获取类的信息

    0 Java反射机制 反射(Reflection)是 Java 的高级特性之一,是框架实现的基础. 0.1 定义 Java 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对 ...

  4. 微信WeixinJSBridge API 屏蔽右上角分享等常用方法

    WeixinJSBridge这个API有几个功能还是相当有用的,比如: 1.隐藏微信网页右上角的按钮(...按钮):开发者可以用这个功能来禁止当前页面被分享 2.隐藏微信网页底部的导航栏(比如前进后退 ...

  5. 使用 append 方法追加元素

    来自于<sencha touch 权威指南> 学习使用 Ext.DomHelper 组件在页面中追加元素.app.js代码如下: Ext.require(['Ext.form.Panel' ...

  6. docker入门及安装

    Docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机).ba ...

  7. 设计模式07: Bridge 桥接模式(结构型模式)

    Bridge 桥接模式(结构型模式) 抽象与实现 抽象不应该依赖于实现细节,实现细节应该依赖于抽象. 抽象B稳定,实现细节b变化 问题在于如果抽象B由于固有的原因,本身并不稳定,也有可能变化,怎么办? ...

  8. poj1840 Eqs(hash+折半枚举)

    Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...

  9. Android Tablayout属性介绍

    1.添加依赖 compile 'com.android.support:design:26.0.0-alpha1' 2.属性 改变选中字体的颜色app:tabSelectedTextColor=&qu ...

  10. 使用JS完成首页定时弹出广告图片

    一.需求分析 在首页中的顶部做一个定时弹出广告图片. 二.技术分析 隐藏图片: display: none        定时操作: setInterval(“显示图片的函数”, 3000); 三.代 ...