当前位置:网站首页>Euler function and finding Euler function by linear sieve
Euler function and finding Euler function by linear sieve
2022-06-13 11:02:00 【I can screw the bottle cap when I am born again】

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int phi(int x)
{
int res = x;
for (int i=2;i<=x/i;i++)
{
if (x%i==0)
{
res = res/i*(i-1);
while (x%i==0) x/=i;
}
}
if (x>1) res = res/x*(x-1);
return res;
}
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
while (n -- ){
int x;
cin>>x;
cout<<phi(x)<<endl;
}
return 0;
}
Finding Euler function through linear sieve
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e6+10;
int pri[N],cnt,phi[N];
bool st[N];
void cul_phi(int n)
{
phi[1]=1;
for (int i=2;i<=n;i++)
{
if (!st[i]) {
phi[i] = i-1;
pri[cnt++] = i;
}
for (int j=0;pri[j]<=n/i;j++)
{
int t = pri[j]*i;
st[t] = true;
if (i%pri[j]==0){
phi[t] = phi[i]*pri[j];
break;
}
phi[t] = phi[i]*(pri[j]-1);
}
}
}
int main()
{
int n;
LL sum=0;
scanf("%d", &n);
cul_phi(n);
for (int i=1;i<=n;i++) sum+=phi[i];
printf ("%lld\n",sum);
return 0;
}
边栏推荐
- How to optimize MySQL?
- 为发泄对上司不满,百度95后程序员删库被判9个月
- Use of servers
- Acwing game 55
- 音视频技术开发周刊 | 249
- Do you agree that the salary of hardware engineers is falsely high?
- 日志1111
- The first laravel workflow engine released the official version of v1.0
- Brief description of redo logs and undo logs in MySQL
- 硬件工程师薪资虚高,你认可吗?
猜你喜欢

文件夹数据同步工具Sync Folders Pro

Navicat connection MySQL in Pagoda

Actual combat analysis of malicious code lab05-01

元宇宙土地:是什么让数字房地产变得有价值

Vivo large scale kubernetes cluster automation operation and maintenance practice

终于,月入 20000 !!

Go 要加个箭头语法,这下更像 PHP 了!
![[elm classification] data classification based on particle swarm optimization convolution neural network CNN combined with limit learning machine elm with matlab code](/img/e3/a299393e865104d96341ce3d93ac6b.png)
[elm classification] data classification based on particle swarm optimization convolution neural network CNN combined with limit learning machine elm with matlab code

实战模拟│企业微信机器人实时报错预警

技术管理进阶——管理者可以使用哪些管理工具
随机推荐
Ubuntu installs MySQL compressed package for future reference
Necessary for Architects: system capacity status checklist
Initial installation and use of redis [play with Huawei cloud]
JGR-A | 南京大学黄安宁团队揭示高原湖泊山地影响极端降水的动力-热力机制
数据库学习笔记(第十六章)
ue5 小知识点 geometry script modeling
Install Kubernetes 1.24
vivo大规模 Kubernetes 集群自动化运维实践
About instruction set bits and instruction architecture bits
vivo大规模 Kubernetes 集群自动化运维实践
Questions and answers of the labor worker general basic (labor worker) work license in 2022
Wait for someone with "source" | openharmony growth plan student challenge registration to start
作为一个测试人员,这些基础知识必不可少
Necessary for Architects: system capacity status checklist
Environ. Sci. Technol.(IF=9.028) | 城市绿化对大气环境的影响
MySQL transaction isolation level and mvcc
宝塔添加一个网站:PHP项目
报告录屏+PPT 傅云飞-喜马拉雅山脉南坡云降水特征研究
Spark source code (I) how spark submit submits jars and configuration parameters to spark server
Pagoda access changed from IP to domain name