zoj 3868 GCD Expectation(容斥原理)

91 篇文章 2 订阅
6 篇文章 0 订阅

Edward has a set of n integers {a1a2,...,an}. He randomly picks a nonempty subset {x1x2,…,xm} (each nonempty subset has equal probability to be picked), and would like to know the expectation of [gcd(x1x2,…,xm)]k

Note that gcd(x1x2,…,xm) is the greatest common divisor of {x1x2,…,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 nk (1 ≤ nk ≤ 106). The second line contains n integers a1a2,…,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 denotes E · (2n - 1) modulo 998244353.

Sample Input
1
5 1
1 2 3 4 5
Sample Output
42

Edward has a set of n integers {a1a2,...,an}. He randomly picks a nonempty subset {x1x2,…,xm} (each nonempty subset has equal probability to be picked), and would like to know the expectation of [gcd(x1x2,…,xm)]k.

Note that gcd(x1x2,…,xm) is the greatest common divisor of {x1x2,…,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 nk (1 ≤ nk ≤ 106). The second line contains n integers a1a2,…,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 denotes E · (2n - 1) modulo 998244353. ((2n - 1) 其实不用考虑了)

Sample Input
1
5 1
1 2 3 4 5
Sample Output
42
这题一不小心就要超时了。。。
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#include <ctime>
#include <cstdlib> 
#define ll long long
#define mod 998244353
using namespace std; 
int num[1000001];    //把这两个数组放到main函数里就超时了。。 
int r[1000001];  
ll power(ll a, ll n) {
  ll r = 1;
  for (; n; n >>= 1) {
    if (n & 1) r = r * a % mod;
    a = a * a % mod;
  }
  return r;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		memset(r,0,sizeof(r)); //我把r换成map居然超时了。。
		int n,m,a;
		scanf("%d%d",&n,&m);
		int max=0;
		for(int i=0;i<n;++i){
			scanf("%d",&a);
			r[a]++;
			if(a>max)
				max=a;
		}
		for(int i=max;i>=1;--i){ //枚举gcd的可能值(倒序)
			int sum=0;
			num[i]=0;
			for(int j=i;j<=max;j+=i){ //求它的倍数 
				sum+=r[j]; //符合条件的元素个数 
				num[i]=(num[i]-num[j]+mod)%mod; //我们求的只有gcd=i而不包括它的倍数 
			}
			num[i]=(num[i]+power(2,sum)-1)%mod; //power(2,sum)-1求长度为sum的非空子集个数 
		}
		int s=0;
		for(int i=1;i<=max;++i){ //gcd
			s=(s+power(i,m)*num[i]%mod)%mod; 
		}
		printf("%d\n",s);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值