A Simple Math Problem

A Simple Math Problem

https://ac.nowcoder.com/acm/contest/8827/A

A Simple Math Problem

图片说明

/*
  Author : lifehappy
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;

ll prime[N], phi[N], mu[N], f[N], g[N], cnt, n;

bool st[N];

void init() {
    phi[1] = mu[1] = f[1] = 1;
    for(int i = 2; i <= n; i++) {
        f[i] = f[i / 10] + i % 10;
        if(!st[i]) {
            prime[++cnt] = i;
            phi[i] = i - 1;
            mu[i] = -1;
        }
        for(int j = 1; j <= cnt && 1ll * i * prime[j] <= n; j++) {
            st[i * prime[j]] = 1;
            if(i % prime[j] == 0) {
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            }
            phi[i * prime[j]] = phi[i] * (prime[j] - 1);
            mu[i * prime[j]] = -mu[i];
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = i; j <= n; j += i) {
            g[i] += f[j];
        }
    }
}

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    scanf("%lld", &n);
    init();
    ll ans1 = 0, ans2 = 0;
    for(int i = 1; i <= n; i++) {
        ans1 += mu[i] * (n / i) * g[i];
        ans2 += f[i] * phi[i];
    }
    printf("%lld\n", ans1 - ans2 + 1);
    return 0;
}
全部评论

相关推荐

11-04 21:17
江南大学 Java
穷哥们想卷进大厂:肯定会问技术呀,面试你的可能是别人
点赞 评论 收藏
分享
评论
7
收藏
分享
牛客网
牛客企业服务