hdu1286 找新朋友 欧拉函数模板
2024-09-01 15:50:24
首先这一题用的是欧拉函数!!函数!!不是什么欧拉公式!!
欧拉函数求的就是题目要求的数。
关于欧拉函数的模板网上百度一下到处都是,原理也容易找,这里要介绍一下另一个强势模板。
在这一题的讨论里看到的。
上题上代码。
-----------------------------------------------------------------------------------
新年快到了,“猪头帮协会”准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有大于1的公约数,否则都是新朋友,现在会长想知道究竟有几个新朋友?请你编程序帮会长计算出来。
Input
第一行是测试数据的组数CN(Case number,1<CN<10000),接着有CN行正整数N(1<n<32768),表示会员人数。
Output
对于每一个N,输出一行新朋友的人数,这样共有CN行输出。
Sample Input
2
25608
24027
25608
24027
Sample Output
7680
16016
16016
Author
SmallBeer(CML)
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <iomanip> using namespace std; typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
const int MOD = 1e9+; int euler(int n)
{
int ret = n;
for(int i = ;i * i <= n;++i)
{
if(n % i == )
{
ret = ret - ret / i;
while(n % i == )
n /= i;
}
}
if(n > )
ret = ret - ret / n;
return ret;
} int main()
{
int T;
cin >>T;
while(T--)
{
int n;
cin >>n;
cout <<euler(n)<<endl;
}
return ;
}
关于if(n > 1):例如n = 12 = 3 * 4,先得到的 i 是2,然后n = 3,就跳出来了,因为循环的判断条件是 i * i <= n。
最新文章
- [Linux 性能检测工具]PIDSTAT
- [leetcode] 根据String数组构造TreeNode,用于LeetCode树结构相关的测试用例
- UIkit框架之UIPickerView
- 数据库迁移之从oracle 到 MySQL
- 斐波那契博弈(Fibonacci Nim)
- oracle 问题若干 提醒注意
- JAVASCRIPT实现XML分页
- android.telephony.SmsManager 短信笔记
- nodejs package.json详细解读
- DataFrame创建
- 如何把Excel中的单元格等对象保存成图片
- Linux的安装和使用技巧
- Visual Studio 2019 使用 Live Share
- Spring AOP实现统一日志输出
- 腾讯云服务器tomcat端口无法访问
- WPF中任务栏只显示主窗口
- conda虚拟环境
- python之路 ---计算机硬件基础
- C++标准模板库(STL)介绍:string的基本用法
- Android忘记锁屏密码如何进入手机?