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<=maxn,而f[2x]、f[3x]、...、f[px]都是正确的,所以f[x]也是正确的。所以可以得到正确的f数组,有了f数组,答案自然出来了。

  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 #include <bitset>
 17 #include <functional>
 18 #include <numeric>
 19 #include <stdexcept>
 20 #include <utility>
 21
 22 using namespace std;
 23
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define lson l, m, rt << 1
 26 #define rson m + 1, r, rt << 1 | 1
 27 #define define_m int m = (l + r) >> 1
 28 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 29 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 30 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 31 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 32 #define all(a) (a).begin(), (a).end()
 33 #define lowbit(x) ((x) & (-(x)))
 34 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 35 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 36 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 37 #define pchr(a) putchar(a)
 38 #define pstr(a) printf("%s", a)
 39 #define sstr(a) scanf("%s", a);
 40 #define sint(a) ReadInt(a)
 41 #define sint2(a, b) ReadInt(a);ReadInt(b)
 42 #define sint3(a, b, c) ReadInt(a);ReadInt(b);ReadInt(c)
 43 #define pint(a) WriteInt(a)
 44 #define if_else(a, b, c) if (a) { b; } else { c; }
 45 #define if_than(a, b) if (a) { b; }
 46 #define test_pint1(a) printf("var1 = %d\n", a)
 47 #define test_pint2(a, b) printf("var1 = %d, var2 = %d\n", a, b)
 48 #define test_pint3(a, b, c) printf("var1 = %d, var2 = %d, var3 = %d\n", a, b, c)
 49
 50 typedef double db;
 51 typedef long long LL;
 52 typedef pair<int, int> pii;
 53 typedef multiset<int> msi;
 54 typedef set<int> si;
 55 typedef vector<int> vi;
 56 typedef map<int, int> mii;
 57
 58 const int dx[8] = {0, 0, -1, 1};
 59 const int dy[8] = {-1, 1, 0, 0};
 60 const int maxn = 1e6 + 7;
 61 const int maxm = 1e5 + 7;
 62 const int maxv = 1e7 + 7;
 63 const int max_val = 1e6 + 7;
 64 const int MD = 998244353;
 65 const int INF = 1e9 + 7;
 66 const double pi = acos(-1.0);
 67 const double eps = 1e-10;
 68
 69 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 70 template<class T>void ReadInt(T &x){char c=getchar();while(!isdigit(c))c=getchar();x=0;while(isdigit(c)){x=x*10+c-‘0‘;c=getchar();}}
 71 template<class T>void WriteInt(T i) {int p=0;static int b[20];if(i == 0) b[p++] = 0;else while(i){b[p++]=i%10;i/=10;}for(int j=p-1;j>=0;j--)pchr(‘0‘+b[j]);}
 72 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 73 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 74 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 75 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 76 int make_id(int x, int y, int n) { return x * n + y; }
 77
 78 int pow_mod(int a, int b) {
 79     static int buf[100];
 80     int p = 0;
 81     while (b) {
 82         buf[p++] = b & 1;
 83         b >>= 1;
 84     }
 85     LL ans = 1;
 86     rep_down0(i, p) {
 87         ans = ans * ans % MD;
 88         if (buf[i]) ans = ans * a % MD;
 89     }
 90     return ans;
 91 }
 92
 93 int cnt[maxn], c[maxn], f[maxn];
 94
 95 int main() {
 96     //freopen("in.txt", "r", stdin);
 97     //freopen("out.txt", "w", stdout);
 98     int T;
 99     cin >> T;
100     while (T--) {
101         mem0(cnt);
102         mem0(c);
103         int n, k;
104         cin >> n >> k;
105         int max_n = 0;
106         rep_up0(i, n) {
107             int x;
108             sint(x);
109             cnt[x]++;
110             max_update(max_n, x);
111         }
112         rep_up1(i, max_n) {
113             for (int j = i; j <= max_n; j += i) {
114                 c[i] += cnt[j];
115             }
116         }
117         rep_up1(i, max_n) f[i] = (pow_mod(2, c[i]) + MD - 1) % MD;
118         LL ans = 0;
119         rep_down1(i, max_n) {
120             for (int j = 2 * i; j <= max_n; j += i) {
121                 f[i] = (f[i] - f[j] + MD) % MD;
122             }
123             ans = (ans + (LL)f[i] * (pow_mod(i, k))) % MD;
124         }
125         cout << ans << endl;
126     }
127     return 0;
128 }

时间: 2024-08-07 01:56:20

zoj[3868]gcd期望的相关文章

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

给一个集合,大小为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的倍数. 因此

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

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

ZOJ 4846 GCD Reduce (数学分析题)

题目链接 : http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5440 题意: 给定一个长度为n的数列每次可以选个二元组(a[i],a[j])换成他们的最大公约数 然后问能不能再5*n次操作内把他们全部换成1, 分析: 因为每次操作都是换成GCD 因此n数的GCD肯定为1,如果不为1的话肯定不能变成1的 然后随便构造一种方法就行了,从1到n搞两遍 一定可以全变成1: 代码如下: #include <iostream> #in

模拟3

    ID Origin Title 6 / 12 Problem A ZOJ 3860 Find the Spy 6 / 27 Problem B ZOJ 3861 Valid Pattern Lock     Problem C ZOJ 3862 Intersection   0 / 1 Problem D ZOJ 3863 Paths on the Tree     Problem E ZOJ 3864 Quiz for EXO-L     Problem F ZOJ 3865 Supe