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.
OutputFor each case, if the expectation is E, output a single integer denotes E · (2n - 1) modulo 998244353.
Sample Input1 5 1 1 2 3 4 5Sample 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;
}