ZOJ-3868-GCD Expectation(容斥)

来源:互联网 发布:air打电话软件 编辑:程序博客网 时间:2024/06/04 23:18


GCD Expectation

Time Limit: 4 Seconds      Memory Limit: 262144 KB

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 denotesE · (2n - 1) modulo 998244353.

Sample Input

15 11 2 3 4 5

Sample Output

42



题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5480



题意 ,给出序列a[1],a[2]...a[i],求子序列 [gcd(x1x2,…,xm)]k.   的期望乘  (2n - 1)  ,   显然子序列个数有 (2n - 1) 个,那么其实就是所有子序列gcd的k次方求和啦

数据范围不超1e6,那么可以直接枚举gcd就好啦,dp[i]表示gcd为i的组数,从高到低枚举gcd,计算出i的倍数出现了m次,那么就有2^m-1组为i倍数的子序列,

显然,dp[i]=2^m-1-dp[i*2]-dp[i*3]-....;



//#include<bits/stdc++.h>#include <iostream>#include <cmath>#include <algorithm>#include <cstdio>#include <cstring>#define LL long longusing namespace std;const int MAXN = 1e6+7;const int MOD = 998244353;long long a[MAXN],dp[MAXN];long long quick_pow(long long a,long long n){    long long ans=1;    while(n)    {        if(n&1)ans*=a,ans%=MOD;        a=a*a%MOD;        n>>=1;    }    return ans;}int main(){    int T,n,k,temp;    cin>>T;    int cas=1;    while(T--)    {        memset(a,0,sizeof(a));        memset(dp,0,sizeof(dp));        scanf("%d%d",&n,&k);        int maxx=0,x;        for(int i=0; i<n; ++i)        {            scanf("%d",&x);            a[x]++;            maxx=max(maxx,x);        }        long long ans=0;        for(int i=maxx; i>=1; --i)        {            int cnt=0,temp=0;            for(int j=i; j<=maxx; j+=i)            {                cnt+=a[j];                temp=(temp-dp[j]+MOD)%MOD;            }            dp[i]=((quick_pow(2,cnt)-1)%MOD+temp)%MOD;            ans=(ans+dp[i]*quick_pow(i,k)%MOD)%MOD;        }        cout<<ans<<endl;    }    return 0;}







0 0
原创粉丝点击