AcWing 874. 874.筛法求欧拉函数:奶酪宝宝来啦!
原题链接
简单
作者:
奶酪宝宝
,
2025-03-27 21:37:52
·山东
,
所有人可见
,
阅读 1
#include <iostream>
#define DEGENERATE_BOUNDARY 2
#define NUMBER_SHED 1
#define NUMBER_LEFT 0
#define NO_REMAINDER 0
using namespace std;
const int N = 1000000 + 10;
int primeSet[N];
int primeBitMap[N];
int naturalRank;
int numberRange;
void declaration(){
scanf("%d", &numberRange);
}
int eulerFunctions[N];
long long eulerSum;
void linearEulerCalculation(){
eulerFunctions[1] = 1;
for(int coefficient = DEGENERATE_BOUNDARY; coefficient <= numberRange; coefficient ++){
if(primeBitMap[coefficient] == NUMBER_LEFT){
primeSet[naturalRank ++] = coefficient;
eulerFunctions[coefficient] = coefficient - 1;
}
for(int primeRank = 0; coefficient <= numberRange / primeSet[primeRank]; primeRank ++){
primeBitMap[coefficient * primeSet[primeRank]] = NUMBER_SHED;
if(coefficient % primeSet[primeRank] == NO_REMAINDER){
eulerFunctions[coefficient * primeSet[primeRank]] = primeSet[primeRank] * eulerFunctions[coefficient];
break;
}
eulerFunctions[coefficient * primeSet[primeRank]] = (primeSet[primeRank] - 1) * eulerFunctions[coefficient];
}
}
}
void outputResult(){
for(int rank = 1; rank <= numberRange; rank ++){
eulerSum += eulerFunctions[rank];
}
printf("%lld", eulerSum);
}
int main(){
declaration();
linearEulerCalculation();
outputResult();
return 0;
}