'commit'
This commit is contained in:
44
TangDou/AcWing_TiGao/T5/OuLaHanShu/201_EulerShai_YouHua.cpp
Normal file
44
TangDou/AcWing_TiGao/T5/OuLaHanShu/201_EulerShai_YouHua.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
#define int long long
|
||||
#define endl "\n"
|
||||
const int N = 1010;
|
||||
|
||||
// 筛法求欧拉函数
|
||||
int primes[N], cnt;
|
||||
bool st[N];
|
||||
int phi[N];
|
||||
int s[N];
|
||||
void euler(int n) {
|
||||
phi[1] = 1;
|
||||
for (int i = 2; i <= n; i++) {
|
||||
if (!st[i]) {
|
||||
primes[cnt++] = i;
|
||||
phi[i] = i - 1;
|
||||
}
|
||||
for (int j = 0; primes[j] * i <= n; j++) {
|
||||
st[i * primes[j]] = true;
|
||||
if (i % primes[j] == 0) {
|
||||
phi[i * primes[j]] = phi[i] * primes[j];
|
||||
break;
|
||||
}
|
||||
phi[i * primes[j]] = phi[i] * (primes[j] - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
signed main() {
|
||||
// 利用筛法,预处理出欧拉函数值数组
|
||||
euler(N - 1); // 这里注意一下是N-1,防止数组越界
|
||||
|
||||
// 前缀和优化版本
|
||||
for (int i = 1; i < N; i++) s[i] = s[i - 1] + phi[i];
|
||||
|
||||
int n, m;
|
||||
cin >> m;
|
||||
for (int i = 1; i <= m; i++) {
|
||||
cin >> n;
|
||||
printf("%d %d %d\n", i, n, s[n] * 2 + 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user