GCD Expectation ZOJ - 3868 (容斥)

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(1n,k106). n , k ( 1 ≤ n , k ≤ 10 6 ) . The second line contains n integers a1,a2,,an(1ai106). 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值