ZOJ 3868 GCD Expectation DP

dp[i] 表示公约数为i时有多少种组合

先预处理一遍dp[i]这是的dp[i]表示含有公约数i或者i的倍数的组合有多少个

再倒着dp dp[i] - = Sigma(dp[j]) (j是i的倍数 2i,3i,4i.....)

结果既为 Sigma[ dp[i]*pow(i,k) ]

GCD Expectation


Time Limit: 4 Seconds      Memory Limit: 262144 KB



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

Author: LIN, Xi

Source: The 15th Zhejiang University Programming Contest

Submit    Status

/* ***********************************************
Author        :CKboss
Created Time  :2015年04月13日 星期一 09时29分19秒
File Name     :I.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;
const LL mod=998244353LL;
const int maxn=2001000;

LL power(LL x,int n)
{
	LL e=1LL;
	while(n) { if(n&1) e=(e*x)%mod; x=(x*x)%mod; n/=2; }
	return e%mod;
}

int n,k,mx;
int vis[maxn];
LL two[maxn],dp[maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	two[0]=1LL;
	for(int i=1;i<maxn;i++) { two[i]=(two[i-1]*2LL)%mod; }

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%d",&n,&k);
		memset(vis,0,sizeof(vis)); memset(dp,0,sizeof(dp)); mx=0;
		for(int i=0;i<n;i++) { int x; scanf("%d",&x); mx=max(mx,x); vis[x]++; }
		dp[1]=(two[n]-1+mod)%mod;
		for(int i=2;i<=mx;i++)
		{
			int temp=0;
			for(int j=i;j<=mx;j+=i) if(vis[j]) temp+=vis[j];
			dp[i]=(two[temp]-1+mod)%mod;
		}
		for(int i=mx;i>=1;i--)
		{
			for(int j=i+i;j<=mx;j+=i)
				dp[i]=(dp[i]-dp[j]+mod)%mod;
		}
		LL sum=0;
		for(int i=1;i<=mx;i++)
			sum=(sum+(dp[i]*power(i,k))%mod)%mod;
		cout<<sum<<endl;
	}
    return 0;
}
时间: 2024-08-07 01:56:17

ZOJ 3868 GCD Expectation DP的相关文章

zoj.3868.GCD Expectation(数学推导&gt;&gt;容斥原理)

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 pick

ACM学习历程—ZOJ 3868 GCD Expectation(莫比乌斯 || 容斥原理)

Description 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, 

Zoj 3868 GCD Expectation

给一个集合,大小为n , 求所有子集的gcd 的期望和 . 期望的定义为 这个子集的最大公约数的K次方 : 每个元素被选中的概率是等可能的 即概率 p = (发生的事件数)/(总的事件数); 总的事件数 = 2^n -1; 大小为n的集合的非空子集个数为2^n -1 期望 = p(i) *i; = 1*p(1) + 2*p(2) + ... +n*p(n); 设x发生的事件数为 dp[x] , 则上式可化简为: =1*dp[1]/(2^n-1) + 2*dp[2]/(2^n-1) + ... +

[暴力统计] zoj 3868 GCD Expectation

题意: 给n和k,求n个数的任意非空子集gcd的k次方的期望. 最后期望乘上2^n-1 思路: 因为取每个子集都是等概率,所以取出每个子集的概率是1/(2^n-1) 然而最后的答案是乘上2^n-1 所以其实求的就是每个非空子集的gcd的k次方的和. 然后就是求法了. 我们可以把题目转换成求gcd等于i的非空集合有多少个. gcd从Max枚举到1,求出答案. 对于每个i,设n个数中是i的倍数的数有x个. 那么gcd等于i的个数就是总共的 (2^x-1)个减去gcd等于j的个数,j是i的倍数. 因此

zoj[3868]gcd期望

题意:求n个数组成的集合的所有非空子集的gcd的期望 大致思路:对于一个数x,设以x为约数的数的个数为cnt[x],所组成的非空集合个数有2^cnt[x]-1个,这其中有一些集合的gcd是x的倍数的,怎么求得最终结果呢?下面来说明过程. 令f[x] = 2^cnt[x]-1,表示以x为gcd的集合个数.令maxn为所有数的最大值,一开始f[maxn]=2^cnt[maxn]-1是肯定正确的.若从大到小更新f数组,类似数学归纳法,f[x]需要减去f[2x].f[3x].....f[px],px<=

ZOJ 3868(容斥原理+快速幂)

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

ZOJ GCD Expectation

#include <iostream> #include <cstring> #include <cstdlib> #include <algorithm> #include <string> #include <map> #include <cmath> #include <math.h> #include <cstdio> #define LL long long using namespace

ZOJ - 3822 Domination (DP)

Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboard with N rows and M columns. Every day after work, Edward will place a chess piec

[ACM] ZOJ 3725 Painting Storages (DP计数+组合)

Painting Storages Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to paint all storages with two colors: red and blue. Each storage will be painted with e