当前位置:网站首页>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;
}
边栏推荐
- Nature communications - modeling armed conflict risk under climate change using machine learning and time series data
- JGR-A | 南京大学黄安宁团队揭示高原湖泊山地影响极端降水的动力-热力机制
- Pytorch basis (II) -- tensor and gradient
- vivo大规模 Kubernetes 集群自动化运维实践
- About instruction set bits and instruction architecture bits
- EasyClick 运行代码片段出Null
- Big O notation interpretation
- Software testing often asks, do you really build a testing environment?
- 音视频技术开发周刊 | 249
- Spark source code (I) how spark submit submits jars and configuration parameters to spark server
猜你喜欢
Database system concept (Chapter 17)
数据库系统概念(第十七章)
[elm classification] data classification based on particle swarm optimization convolution neural network CNN combined with limit learning machine elm with matlab code
To vent their dissatisfaction with their superiors, Baidu post-95 programmers were sentenced to 9 months for deleting the database
vivo大规模 Kubernetes 集群自动化运维实践
2021CCPC网络赛题解加总结
Experience of electric competition - program-controlled wind pendulum
Codeforces Round #798 (Div. 2)ABCD
Idea remote debugging jar submitted by spark submit
Install Kubernetes 1.24
随机推荐
Codeforces Round #798 (Div. 2)ABCD
Flutter simple and excellent open source dialog uses free_ dialog
[dynamic planning] beginner level
On the exploitation of a horizontal ultra vires vulnerability
容斥原理(能被整除的数)
Brief introduction to memory structure of virtual machine
Test cases that testers must master
宝塔中查看mysql默认密码
Initial installation and use of redis [play with Huawei cloud]
flutter简单优秀的开源dialog使用free_dialog
Pagoda access changed from IP to domain name
Actual combat simulation │ real time error alarm of enterprise wechat robot
Multithreading starts from the lockless queue of UE4 (thread safe)
Software testing often asks, do you really build a testing environment?
D generate unique ID at compile time
第七章 文件管理作业
Folder data synchronization tool sync folders Pro
Big O notation interpretation
Web3和NFT中的匿名性问题
JGR-A | 南京大学黄安宁团队揭示高原湖泊山地影响极端降水的动力-热力机制