ZOJ 3868 GCD Expectation(暴力统计技巧+一点数论知识)

本文探讨了一个关于整数集合的子集及其最大公约数(GCD)的期望值问题。通过使用动态规划(DP)技术和容斥原理,解决了一个随机选择非空子集时的 GCD 期望值计算问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GCD Expectation

Time Limit: 4 Seconds      Memory Limit: 262144 KB

Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, x2,…,xm} (each nonempty subset has equal probability to be picked), and would like to know the expectation of [gcd(x1, x2,…,xm)]k.

Note that gcd(x1, x2,…,xm) is the greatest common divisor of {x1, x2,…,xm}.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers n, k (1 ≤ n, k ≤ 106). The second line contains n integers a1, a2,…,an (1 ≤ ai ≤ 106).

The sum of values max{ai} for all the test cases does not exceed 2000000.

Output

For each case, if the expectation is E, output a single integer denotes E · (2n - 1) modulo 998244353.

Sample Input
1
5 1
1 2 3 4 5
Sample Output
42


一道中等难度的题目。。。
刚开始很愚蠢的想用暴力中的暴力,就是遍历,直到看到n开到了十万瞬间被打脸,
因为就算用状态压缩的遍历那就是说要搞个2^十万的数。。。。。
最后参考网上的题解,知道可以统计公因子为i的个数,然后循环i从1到Max(序列中最大数),
结合dp手段,和容斥定理的思想,爆破成功!!
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cstring>
using namespace std;
typedef long long LL;
#define maxn (2000005)
#define MOD 998244353
int n,k,seq[maxn];
int  dp[maxn],sum[maxn];
int Max;

long long Pow(long long x,long long  kk)//快速幂模板
{
    long long  result=1;
    while(kk)
    {
        if(kk&1) result=result*x%MOD;
        x=x*x%MOD;
        kk>>=1;
    }
    return result%MOD;
}//问题出现在数据范围上。。。
//取得模是超出整数范围的。。。(汗)

int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        memset(sum,0,sizeof(sum));
        Max=0;

        scanf("%d %d",&n,&k);
        for(int i=0;i<n;i++)
        {
            scanf( "%d",&seq[i] );
            Max=max(Max,seq[i]);
            sum[seq[i]]++;
        }

        long long ans=0;
        for(long long i=0;i<=Max;i++)  dp[i]=0;
        for(long long  i=Max;i>=1;i--)
        {
            long long  tmp=0;
            for(long long j=i;j<=Max;j+=i)
                tmp+=sum[j];
               // dp[i]=(dp[i]-dp[j]+MOD)%MOD;
            dp[i]=((dp[i]+Pow(2,tmp)-1)%MOD+MOD)%MOD;

            for(long long j=2*i;j<=Max;j+=i)
                dp[i]=(dp[i]-dp[j]+MOD)%MOD;//注意dp数组的时间性很重要(尤其是一维数组,一定要找准过去利用的状态和现在加工的状态)
            ans=(ans+dp[i]*Pow(i,k))%MOD;
        }//容斥定理用dp思想实现。
        cout<<ans<<endl;
    }
    return 0;
}
当然有个伏笔,这题肯定还不止如此,这题的函数是积性函数所以可以考虑莫比乌斯反演的推导方法
未完待续。。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值