一、题目

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. Linux修复MBR扇区故障

    给虚拟机增加一块硬盘,用于备份mbr的信息 fdisk -l 查看硬盘系统是否认识 fdisk /dev/sdb 进行分区 fdisk -l 查看分区是否出来 mkfs -t ext4 /dev/sb ...

  2. Suse系统磁盘文件损坏恢复

    进入救援(failSafe)模式检测问题,发现是因为/dev/sda4分区出现文件系统损坏.   /dev/sda4: UNEXPECTED INCONSISTENCY: run fsck manua ...

  3. ubuntu 安装两个版本的Anaconda

    1.下载anaconda2/anaconda3,下载地址:https://www.anaconda.com/download/#linux,anaconda官网如下所示,选择对应版本下载. 2.使用如 ...

  4. mysql sandbox的问题备忘

    工具很好用,但是安装运行时有些小问题: 1.启动数据库时提示--bootstrap已禁用:那是mysql5.7以后废弃了此参数,改用--initialize来初始化了,而一般公共仓库里的sandbox ...

  5. sam格式详细说明

    原文链接 https://www.jianshu.com/p/386f520e5de1 The SAM Format Specification(sam格式说明) 1 The SAM Format S ...

  6. dataview 组件使用示例

    来自<sencha touch 权威指南> ------------------------------- 例子1——app.js代码如下: Ext.require(['Ext.data. ...

  7. sencha表单入门例子

    来自于<sencha touch 权威指南> ------------------------------- 一.网站结构 二.index.html代码 <!DOCTYPE HTML ...

  8. lnmp+laravel部署到服务器出现 "GET / HTTP/1.1" 500 5

    lnmp一键安装包直接下载安装,就可以了,在此不多说. 虚拟机配置给个参考(lnmp安装包) server { listen 80; #listen [::]:80; server_name www. ...

  9. SpringMVC——视图和视图解析器

    请求处理方法执行完成后,最终返回一个 ModelAndView对象.对于那些返回 String,View 或 ModeMap 等类型的处理方法,Spring MVC 也会在内部将它们装配成一个Mode ...

  10. Map集合的关联数组实现

    public class AssoiativeArray<K,V>{ //创建一个二维数组 private Object[][] pairs; //声明索引 private int ind ...