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;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/80138884
ZOJ
gcd