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
42The meaning of the question: Given n and k, find the expectation of the k-th power of any non-empty subset gcd of n numbers, and finally multiply the expectation by 2^n-1
Thought: because each subset is equal probability, take each set The probability of is 1/(2^n-1), so in the end, what is actually calculated is the sum of the k-th power of gcd of each non-empty set.
We can convert the problem to find how many non-empty sets with gcd equal to i are there . gcd enumerates from 1 to the maximum maxt to find the answer.
For each i, let there be x number of n numbers that are multiples of i.
Then, the number of gcd equal to i is equal to the total of 2^x-1 minus gcd is equal to the number of j, j is a multiple of i
namely : dp[i]=2^x-1-dp[i*2]-dp[i*3] - .......,dp[i ]b indicates the number of sets whose gcd is 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]]++; //The number of occurrences of each number } 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; }