【2020杭电多校第六场】 6833 Very Easy Math Problem

    科技2024-08-07  25

    【2020杭电多校第六场】 6833 Very Easy Math Problem 莫比乌斯反演

    题意思路Code(2121MS)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6833

    借鉴大佬: https://blog.csdn.net/weixin_44282912/article/details/107844614 https://blog.csdn.net/oampamp1/article/details/107865300

    题意

    求 解 ∑ a 1 = 1 n ∑ a 2 = 1 n . . . ∑ a x = 1 n ( ∏ j = 1 x a j k ) f ( g c d ( a 1 , a 2 . . . a x ) ) g c d ( a 1 , a 2 . . . a x ) 求解\sum_{a_1=1}^n\sum_{a_2=1}^n...\sum_{a_x=1}^n\left(\prod_{j=1}^xa_j^k\right)f(gcd(a_1,a_2...a_x))gcd(a_1,a_2...a_x) a1=1na2=1n...ax=1n(j=1xajk)f(gcd(a1,a2...ax))gcd(a1,a2...ax)

    思路

    令 d = g c d ( a 1 , a 2 . . . a x ) 得 : 令d=gcd(a_1,a_2...a_x)得: d=gcd(a1,a2...ax)

    ∑ a 1 = 1 n ∑ a 2 = 1 n . . . ∑ a x = 1 n ( ∏ j = 1 x a j k ) f ( d ) d \sum_{a_1=1}^n\sum_{a_2=1}^n...\sum_{a_x=1}^n\left(\prod_{j=1}^xa_j^k\right)f(d)d a1=1na2=1n...ax=1n(j=1xajk)f(d)d

    枚 举 d 得 : 枚举d得: d

    ∑ d = 1 n d f ( d ) ∑ a 1 = 1 n ∑ a 2 = 1 n . . . ∑ a x = 1 n ( ∏ j = 1 x a j k ) [ g c d ( a 1 , a 2 . . . a x ) = d ] \sum_{d=1}^ndf(d)\sum_{a_1=1}^n\sum_{a_2=1}^n...\sum_{a_x=1}^n\left(\prod_{j=1}^xa_j^k\right)[gcd(a_1,a_2...a_x)=d] d=1ndf(d)a1=1na2=1n...ax=1n(j=1xajk)[gcd(a1,a2...ax)=d]

    ∑ d = 1 n d k x + 1 f ( d ) ∑ a 1 = 1 ⌊ n d ⌋ ∑ a 2 = 1 ⌊ n d ⌋ . . . ∑ a x = 1 ⌊ n d ⌋ ( ∏ j = 1 x a j k ) [ g c d ( a 1 , a 2 . . . a x ) = 1 ] \sum_{d=1}^nd^{kx+1}f(d)\sum_{a_1=1}^{\left \lfloor \frac{n}{d} \right \rfloor }\sum_{a_2=1}^{\left \lfloor \frac{n}{d} \right \rfloor }...\sum_{a_x=1}^{\left \lfloor \frac{n}{d} \right \rfloor }\left(\prod_{j=1}^xa_j^k\right)[gcd(a_1,a_2...a_x)=1] d=1ndkx+1f(d)a1=1dna2=1dn...ax=1dn(j=1xajk)[gcd(a1,a2...ax)=1]

    ∑ d = 1 n d k x + 1 f ( d ) ∑ a 1 = 1 ⌊ n d ⌋ ∑ a 2 = 1 ⌊ n d ⌋ . . . ∑ a x = 1 ⌊ n d ⌋ ( ∏ j = 1 x a j k ) ∑ t ∣ a 1    t ∣ a 2    . . .    t ∣ a x μ ( t ) \sum_{d=1}^nd^{kx+1}f(d)\sum_{a_1=1}^{\left \lfloor \frac{n}{d} \right \rfloor }\sum_{a_2=1}^{\left \lfloor \frac{n}{d} \right \rfloor }...\sum_{a_x=1}^{\left \lfloor \frac{n}{d} \right \rfloor }\left(\prod_{j=1}^xa_j^k\right)\sum_{t|a_1\;t|a2\;...\;t|a_x}\mu(t) d=1ndkx+1f(d)a1=1dna2=1dn...ax=1dn(j=1xajk)ta1ta2...taxμ(t)

    ∑ d = 1 n d k x + 1 f ( d ) ( ∑ i = 1 ⌊ n d ⌋ i k ) x ∑ t ∣ a 1    t ∣ a 2    . . .    t ∣ a x μ ( t ) \sum_{d=1}^nd^{kx+1}f(d)\left (\sum_{i=1}^{\left \lfloor \frac{n}{d} \right \rfloor }i^k\right)^x\sum_{t|a_1\;t|a2\;...\;t|a_x}\mu(t) d=1ndkx+1f(d)i=1dnikxta1ta2...taxμ(t)

    枚 举 t 得 : 枚举t得: t

    ∑ d = 1 n d k x + 1 f ( d ) ∑ t = 1 ⌊ n d ⌋ μ ( t ) ( ∑ i = 1 ⌊ n d t ⌋ ( i t ) k ) x \sum_{d=1}^nd^{kx+1}f(d)\sum_{t=1}^{\left \lfloor \frac{n}{d} \right \rfloor }\mu(t)\left (\sum_{i=1}^{\left \lfloor \frac{n}{dt} \right \rfloor }(it)^k\right)^x d=1ndkx+1f(d)t=1dnμ(t)i=1dtn(it)kx

    ∑ d = 1 n d k x + 1 f ( d ) ∑ t = 1 ⌊ n d ⌋ μ ( t ) t k x ( ∑ i = 1 ⌊ n d t ⌋ i k ) x \sum_{d=1}^nd^{kx+1}f(d)\sum_{t=1}^{\left \lfloor \frac{n}{d} \right \rfloor }\mu(t)t^{kx}\left (\sum_{i=1}^{\left \lfloor \frac{n}{dt} \right \rfloor }i^k\right)^x d=1ndkx+1f(d)t=1dnμ(t)tkxi=1dtnikx

    令 T = d t 并 枚 举 T 得 : 令T=dt并枚举T得: T=dtT

    ∑ T = 1 n ( ∑ i = 1 ⌊ n d t ⌋ i k ) x T k x ∑ d ∣ T d f ( d ) μ ( T d ) \sum_{T=1}^n\left (\sum_{i=1}^{\left \lfloor \frac{n}{dt} \right \rfloor }i^k\right)^xT^{kx}\sum_{d|T}df(d)\mu(\frac{T}{d}) T=1ni=1dtnikxTkxdTdf(d)μ(dT)

    令 g ( T ) = ∑ d ∣ T d f ( d ) μ ( T d ) , 则 令g(T)=\sum_{d|T}df(d)\mu(\frac{T}{d}),则 g(T)=dTdf(d)μ(dT)

    ∑ T = 1 n ( ∑ i = 1 ⌊ n T ⌋ i k ) x T k x g ( T ) \sum_{T=1}^n\left (\sum_{i=1}^{\left \lfloor \frac{n}{T} \right \rfloor }i^k\right)^xT^{kx}g(T) T=1ni=1TnikxTkxg(T)

    O ( n l o g n ) 筛 f 和 g , 并 对 T k x g ( T ) 和 ∑ i = 1 ⌊ n T ⌋ i k 做 前 缀 和 , 最 后 n 分 块 计 算 , 复 杂 度 为 O ( n l o g n + T n ) \red{O(nlogn)筛f和g,并对T^{kx}g(T)和\sum_{i=1}^{\left \lfloor \frac{n}{T} \right \rfloor }i^k做前缀和,最后\sqrt n分块计算,复杂度为O(nlogn+T\sqrt n)} O(nlogn)fgTkxg(T)i=1Tnikn O(nlogn+Tn )

    Code(2121MS)

    #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pdd; #define INF 0x3f3f3f3f #define lowbit(x) x & (-x) #define mem(a, b) memset(a , b , sizeof(a)) #define FOR(i, x, n) for(int i = x;i <= n; i++) // const ll mod = 998244353; const ll mod = 1e9 + 7; // const double eps = 1e-6; // const double pi = acos(-1); const int N = 2e5 + 10; ll T, k, x; int mu[N]; // 莫比乌斯函数 bool is_prime[N]; int prime[N]; int cnt; ll g[N], f[N]; ll sumG[N], sumi[N]; ll quick_pow(ll a, ll b) { ll ans = 1; while(b) { if(b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans % mod; } void Mobi() // 莫比乌斯函数初始化 { f[1] = mu[1] = 1; is_prime[0] = is_prime[1] = true; for(int i = 2;i < N; i++) { f[i] = 1; if (!is_prime[i]) { mu[i] = -1; prime[++cnt] = i; } for (int j = 1; j <= cnt && i * prime[j] < N; j++) { is_prime[i * prime[j]] = true; if (i % prime[j] == 0) { mu[i * prime[j]] = 0; break; } mu[i * prime[j]] = -mu[i]; } } // 筛f函数 for(int d = 2;d * d < N; d++) { for(int i = d * d;i < N; i += d * d) { f[i] = 0; } } // 筛g函数 for(int d = 1;d < N; d++) { for(int i = d;i < N; i += d) { g[i] = (g[i] + 1ll * d * f[d] % mod * mu[i / d] % mod + mod) % mod; } } // 处理sumG函数和sumi函数前缀和 for(int i = 1;i < N; i++) { ll t = quick_pow(i, k); sumi[i] = (sumi[i - 1] + t) % mod; sumG[i] = (sumG[i - 1] + quick_pow(t, x) * g[i] % mod) % mod; } } void solve() { cin >> T >> k >> x; Mobi(); while(T--) { int n; cin >> n; ll ans = 0; for(int l = 1, r;l <= n; l = r + 1) { r = min(n, n / (n / l)); ans = (ans + (sumG[r] - sumG[l - 1] + mod) % mod * quick_pow(sumi[n / l], x) % mod) % mod; } cout << ans << endl; } } signed main() { ios_base::sync_with_stdio(false); //cin.tie(nullptr); //cout.tie(nullptr); #ifdef FZT_ACM_LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); signed test_index_for_debug = 1; char acm_local_for_debug = 0; do { if (acm_local_for_debug == '$') exit(0); if (test_index_for_debug > 20) throw runtime_error("Check the stdin!!!"); auto start_clock_for_debug = clock(); solve(); auto end_clock_for_debug = clock(); cout << "Test " << test_index_for_debug << " successful" << endl; cerr << "Test " << test_index_for_debug++ << " Run Time: " << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl; cout << "--------------------------------------------------" << endl; } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug)); #else solve(); #endif return 0; }
    Processed: 0.014, SQL: 8