题意:求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数组,答案自然出来了。

 #pragma comment(linker, "/STACK:10240000,10240000")

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#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) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a);
#define sint(a) ReadInt(a)
#define sint2(a, b) ReadInt(a);ReadInt(b)
#define sint3(a, b, c) ReadInt(a);ReadInt(b);ReadInt(c)
#define pint(a) WriteInt(a)
#define if_else(a, b, c) if (a) { b; } else { c; }
#define if_than(a, b) if (a) { b; }
#define test_pint1(a) printf("var1 = %d\n", a)
#define test_pint2(a, b) printf("var1 = %d, var2 = %d\n", a, b)
#define test_pint3(a, b, c) printf("var1 = %d, var2 = %d, var3 = %d\n", a, b, c) typedef double db;
typedef long long LL;
typedef pair<int, int> pii;
typedef multiset<int> msi;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii; const int dx[] = {, , -, };
const int dy[] = {-, , , };
const int maxn = 1e6 + ;
const int maxm = 1e5 + ;
const int maxv = 1e7 + ;
const int max_val = 1e6 + ;
const int MD = ;
const int INF = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>void ReadInt(T &x){char c=getchar();while(!isdigit(c))c=getchar();x=;while(isdigit(c)){x=x*+c-'';c=getchar();}}
template<class T>void WriteInt(T i) {int p=;static int b[];if(i == ) b[p++] = ;else while(i){b[p++]=i%;i/=;}for(int j=p-;j>=;j--)pchr(''+b[j]);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } int pow_mod(int a, int b) {
static int buf[];
int p = ;
while (b) {
buf[p++] = b & ;
b >>= ;
}
LL ans = ;
rep_down0(i, p) {
ans = ans * ans % MD;
if (buf[i]) ans = ans * a % MD;
}
return ans;
} int cnt[maxn], c[maxn], f[maxn]; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int T;
cin >> T;
while (T--) {
mem0(cnt);
mem0(c);
int n, k;
cin >> n >> k;
int max_n = ;
rep_up0(i, n) {
int x;
sint(x);
cnt[x]++;
max_update(max_n, x);
}
rep_up1(i, max_n) {
for (int j = i; j <= max_n; j += i) {
c[i] += cnt[j];
}
}
rep_up1(i, max_n) f[i] = (pow_mod(, c[i]) + MD - ) % MD;
LL ans = ;
rep_down1(i, max_n) {
for (int j = * i; j <= max_n; j += i) {
f[i] = (f[i] - f[j] + MD) % MD;
}
ans = (ans + (LL)f[i] * (pow_mod(i, k))) % MD;
}
cout << ans << endl;
}
return ;
}

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

  1. zoj.3868.GCD Expectation(数学推导>>容斥原理)

    GCD Expectation Time Limit: 4 Seconds                                     Memory Limit: 262144 KB    ...

  2. Zoj 3868 GCD Expectation

    给一个集合,大小为n , 求所有子集的gcd 的期望和 . 期望的定义为 这个子集的最大公约数的K次方 : 每个元素被选中的概率是等可能的 即概率 p = (发生的事件数)/(总的事件数); 总的事件 ...

  3. ZOJ 3868 GCD Expectation (容斥+莫比乌斯反演)

    GCD Expectation Time Limit: 4 Seconds     Memory Limit: 262144 KB Edward has a set of n integers {a1 ...

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

    Description Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, ...

  5. ZOJ 3868 - Earthstone: Easy Version

    3868 - Earthstone: Easy Version Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld ...

  6. ZOJ 3822 Domination 期望dp

    Domination Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...

  7. ZOJ 3846 GCD Reduce//水啊水啊水啊水

    GCD Reduce Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge You are given a sequ ...

  8. One Person Game ZOJ - 3329(期望dp, 数学)

    There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. ...

  9. ZOJ.3551.Bloodsucker(期望DP)

    题目链接 \(Description\) 有1个吸血鬼和n-1个人,每天有且只会有两个人/吸血鬼相遇,如果是人与吸血鬼相遇,那个人会有p的概率变成吸血鬼:否则什么也不发生.求n个都变成吸血鬼的期望天数 ...

随机推荐

  1. Go gRPC进阶-go-grpc-middleware使用(八)

    前言 上篇介绍了gRPC中TLS认证和自定义方法认证,最后还简单介绍了gRPC拦截器的使用.gRPC自身只能设置一个拦截器,所有逻辑都写一起会比较乱.本篇简单介绍go-grpc-middleware的 ...

  2. 小小小小小flag

    2020:300道题 小小小小小flag 150红题 100道橙题 50道黄题 努力变强!加油 我的主页: 主页https://www.luogu.com.cn/user/306734 谢谢大家,目前 ...

  3. 2020新Asp.NET敏捷快速开发框架7.0.5旗舰版源码asp.net mvc框架,工具类CRM,工作流

    演示地址: http://frame3.diytassel.com  用户名:system  密码:0000    需要的联系QQ:22539134 一.新添加了 1.多语言功能: 2.代码生成器模版 ...

  4. DBeaver数据表的拷贝过程

    通过DBeaver工具链接数据库后,我们就可以通过菜单或者命令功能来实现我们想要的目的. (一)创建数据库 新建数据库,如下图所示:  ——> 在点击“确定”按钮后,可能会遇到失败,此时,我们可 ...

  5. Jenkins(1)- centos7.X下安装Jenkins

    如果想从头学起Jenkins的话,可以看看这一系列的文章哦 https://www.cnblogs.com/poloyy/category/1645399.html 下载安装包 wget http:/ ...

  6. 【转帖】Python 重复造轮子/造轮子找模子,你都应该熟读该文

    Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要用来给文本添加各种颜色,并且非常简单易用. Prettytable,主要用于在终端或浏览器端构建格式化的输 ...

  7. php最快捷的插入数据,3000万仅需5秒

    <?phpheader('content-type:text/html;charset=utf-8');//采集数据$url="http://www.keepclub.com/club ...

  8. StringRedisTemplate的常用操作

    stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向r ...

  9. 前端JS—显示赋值(一)

    一:js代码必须位于<script>js代码</script> 把js代码放到<body>元素的地步,可以改善显示速度 二:js显示数据 使用 window.ale ...

  10. python字符串 提取括号中的内容

    返回值是一个列表 re.findall(r'[(](.*?)[)]', str1)