0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 19:28:48 +00:00

#168. 【模板】线性筛

https://sjzezoj.com/submission/74744
This commit is contained in:
Baoshuo Ren 2023-03-30 11:08:10 +08:00
parent ad980d856c
commit f50cbec887
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -1,76 +1,63 @@
#pragma GCC optimize("Ofast")
#include <iostream>
#include <bits/stdc++.h>
using std::cin;
using std::cout;
const char endl = '\n';
using namespace std;
const int N = 1e6 + 5;
int q, p, op, k, prime[1000005], miu[1000005], phi[1000005];
long long sig[1000005];
bool is_prime[1000005];
template <typename T>
inline void read(T& x) {
x = 0;
int f = 1;
char ch;
while (!isdigit(ch = getchar())) {
if (ch == '-') f = -1;
}
while (isdigit(ch)) {
x = x * 10 + (ch ^ 48);
ch = getchar();
}
x *= f;
}
template <typename T>
void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
int q, p, primes[N], sig[N], miu[N], phi[N];
bool is_prime[N];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
// ======
phi[1] = miu[1] = sig[1] = 1;
for (int i = 2; i <= 1000000; i++) {
for (int i = 2; i < N; i++) {
if (!is_prime[i]) {
prime[++p] = i;
primes[++p] = i;
phi[i] = i - 1;
miu[i] = -1;
sig[i] = i + 1;
}
for (int j = 1; j <= p && prime[j] * i <= 1000000; j++) {
is_prime[i * prime[j]] = 1, miu[i * prime[j]] = -miu[i];
phi[i * prime[j]] = phi[i] * phi[prime[j]];
sig[i * prime[j]] = sig[i] * sig[prime[j]];
if (i % prime[j] == 0) {
miu[i * prime[j]] = 0;
phi[i * prime[j]] = phi[i] * prime[j];
sig[i * prime[j]] -= sig[i / prime[j]] * prime[j];
for (int j = 1; j <= p && primes[j] * i < N; j++) {
is_prime[i * primes[j]] = 1;
miu[i * primes[j]] = -miu[i];
phi[i * primes[j]] = phi[i] * phi[primes[j]];
sig[i * primes[j]] = sig[i] * sig[primes[j]];
if (i % primes[j] == 0) {
miu[i * primes[j]] = 0;
phi[i * primes[j]] = phi[i] * primes[j];
sig[i * primes[j]] -= sig[i / primes[j]] * primes[j];
break;
}
}
}
read(q);
for (int i = 0; i < q; i++) {
read(op);
read(k);
// ======
cin >> q;
while (q--) {
int op, k;
cin >> op >> k;
if (op == 1) {
write(prime[k]);
putchar('\n');
} else {
write(sig[k]);
putchar(' ');
write(miu[k]);
putchar(' ');
write(phi[k]);
putchar('\n');
cout << primes[k] << endl;
} else { // op == 2
cout << sig[k] << ' ' << miu[k] << ' ' << phi[k] << endl;
}
}
return 0;
}