POJ 2407 Relatives (欧拉函数)
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
分析:
求一个数的所有的小于该数且与该数互质的数的个数,也就是欧拉函数的应用。
直接套欧拉函数公式,即将n素分解后有n=p1k1*p2k2…pm^km,则euler(n)=n(1-1/p1)(1-1/p2)…(1-1/pm) 。
代码:
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
ll get_euler(ll n)//欧拉函数的应用
{
ll ans=n;
for(ll i=2; i*i<=n; i++)
{
if(n%i==0)
{
ans=ans/i*(i-1);
while(n%i==0)
n/=i;
}
}
if(n>1)
ans=ans/n*(n-1);
return ans;
}
int main()
{
ll n;
while(scanf("%lld",&n),n)
printf("%lld\n",get_euler(n));
return 0;
}
POJ 2407 Relatives (欧拉函数)的更多相关文章
- POJ 2407 Relatives(欧拉函数)
题目链接 题意 : 求小于等于n中与n互质的数的个数. 思路 : 看数学的时候有一部分是将欧拉函数的,虽然我没怎么看懂,但是模板我记得了,所以直接套了一下模板. 这里是欧拉函数的简介. #includ ...
- POJ 2407 Relatives 欧拉函数题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- POJ 2480 (约数+欧拉函数)
题目链接: http://poj.org/problem?id=2480 题目大意:求Σgcd(i,n). 解题思路: 如果i与n互质,gcd(i,n)=1,且总和=欧拉函数phi(n). 如果i与n ...
- Poj 2478-Farey Sequence 欧拉函数,素数,线性筛
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14291 Accepted: 5647 D ...
- poj2407 Relatives 欧拉函数基本应用
题意很简单 就是欧拉函数的定义: 欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) .题目求的就是φ(n) 根据 通式:φ(x)=x*(1-1/p1)*(1-1/ ...
- POJ2407–Relatives(欧拉函数)
题目大意 给定一个正整数n,要求你求出所有小于n的正整数当中与n互质的数的个数 题解 欧拉函数模板题~~~因为n过大~~~所以直接用公式求 代码: #include<iostream> # ...
- poj 2773 利用欧拉函数求互质数
题意:找到与n互质的第 k个数 开始一看n是1e6 敲了个暴力结果tle了,后来发现k达到了 1e8 所以需要用到欧拉函数. 我们设小于n的 ,与n互质的数为 (a1,a2,a3.......a(p ...
- poj 2480 (欧拉函数应用)
点击打开链接 //求SUM(gcd(i,n), 1<=i<=n) /* g(n)=gcd(i,n),根据积性定义g(mn)=g(m)*g(n)(gcd(m,n)==1) 所以gcd(i,n ...
- poj 2154 Color 欧拉函数优化的ploya计数
枚举位移肯定超时,对于一个位移i.我们须要的是它的循环个数,也就是gcd(i,n),gcd(i,n)个数肯定不会非常多,由于等价于n的约数的个数. 所以我们枚举n的约数.对于一个约数k,也就是循环个数 ...
- POJ 2407 Relatives(欧拉函数)
http://poj.org/problem?id=2407 题意: 给出一个n,求小于等于的n的数中与n互质的数有几个. 思路: 欧拉函数的作用就是用来求这个的. #include<iostr ...
随机推荐
- python 模块之-hashlib
python 模块hashlib import hashlib m=hashlib.md5() # 生成MD5加密对象 m.update('jiami-string'.encode(' ...
- echarts之简单的入门——【一】做个带时间轴的柱状统计图
百度Echarts 官网首页 http://echarts.baidu.com/ 配置项手册 http://echarts.baidu.com/option.html#title GL配置项手册 h ...
- vmware错误汇总
[问题来源] 因为虚拟机过大,所以直接在本地磁盘直接复制,启动的时候,换好IP重新启动网卡报错. device eth0 does not seem to be present.. ifconfig查 ...
- JeeSite框架中httpSession.invalidate();无效
想要实现一个功能:同一个用户在两个不同的浏览器中登录,后面的踢掉之前的登录. 本来的思路是在httpSession监听器中进行判断.但是在使用httpSession.invalidate();销毁Se ...
- 架构师成长之路6.1 DNS理论
点击返回架构师成长之路 架构师成长之路6.1 DNS理论 1.DNS一些基本概念 ① FQDN:Full Qualified Domain Name,完全限定域名,即每个域在全球网络都是唯 ...
- A1038. Recover the Smallest Number
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- 在Windows上部署dubbo-admin(监控中心)
在Windows上部署dubbo-admin(监控中心) 2018年11月12日 22:45:13 zzz_er 阅读数:1004 标签: dubbo 更多 个人分类: dubbo 版权声明:本文 ...
- 2018.9青岛网络预选赛(C)
传送门:Problem C https://www.cnblogs.com/violet-acmer/p/9664805.html 题意: 定义五个指令,判断能否从输入的n条指令中成功跳出循环,如果不 ...
- Linux网络协议栈(一)——Socket入门(1)
转自: http://www.cnblogs.com/hustcat/archive/2009/09/17/1568738.html 1.TCP/IP参考模型为了实现各种网络的互连,国际标准化组织(I ...
- 修改sqlarchemy源码使其支持jdbc连接mysql
注意:本文不会将所有完整源码贴出,只是将具体的思路以及部分源码贴出,需要感兴趣的读者自己实验然后实现吆. 缘起 公司最近的项目需要将之前的部分业务的数据库连接方式改为jdbc,但由于之前的项目都使用s ...