1 Star 2 Fork 0

YangZhi1605/CSDNCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
公式法求欧拉函数.cpp 679 Bytes
一键复制 编辑 原始数据 按行查看 历史
YangZhi1605 提交于 2022-01-20 13:23 . 数论二代码
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
//输入
int n;
cin >>n;
while(n--)
{
int a;
cin >> a;
int res = a;
for(int i = 2; i <= a/i;i++)
if(a % i == 0) //i 是 a 的一个质因子
{
//公式放进来
//N x(1- 1/p1)x(1-1/p2)x...x
res = res /i * (i-1);
//用循环把质因子剔干净
while(a % i == 0) a/= i;
}
//如果a大于1,且有没有在if里执行,说明它是就是一个质数,直接处理它
if(a > 1) res = res / a * (a-1);
//res就是欧拉函数的值
cout << res << endl;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/twopointers/csdncode.git
git@gitee.com:twopointers/csdncode.git
twopointers
csdncode
CSDNCode
master

搜索帮助