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 [ g c d ( x 1 , x 2 , … , x m ) ] 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). n , k ( 1 ≤ n , k ≤ 10 6 ) . The second line contains n integers a1,a2,…,an(1≤ai≤106). a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 10 6 ) .
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<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#define maxx 1000005
#define mod 998244353
using namespace std ;
vector<int> fac[maxx];
int num[maxx];
long long f[maxx];
long long P(long long a,long long b)
{
if(a==0)
return 0;
long long ans=1;
while(b)
{
if(b&1)
ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
int n,k;
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>k;
memset(num,0,sizeof(num));
memset(f,0,sizeof(f));
int maxn=1;
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
maxn=max(maxn,x);
num[x]++;
}
long long ans=0;
for(int g=maxn;g>=1;g--)
{
int cnt=0;
for(int j=g;j<=maxn;j+=g)
{
f[g]=(f[g]-f[j])%mod;
cnt+=num[j];//很巧妙的直接在这里统计有多少个是g的倍数。
}
f[g]=(f[g]+P(2,cnt)-1)%mod;
f[g]=(f[g]+mod)%mod;
ans=(ans+f[g]*P(g,k)%mod)%mod;
}
cout<<ans<<endl;
}
return 0;
}