GCD Expectation ZOJ - 3868(暴力求解)

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和k,求n个数的任意非空子集gcd的k次方的期望,最后期望乘上2^n-1
思路:因为取每个子集都是等概率,取每个集合的概率为1/(2^n-1),所以最后实际上求的是每个非空集合的gcd的k次方的和
我们可以把题目转化为求gcd等于i的非空集合有多少个。gcd从1到最大maxt枚举求出答案
对于每个i,设n个数中是i的倍数的数有x个
那么,gcd等于i的个数就是等于总共的2^x-1个减去gcd等于j的个数,j是i的倍数
即:dp[i]=2^x-1-dp[i*2]-dp[i*3] - .......,dp[i]b表示gcd为i的集合个数
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define mod 998244353
using namespace std;
typedef long long ll;
const int N = 2000200;
int sum[N],arr[N];
int dp[N];
ll quick(ll a,int b){
	ll ans=1;
	while(b){
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
	    memset(sum,0,sizeof(sum));
		memset(dp,0,sizeof(dp));
		int n,k,maxt=0;
		scanf("%d%d",&n,&k);
		for(int i=0;i<n;i++){
			scanf("%d",&arr[i]);
			maxt=max(maxt,arr[i]);
			sum[arr[i]]++; //每个数字出现的次数 
		}
		ll ans=0;
		for(int i=maxt;i>=1;i--){
			int tmp=0;
			for(int j=i;j<=maxt;j+=i){
				tmp+=sum[j];
				dp[i]=(dp[i]-dp[j]+mod)%mod;
			}
			dp[i]=((dp[i]+quick(2,tmp)-1)%mod+mod)%mod;
			ans=(ans+dp[i]*quick(i,k))%mod;
		}
		printf("%lld\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值