From 5f44c714b6978b4221ce392bd7c08f0c967bf4f2 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Wed, 1 Sep 2021 23:01:11 +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/25484 --- S2OJ/168/168.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 S2OJ/168/168.cpp diff --git a/S2OJ/168/168.cpp b/S2OJ/168/168.cpp new file mode 100644 index 00000000..2d93ca89 --- /dev/null +++ b/S2OJ/168/168.cpp @@ -0,0 +1,76 @@ +#pragma GCC optimize("Ofast") + +#include + +using namespace std; + +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 main() { + phi[1] = miu[1] = sig[1] = 1; + for (int i = 2; i <= 1000000; i++) { + if (!is_prime[i]) { + prime[++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]; + break; + } + } + } + read(q); + for (int i = 0; i < q; i++) { + read(op); + read(k); + if (op == 1) { + write(prime[k]); + putchar('\n'); + } else { + write(sig[k]); + putchar(' '); + write(miu[k]); + putchar(' '); + write(phi[k]); + putchar('\n'); + } + } + return 0; +}