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) + ... +n*dp[n]/(2^n-1);

=1/(2^n-1)*(1*dp[1] + 2*dp[2] + ... + n*dp[n]);

题目要求最后所得结果乘以 (2^n-1);

所以式子最后化简为:1*dp[1] + 2*dp[2] + ... + n*dp[n]

即问题转化为求gcd = i 的子集数

假设gcd = m*i (m = 0,1,2,3,... && m*i <= max_num)的个数为dp[i]个

那么gcd = i 的个数则为 for(int j= i + i ; j <= max_num ; j += i) dp[i]-=dp[j] ;

则期望为:dp[1] * 1^k + dp[2] * 2^k + ... dp[i] * i^k ;

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <iostream>
 6 #include <map>
 7 #include <list>
 8 #include <queue>
 9 #include <stack>
10 #include <string>
11 #include <algorithm>
12 #include <iterator>
13 using namespace std;
14 #define MAXN 1000010
15 #define INF 0x3f3f3f3f
16 #define MOD 998244353
17 #define eps 1e-6
18 #define LL long long
19 int num[MAXN];
20 LL dp[MAXN];
21 //dp[i] = 2^x -1 ; gcd = n*i;
22 //for(int j = i ; j <= max_num ; j += i) dp[i] -= dp[j];
23 LL qpow(LL x , LL k)
24 {
25     LL res=1;
26     while(k)
27     {
28         if(k & 1) res = res * x % MOD;
29         x = x * x % MOD;
30         k >>= 1;
31     }
32     return res;
33 }
34
35 int main()
36 {
37     int T;
38     int n,k;
39     LL ans;
40     scanf("%d",&T);
41     while(T--)
42     {
43         scanf("%d %d",&n,&k);
44         int x;
45         int max_num = 0;
46         int cunt = 0;
47         memset(num , 0 , sizeof(num));
48         memset(dp , 0 , sizeof(dp));
49         for(int i = 0 ; i < n ; i ++)
50         {
51             scanf("%d",&x);
52             num[x] ++;
53             max_num = max(x , max_num);
54         }
55
56         ans = 0;
57         for(int i = max_num ; i >= 1 ; i --)
58         {
59             cunt = 0;
60             dp[i] = 0;
61             for(int j = i ; j <= max_num ; j += i)
62             {
63                 cunt += num[j];
64                 if(j > i) dp[i] = (dp[i] - dp[j] + MOD) % MOD;
65             }
66             dp[i] = (dp[i] + qpow(2 , cunt) - 1 + MOD) % MOD;
67             ans = (ans + (dp[i] * qpow(i , k)) % MOD ) % MOD;
68         }
69         printf("%d\n",(int)ans);
70     }
71     return 0;
72 }

时间: 2024-10-10 10:38:44

Zoj 3868 GCD Expectation的相关文章

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

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 {a1, a2,.

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和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

ZOJ3868:GCD Expectation

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

ZOJ 3846 GCD Reduce//水啊水啊水啊水

GCD Reduce Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge You are given a sequence {A1, A2, ..., AN}. You task is to change all the element of the sequence to 1 with the following operations (you may need to apply it multiple ti