GCD Expectation
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
Title:
Given n numbers {a1,a2,a3...an}, let us sum the k-th power of the greatest common divisor of any subset of this set
Ideas:
We can convert the problem into finding the number of non-empty sets with gcd equal to i, so that we can calculate directly.
We store the largest of the n numbers as Max when inputting, because the greatest common factor of these n numbers must not exceed Max, and record the number of each number
gcd i enumerates from Max to 1
For each greatest common factor i, we ask for the number of multiples of i. The inner loop starts from i and increases exponentially to Max +i each time, because we have recorded the number of each number. , nothing is zero, so the number of multiples of i can be obtained by directly adding in the loop process
Suppose there are x numbers of n numbers that are multiples of i.
Then the number of gcd equal to i is the total (2^x-1) (all sets with i as a multiple) minus the number of gcd equal to j, j is a multiple of i (that is, the greatest common factor is i number of multiples), which is equivalent to the simplest case of the inclusion-exclusion theorem.
After the last gcd is the number of i dp[i], directly find dp[i]*q_pow(i,k) and add it to the answer to take the modulo
code:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const int mod = 998244353; const int maxn = 1e6+100; int sum[maxn];//The number of each number given int a[maxn];//The given array int dp[maxn];//dp[i] is the number of numbers whose i is the greatest common factor ll q_pow(ll a,ll b){ ll ans = 1; while(b){ if(b & 1) years = years * a % mod; b >>= 1; a = a * a % mod; } return ans; } int main(){ int t; scanf("%d",&t); while(t--){ memset(sum,0,sizeof(sum)); memset(dp,0,sizeof(dp)); int n,k,Max = 0; scanf("%d%d",&n,&k); for(int i = 0; i < n; i++){ scanf("%d",&a[i]); Max = max(a[i],Max); sum[a[i]]++; } ll ans = 0; for(int i = Max; i >= 1; i--){ int cnt = 0; for(int j = i; j <= Max; j += i){ cnt += sum[j];//How many multiples of i are recorded dp[i] = (dp[i] - dp[j] + mod) % mod; //Calculate how many numbers have i as the greatest common factor, subtract the total set of multiples of i //The number of the greatest common factor which is a multiple of i } dp[i] = ((dp[i] + q_pow(2,cnt) - 1) % mod + mod) % mod; ans = (ans + dp[i] * q_pow(i,k)) % mod;//Calculate the k power of gcd, and there are dp[i] } printf("%lld\n",ans); } return 0; }