http://poj.org/problem?id=2407

                                         Relatives
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16652   Accepted: 8470

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

 
思路:欧拉函数板子题,就是就给定数字的欧拉函数
 
 
#include <iostream>
using namespace std;
#define LL long long
LL Phi(LL x){ //按定义直接算
if(x == 1){
return 1;
}
LL ans = x;
for(int i = 2; i * i <= x; i++){
if(x % i == 0){
ans = ans / i * (i - 1);
while(x % i == 0){
x = x / i;
}
}
}
// cout << ans << " " << x << endl;
if(x >= 2)
ans = ans / x * (x - 1);
return ans;
} //bool visit[10001];
//int tot=0, pri[1000005], phi[1000005];
//LL Phi(LL n){ //线性筛选
// phi[1] = 1;
// for(int i = 2; i <= n; i++){
// if(!visit[i]){
// pri[++tot] = i;
// phi[i] = i - 1;
// }
// for(int j = 1, x; j <= tot && (x = i * pri[j]) <= n; j++){
// visit[x] = true;
// if(i % pri[j] == 0){ //不互质时
// phi[x] = phi[i] * pri[j];
// break;
// }
// else{
// phi[x] = phi[i] * phi[pri[j]];
// }
// }
// }
// return phi[n];
//} int main(){
LL x;
while(cin >> x && x){
cout << Phi(x) << endl;
}
return 0;
}

  

76-Relatives-欧拉函数的更多相关文章

  1. poj2407 Relatives 欧拉函数基本应用

    题意很简单 就是欧拉函数的定义: 欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) .题目求的就是φ(n) 根据 通式:φ(x)=x*(1-1/p1)*(1-1/ ...

  2. POJ 2407 Relatives(欧拉函数)

    题目链接 题意 : 求小于等于n中与n互质的数的个数. 思路 : 看数学的时候有一部分是将欧拉函数的,虽然我没怎么看懂,但是模板我记得了,所以直接套了一下模板. 这里是欧拉函数的简介. #includ ...

  3. POJ2407–Relatives(欧拉函数)

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

  4. POJ 2407 Relatives 欧拉函数题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

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

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

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

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

  7. POJ 2407:Relatives(欧拉函数模板)

    Relatives AC代码 Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16186   Accept ...

  8. POJ 2407 Relatives 【欧拉函数】

    裸欧拉函数. #include<stdio.h> #include<string.h> ; int p[N],pr[N],cnt; void init(){ ;i<N;i ...

  9. POJ2407 Relatives(欧拉函数)

    题目问有多少个小于n的正整数与n互质. 这个可以用容斥原理来解HDU4135.事实上这道题就是求欧拉函数$φ(n)$. $$φ(n)=n(1-1/p_1)(1-1/p_2)\dots(1-1/p_m) ...

  10. 欧拉函数K - Relatives

    欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n). 特殊性质:当n为奇数时,φ(2n)=φ(n), φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..( ...

随机推荐

  1. RK3288 修改设备默认的蓝牙名称

    path:device/rockchip/rk3288/bluetooth/bdroid_buildcfg.h /* * Copyright (C) 2012 The Android Open Sou ...

  2. RK3288 添加USB转虚拟串口设备

    在系统开启并有日志打印的前提下,插入USB设备,就会打印USB设备和虚拟串口信息. 打印信息如下: 供应商ID(VID):idVendor=1234,产品ID(PID): idProduct=5678 ...

  3. 从汇编的角度看待const与#define

    先观察一下的代码: #include<stdio.h> int main(){ ; int y; int *pi=(int*)&i; *pi=; y=*pi; int tempi; ...

  4. printf()_scanf()_取余运算符与取模运算符

    基本的输入和输出函数的用法 printf();四种用法 1.printf("字符串\n"); 2.printf("输出控制符",输出参数); 3.printf( ...

  5. RedHat6.5用ISO配置yum源

    CentOS自带强大的yum功能,默认为从网上自动下载rpm包,对于网速不太给力或者没有网络的情况下需要用的话就不是很方便,很多软件尤其是服务器上的软件我们么有必要追求最新,稳定性最重要,这里我们用C ...

  6. 报错:Can't find a source file at "xxxxx“Locate the file or edit the source lookup path to include its location.

    调试问题: Can't find a source file at "/tmp/TI_MKLIB6sLCzz/SRC/exit.c" Locate the file or edit ...

  7. 27 mysql主从出现错误

    大多数的互联网应用场景都是读多写少,在发展过程中可能会出现读性能问题,在数据库层解决读性能问题:一主多从 下面是多主从结构 虚线箭头表示主备关系,A与A’互为主备,从库B,C,D指向主库A,一主多从的 ...

  8. 第七章 HTTP流量管理(一) gateway

    Gateway: istio的一种资源类型,Istio Gateway告诉k8s的istio-ingressgateway pods可以打开哪些主机和端口(如下的80是 ingressgateway ...

  9. MySQL 库、表

    1.库 1.库的基本操作 1.查看已有的库 show databases; 2.创建库(指定默认字符集) create database 库名 default charset=utf8; 3.查看创建 ...

  10. 使用Ajax异步上传文件

    之前上传文件都是用表单form设置post请求和enctype类型: <form id="upload_form"action="" method=&qu ...