HDU 6833 A Very Easy Math Problem(莫比乌斯反演)

原题题面

Given you n,x,k , find the value of the following formula:
∑ 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}(\prod_{j=1}^{x}a_{j}^{k})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)
g c d ( a 1 , a 2 , … , a n ) gcd(a_1,a_2,…,a_n) gcd(a1,a2,,an) is the greatest common divisor of a 1 , a 2 , . . . , a − n a_1,a_2,...,a-n a1,a2,...,an.
The function f ( x ) f(x) f(x) is defined as follows:
If there exists an ingeter k k k ( k > 1 ) (k>1) (k>1) , and k 2 k^2 k2 is a divisor of x x x,
then f ( x ) = 0 f(x)=0 f(x)=0, else f ( x ) = 1 f(x)=1 f(x)=1.

输入格式

The first line contains three integers t , k , x ( 1 ≤ t ≤ 1 0 4 , 1 ≤ k ≤ 1 0 9 , 1 ≤ x ≤ 1 0 9 ) t,k,x (1≤t≤10^4,1≤k≤10^9,1≤x≤10^9) t,k,x(1t104,1k109,1x109)
Then t t t test cases follow. Each test case contains an integer n n n ( 1 ≤ n ≤ 2 × 1 0 5 ) (1≤n≤2×10^5) (1n2×105)

输出格式

For each test case, print one integer — the value of the formula.
Because the answer may be very large, please output the answer modulo 1 0 9 + 7 10^9+7 109+7.

输入样例

3 1 3
56
5
20

输出样例

139615686
4017
11554723

题面分析

g c d ( a 1 , a 2 , . . . a x ) = d gcd(a_1,a_2,...a_x)=d gcd(a1,a2,...ax)=d,得
∑ a 1 = 1 n ∑ a 2 = 1 n . . . ∑ a x = 1 n ( ∏ j = 1 x a j k ) f ( d ) ∗ d [ g c d ( a 1 , a 2 , . . . a x ) = d ] \sum_{a_1=1}^{n}\sum_{a_2=1}^{n}...\sum_{a_x=1}^{n}(\prod_{j=1}^{x}a_{j}^{k})f(d)*d[gcd(a_1,a_2,...a_x)=d] a1=1na2=1n...ax=1n(j=1xajk)f(d)d[gcd(a1,a2,...ax)=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}^{n}d*f(d)*\sum_{a_1=1}^{n}\sum_{a_2=1}^{n}...\sum_{a_x=1}^{n}(\prod_{j=1}^{x}a_{j}^{k})[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}^{n}d^{kx+1}*f(d)*\sum_{a_1=1}^{\lfloor \frac{n}{d}\rfloor}\sum_{a_2=1}^{\lfloor \frac{n}{d}\rfloor}...\sum_{a_x=1}^{\lfloor \frac{n}{d}\rfloor}(\prod_{j=1}^{x}a_{j}^{k})[gcd(a_1,a_2,...a_x)=1] d=1ndkx+1f(d)a1=1dna2=1dn...ax=1dn(j=1xajk)[gcd(a1,a2,...ax)=1]
[ g c d ( a 1 , a 2 , . . . a x ) = 1 ] [gcd(a_1,a_2,...a_x)=1] [gcd(a1,a2,...ax)=1]化作 μ \mu μ,得到
∑ 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}^{n}d^{kx+1}*f(d)*\sum_{a_1=1}^{\lfloor \frac{n}{d}\rfloor}\sum_{a_2=1}^{\lfloor \frac{n}{d}\rfloor}...\sum_{a_x=1}^{\lfloor \frac{n}{d}\rfloor}(\prod_{j=1}^{x}a_{j}^{k})\sum_{t|a_1,t|a_2,...t|a_x}\mu(t) d=1ndkx+1f(d)a1=1dna2=1dn...ax=1dn(j=1xajk)ta1,ta2,...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}^{n}d^{kx+1}*f(d)*(\sum_{i=1}^{\lfloor \frac{n}{d}\rfloor}i^k)^x*\sum_{t|a_1,t|a_2,...t|a_x}\mu(t) d=1ndkx+1f(d)(i=1dnik)xta1,ta2,...taxμ(t)
枚举 t t t
∑ d = 1 n d k x + 1 ∗ t k x ∗ f ( d ) ∗ ∑ t = 1 ⌊ n d ⌋ μ ( t ) ( ∑ i = 1 ⌊ n d t ⌋ i k ) x \sum_{d=1}^{n}d^{kx+1}*t^{kx}*f(d)*\sum_{t=1}^{\lfloor \frac{n}{d}\rfloor}\mu(t)(\sum_{i=1}^{\lfloor \frac{n}{dt}\rfloor}i^k)^x d=1ndkx+1tkxf(d)t=1dnμ(t)(i=1dtnik)x
T = t d T=td T=td,得到
∑ T = 1 n T k x d f ( d ) ∗ ( ∑ i = 1 ⌊ n T ⌋ i k ) x ∗ ∑ t ∣ T μ ( t ) \sum_{T=1}^{n}T^{kx}df(d)*(\sum_{i=1}^{\lfloor \frac{n}{T}\rfloor}i^k)^x*\sum_{t|T}\mu(t) T=1nTkxdf(d)(i=1Tnik)xtTμ(t)
为了方便计算,我们把右边的 t ∣ T t|T tT改成 d ∣ T d|T dT,得到
∑ T = 1 n T k x ( ∑ i = 1 ⌊ n T ⌋ i k ) x ∗ ∑ d ∣ T μ ( d ) d f ( T d ) \sum_{T=1}^{n}T^{kx}(\sum_{i=1}^{\lfloor \frac{n}{T}\rfloor}i^k)^x*\sum_{d|T}\mu(d)df(\frac{T}{d}) T=1nTkx(i=1Tnik)xdTμ(d)df(dT)
G ( x ) = ∑ d ∣ T μ ( d ) d f ( T d ) G(x)=\sum_{d|T}\mu(d)df(\frac{T}{d}) G(x)=dTμ(d)df(dT)
首先 μ \mu μ可以 O ( n ) O(n) O(n)预处理, f f f也可以 O ( n l o g n ) O(nlogn) O(nlogn)预处理
那么G可以在 O ( n ) O(n) O(n)下预处理
对于 ( ∑ i = 1 ⌊ n T ⌋ i k ) x (\sum_{i=1}^{\lfloor \frac{n}{T}\rfloor}i^k)^x (i=1Tnik)x,也可以在 O ( n l o g n ) O(nlogn) O(nlogn)下处理完
对于每一次查询,利用分块和预处理前缀和,只需要 O ( n ) O(\sqrt{n}) O(n )
故总复杂度为 O ( n l o g n + T n ) O(nlogn+T\sqrt{n}) O(nlogn+Tn )

AC代码(2199ms)

#include 
using namespace std;
const long long maxn=2e5;
const long long mod=1e9+7;
bool check[maxn+10];//访问标记
int prime[maxn+10];//质数
int mu[maxn+10];//mu函数
int f[maxn+10];//f函数
long long powk[maxn+10];
long long G[maxn+10];
long long sumG[maxn+10];
long long n, k, x;
inline long long quick_pow(long long a, long long b)//快速幂
{
    long long ans=1, base=a;
    while(b!=0)
    {
        if (b&1)
            ans=(long long) ans*base%mod;
        base=(long long) base*base%mod;
        b>>=1;
    }
    return ans;
}
void init()
{
    f[1]=mu[1]=1;
    int tot=0;
    for(int i=2; i<=maxn; i++)
    {
        f[i]=1;
        if (!check[i])
        {
            prime[tot++]=i;
            mu[i]=-1;
        }
        for(int j=0; j<tot; j++)
        {
            if (i*prime[j]>maxn) break;
            check[i*prime[j]]=true;
            if (i%prime[j]==0)
            {
                mu[i*prime[j]]=0;
                break;
            }
            else
            {
                mu[i*prime[j]]=-mu[i];
            }
        }
    }
    for(long long d=2; d<=maxn; d++)//处理f
    {
        for(long long j=d*d; j<=maxn; j+=d*d)
        {
            f[j]=0;
        }
    }
    G[0]=0;
    for(long long d=1; d<=maxn; d++)//处理G
    {
        for(long long i=d; i<=maxn; i+=d)
        {
            G[i]=(G[i]+d*f[d]%mod*mu[i/d]%mod+mod)%mod;
        }
    }
    powk[0]=0;
    sumG[0]=0;
    for(long long i=1; i<=maxn; i++)//处理累加
    {
        long long z=quick_pow(i, k);
        powk[i]=(powk[i-1]+z)%mod;
        sumG[i]=(sumG[i-1]+quick_pow(z, x)*G[i]%mod)%mod;
    }
}
long long answer()
{
    long long ans=0;
    for(long long l=1, r; l<=n; l=r+1)
    {
        r=n/(n/l);
        ans=(ans+(sumG[r]-sumG[l-1]+mod)%mod*quick_pow(powk[n/l], x)%mod)%mod;
    }
    return ans;
}
void solve()
{
    int t;
    scanf("%d%lld%lld", &t, &k, &x);
    init();
    while(t--)
    {
        scanf("%lld", &n);
        printf("%lld\n", answer());
    }
}
int main()
{
//    ios_base::sync_with_stdio(false);
//    cin.tie(0);
//    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    long long test_index_for_debug=1;
    char acm_local_for_debug;
    while(cin>>acm_local_for_debug)
    {
        cin.putback(acm_local_for_debug);
        if (test_index_for_debug>100)
        {
            throw runtime_error("Check the stdin!!!");
        }
        auto start_clock_for_debug=clock();
        solve();
        auto end_clock_for_debug=clock();
        cout<<"\nTest "<<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;
    }
#else
    solve();
#endif
    return 0;
}

DrGilbert 2020.8.7

你可能感兴趣的:(数论,ACM,莫比乌斯反演)