I - 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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
const int mod=998244353;
ll num_mul[maxn],sum[maxn];
int val[maxn];
/*
数量也必须同时取余,因为后面有可能导致这个值变得很“大 ”,导致没有办法计算
利用容斥原理,感觉更像是筛选,i 去掉i的倍数 
*/

void init(){
	memset(num_mul,0,sizeof(num_mul));
	memset(sum,0,sizeof(sum));
}

ll q_pow(ll base,int n){
	ll ans=1;
	while(n){
		if(n&1){
			ans=(ans*base)%mod;
		}
		base=(base*base)%mod;
		n>>=1;
	}
	return ans;
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		init();
		int n,k,mv=0;
		scanf("%d %d",&n,&k);
		for(int i=1;i<=n;i++){
			scanf("%d",&val[i]);
			sum[val[i]]++;
			mv=max(mv,val[i]);
		}
		ll ans=0;
		for(int i=mv;i>=1;i--){
			int cnt=0;
			for(int j=i;j<=mv;j+=i){
				cnt+=sum[j];
				num_mul[i]=(num_mul[i]-num_mul[j]+mod)%mod;;
			}
			num_mul[i]=(num_mul[i]+q_pow((ll)2,cnt)-1)%mod;
			ans=(ans+num_mul[i]*q_pow((ll)i,k))%mod;
		}
		printf("%lld\n",ans);
	}	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值