From f50cbec887136d849c91bc9eb3b33cca4513e504 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 30 Mar 2023 11:08:10 +0800 Subject: [PATCH] =?UTF-8?q?#168.=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E7=BA=BF=E6=80=A7=E7=AD=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/74744 --- S2OJ/168/168.cpp | 99 +++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 56 deletions(-) diff --git a/S2OJ/168/168.cpp b/S2OJ/168/168.cpp index 2d93ca89..af9cbb94 100644 --- a/S2OJ/168/168.cpp +++ b/S2OJ/168/168.cpp @@ -1,76 +1,63 @@ -#pragma GCC optimize("Ofast") +#include -#include +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 -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 -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; }