牛客小白月赛12 D 月月给华华出题 莫比乌斯反演

原题链接:https://ac.nowcoder.com/acm/contest/392/D

目录

  • 题意
  • 化简
  • Code

题意

求 ∑ i = 1 n i g c d ( i , n ) 求\sum_{i=1}^{n}\frac{i}{gcd(i,n)} i=1ngcd(i,n)i

化简

∑ i = 1 n i g c d ( i , n ) \sum_{i=1}^{n}\frac{i}{gcd(i,n)} i=1ngcd(i,n)i

= ∑ d ∣ n ∑ i = 1 n i d [ g c d ( i , n ) = d ] =\sum_{d|n}\sum_{i=1}^{n}\frac{i}{d}[gcd(i,n)=d] =dni=1ndi[gcd(i,n)=d]

= ∑ d ∣ n ∑ i = 1 n / d i [ g c d ( i , n d ) = 1 ] =\sum_{d|n}\sum_{i=1}^{n/d}i[gcd(i,\frac{n}{d})=1] =dni=1n/di[gcd(i,dn)=1]

= ∑ d ∣ n ∑ i = 1 d i [ g c d ( i , d ) = 1 ] =\sum_{d|n}\sum_{i=1}^{d}i[gcd(i,d)=1] =dni=1di[gcd(i,d)=1]

= ∑ d ∣ n ∑ i = 1 d i ∑ t ∣ i , t ∣ d μ ( t ) =\sum_{d|n}\sum_{i=1}^{d}i\sum_{t|i,t|d}\mu(t) =dni=1diti,tdμ(t)

= ∑ d ∣ n ∑ t ∣ d μ ( t ) ∑ i = 1 d / t i t =\sum_{d|n}\sum_{t|d}\mu(t)\sum_{i=1}^{d/t}it =dntdμ(t)i=1d/tit

= ∑ d ∣ n ∑ t ∣ d μ ( t ) t d / t ( d / t + 1 ) 2 =\sum_{d|n}\sum_{t|d}\mu(t)t\frac{d/t(d/t+1)}{2} =dntdμ(t)t2d/t(d/t+1)

= 1 2 ∑ d ∣ n ( ∑ t ∣ d μ ( t ) d 2 t + ∑ t ∣ d μ ( t ) d ) =\frac{1}{2}\sum_{d|n}(\sum_{t|d}\mu(t)\frac{d^2}{t}+\sum_{t|d}\mu(t)d) =21dn(tdμ(t)td2+tdμ(t)d)

= 1 2 ∑ d ∣ n d ( ∑ t ∣ d μ ( t ) d t + ∑ t ∣ d μ ( t ) ) =\frac{1}{2}\sum_{d|n}d(\sum_{t|d}\mu(t)\frac{d}{t}+\sum_{t|d}\mu(t)) =21dnd(tdμ(t)td+tdμ(t))

= 1 2 ∑ d ∣ n d ( ϕ ( d ) + ϵ ( d ) ) =\frac{1}{2}\sum_{d|n}d(\phi(d)+\epsilon(d)) =21dnd(ϕ(d)+ϵ(d))

Code

#include 
#pragma comment(linker, "/STACK:16777216")
using namespace std;
//#define ACM_LOCAL
#define re register
#define fi first
#define se second
#define please_AC return 0
const int N = 1e6 + 10;
const int M = 1e6 + 10;
const int INF = 1e9;
const double eps = 1e-4;
const int MOD = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
ll prime[N], k, phi[N], f[N];
bool is_prime[N];
inline void init(int n) {
    memset(is_prime, true, sizeof is_prime);
    phi[1] = 1;
    for (int i = 2; i < n; ++i) {
        if (is_prime[i]) prime[++k] = i, phi[i] = i-1;
        for (int j = 1; j <= k && i * prime[j] < n; ++j) {
            is_prime[i * prime[j]] = false;
            if (i % prime[j] == 0) {
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            } else {
                phi[i * prime[j]] = phi[i] * (prime[j] - 1);
            }
        }
    }
    for (int d = 1; d <= n; d++) {
        for (int i = d; i <= n; i += d) {
            f[i] += d * (phi[d] + (d == 1)) / 2;
        }
    }
}
void solve() {
    init(N);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) printf("%lld\n", f[i]);

}

signed main() {
#ifdef ACM_LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    solve();
    return 0;
}

你可能感兴趣的:(数论)