From b634d12e32fd7a22800cb4ea405017b11c623c82 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 19 Jul 2022 19:40:13 +0800 Subject: [PATCH] P2398 GCD SUM https://www.luogu.com.cn/record/80288917 --- Luogu/P2398/P2398.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Luogu/P2398/P2398.cpp diff --git a/Luogu/P2398/P2398.cpp b/Luogu/P2398/P2398.cpp new file mode 100644 index 00000000..a7b9c93d --- /dev/null +++ b/Luogu/P2398/P2398.cpp @@ -0,0 +1,42 @@ +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int n, phi[N]; +long long sum[N], ans; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + phi[1] = 1; + for (int i = 2; i <= n; i++) { + phi[i] = i; + } + + for (int i = 2; i <= n; i++) { + if (phi[i] == i) { + for (int j = i; j < N; j += i) { + phi[j] = phi[j] / i * (i - 1); + } + } + } + + for (int i = 1; i <= n; i++) { + sum[i] = sum[i - 1] + phi[i]; + } + + for (int i = 1; i <= n; i++) { + ans += (sum[n / i] * 2 - 1) * i; + } + + cout << ans << endl; + + return 0; +}