You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.1 KiB
41 lines
1.1 KiB
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 1010;
|
|
|
|
// 筛法求欧拉函数
|
|
int primes[N], cnt;
|
|
bool st[N];
|
|
int phi[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);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
// 利用筛法,预处理出欧拉函数值数组
|
|
euler(N - 1); // 这里注意一下是N-1,防止数组越界
|
|
|
|
int n, m;
|
|
cin >> m;
|
|
for (int i = 1; i <= m; i++) {
|
|
cin >> n;
|
|
int res = 1; // x=y 记1个
|
|
for (int j = 1; j <= n; j++) res += phi[j] * 2; // 叠加两倍的欧拉函数值
|
|
printf("%d %d %d\n", i, n, res);
|
|
}
|
|
|
|
return 0;
|
|
} |