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数组,答案自然出来了。
#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期望的更多相关文章
- zoj.3868.GCD Expectation(数学推导>>容斥原理)
		GCD Expectation Time Limit: 4 Seconds Memory Limit: 262144 KB ... 
- Zoj 3868 GCD Expectation
		给一个集合,大小为n , 求所有子集的gcd 的期望和 . 期望的定义为 这个子集的最大公约数的K次方 : 每个元素被选中的概率是等可能的 即概率 p = (发生的事件数)/(总的事件数); 总的事件 ... 
- ZOJ 3868 GCD Expectation (容斥+莫比乌斯反演)
		GCD Expectation Time Limit: 4 Seconds Memory Limit: 262144 KB Edward has a set of n integers {a1 ... 
- ACM学习历程—ZOJ 3868 GCD Expectation(莫比乌斯 || 容斥原理)
		Description Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, ... 
- ZOJ 3868 - Earthstone: Easy Version
		3868 - Earthstone: Easy Version Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld ... 
- ZOJ 3822 Domination 期望dp
		Domination Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ... 
- ZOJ 3846 GCD Reduce//水啊水啊水啊水
		GCD Reduce Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge You are given a sequ ... 
- 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. ... 
- ZOJ.3551.Bloodsucker(期望DP)
		题目链接 \(Description\) 有1个吸血鬼和n-1个人,每天有且只会有两个人/吸血鬼相遇,如果是人与吸血鬼相遇,那个人会有p的概率变成吸血鬼:否则什么也不发生.求n个都变成吸血鬼的期望天数 ... 
随机推荐
- 14. 最长公共前缀----LeetCode
			编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ... 
- JavaScript_Array
			Array 概念特点 值的有序集合: 每一个值叫一个元素: 每个元素在数组中有一个位置,以数字表示,称为索引(下标): 元素可以是任何类型 索引从0开始,最大为2的32次方 数组的创建 数组直接量 v ... 
- 硬盘性能测试工具之bonnie++
			bonnie++ 官方站点 先写内存的两倍,内存较大时比较耗时.适合简单的测试场景. # bonnie++ -u root 写测试 读测试 Version 1.97 ------Sequential ... 
- Exercise
			""" 问:执行完下面的代码后, l,m的内容分别是什么? """ def func(m): for k,v in m.items(): m ... 
- liunx常用知识基本命令大全
			liunx基础命令使用 标签(空格分隔):liunx常用命令 网络配置 虚拟网卡的绝对路径 /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ... 
- 5. 配置项:rule_files
			prometheus配置文件内容: global: # 默认情况下抓取目标的频率. [ scrape_interval: <duration> | default = 1m ] # 抓取超 ... 
- all_user_func()详解
			来源:https://blog.csdn.net/moliyiran/article/details/83514495 call_user_func — 把第一个参数作为回调函数调用 通过函数的方式回 ... 
- zabbix 微信告警机制
			微信告警首先得注册一个企业微信,然后才能实现微信告警.自行百度 微信: 添加一个用户到上面创建的部门里面 创建完成记住 AgentID 和 Secret 下一步:记住企业 ID 1)编辑zabbix ... 
- Cannot find libcrypto in Ubuntu
			https://stackoverflow.com/questions/13811889/cannot-find-libcrypto-in-ubuntu sudo apt-get install li ... 
- 接近8000字的Spring/SpringBoot常用注解总结!安排!
			0.前言 大家好,我是 Guide 哥!这是我的 221 篇优质原创文章.如需转载,请在文首注明地址,蟹蟹! 本文已经收录进我的 75K Star 的 Java 开源项目 JavaGuide:http ... 
