U - Relatives(欧拉函数)
2024-10-16 06:01:49
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互质的数的个数。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int Euler(int x){
int r=x;
for(int i=;i*i<=x;i++){//由于任何一个合数都至少有一个不大于根号x的质因子,所以只需遍历到根号x即可
if(x%i==){
r=r/i*(i-);//欧拉函数的通式,先除后乘,避免数据溢出
while(x%i==)x/=i;//消除x中所有质因子i,直到不能被i整除
}
}
if(x>)r=r/x*(x-);//如果x大于1,说明还有一个质因子没有除掉
return r;//返回个数
}
int main(){
int n;
while(cin>>n&&n)
cout<<Euler(n)<<endl;
return ;
}
最新文章
- DAC Usage2:通过DAC实现DB Schema的Migration和Upgrade
- IOS开发之显示微博表情
- 背景建模post_processing常用opencv函数(怒了)
- Spring mvc 中使用ftl引用共通文件出错 FreeMarker template error: Error reading included file ";/WEB-INF/ftl/common/errormessage.ftl";
- pip卡住不动的解决方案
- Design and Analysis of Algorithms_Introduction
- Android中的sp与wp
- Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口
- HTML5元素拖拽实现示例
- android获得屏幕高度和宽度
- [布局]bootstrap基本标签总结2
- 黑马程序员 ——Java SE(1)
- 团队作业4----第一次项目冲刺(Alpha版本)4.25
- Asynchronous vs synchronous client applications(MQTT)
- python全栈学习--day3
- jacascript JSON对象的学习
- Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控
- 【Unity】6.5 Time类、Mathf类、Coroutine类
- dubbo provider如何对invoker进行export
- Bootloader的结构和启动过程
热门文章
- ci框架(codeigniter)Email发送邮件、收件人、附件、Email调试工具
- POJ 3320_Jessica&#39;s Reading Problem
- NOIP 2010 乌龟棋
- Android GIS开发系列-- 入门季(14)FeatureLayer之范围查询
- mysql建表语句key的含义
- HDU 1796 How many integers can you find(容斥原理+二进制/DFS)
- Eclipse或SVN—怎样在Eclipse中安装SVNclient插件
- C# 实现WEBSOCKET聊天应用示例
- 使用fiddler将网站上的css js重定向至本地文件
- JAVA 并发编程-读写锁之模拟缓存系统(十一)