码迷,mamicode.com
首页 > 其他好文 > 详细

zoj[3868]gcd期望

时间:2015-04-14 16:11:15      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

题意:求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 }
View Code

 

zoj[3868]gcd期望

标签:

原文地址:http://www.cnblogs.com/jklongint/p/4424901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!