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. MySQL出现1030-Got error 28 from storage engine错误

    Navicat for MySQL出现1030-Got error 28 from storage engine错误  刚刚还能用这会儿就用不了了,估计是磁盘空间不足引起的! 在根目录/下执行命令:d ...

  2. (转)Android短信的发送和接收监听

    /**发送与接收的广播**/     String SENT_SMS_ACTION = "SENT_SMS_ACTION";     String DELIVERED_SMS_AC ...

  3. RK3288 error: undefined reference to 'LOGD'

    HAL层和JNI层中的打印都必须包含下面的宏和头文件. 比如:LOGD.LOGE等等. #define LOG_TAG "TEST_LED" #include <utils/ ...

  4. Android:ScrollView和SwipeRefreshLayout高度测量

    今天组里的同事要做一个奇葩的效果,要求在ScrollView里嵌套一个RefreshLayout.类似代码如下: <?xml version="1.0" encoding=& ...

  5. python 之 itertools模块

    官方:https://yiyibooks.cn/xx/python_352/library/itertools.html 参考: https://blog.csdn.net/neweastsun/ar ...

  6. Tow types of EF's default connection

    When you create a new project that makes use of Entity Framework 5, you'll notice how it uses LocalD ...

  7. Python环境搭建之OpenGL

    以下内容为我python OpenGl 环境搭建历程: win7 64位操作系统,python3.5.3 ,无其他相关. 直接cmd或PowerShell输入以下命令: pip install PyO ...

  8. Android 4 学习(21):对话框

    对话框 创建Dialog的两种方式: 1. 使用Dialog类或其子类,包括DialogFragment 2. 在Activity中使用Dialog主题(theme) 下面是使用Dialog类的一个例 ...

  9. 使用JAVA爬取京东商品价格

    有一件物品,你想看看它在京东下所有搜索结果的价格,要怎么办呢? 京东这个网站还是很好爬的,所有价格信息都写在了Html里面,而且跳到第二页之后,url也是有规律的,基本没有什么技术难度. 例如:想找i ...

  10. 读《分布式一致性原理》CURATOR客户端3

    分布式锁 在分布式环境中,为了保证数据的一致性,经常在程序运行的某个运行点.需要进行同步控制. package master; import java.text.SimpleDateFormat; i ...