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 ;
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
#define MAXN 1000010
#define INF 0x3f3f3f3f
#define MOD 998244353
#define eps 1e-6
#define LL long long
int num[MAXN];
LL dp[MAXN];
//dp[i] = 2^x -1 ; gcd = n*i;
//for(int j = i ; j <= max_num ; j += i) dp[i] -= dp[j];
LL qpow(LL x , LL k)
{
LL res=;
while(k)
{
if(k & ) res = res * x % MOD;
x = x * x % MOD;
k >>= ;
}
return res;
} int main()
{
int T;
int n,k;
LL ans;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&k);
int x;
int max_num = ;
int cunt = ;
memset(num , , sizeof(num));
memset(dp , , sizeof(dp));
for(int i = ; i < n ; i ++)
{
scanf("%d",&x);
num[x] ++;
max_num = max(x , max_num);
} ans = ;
for(int i = max_num ; i >= ; i --)
{
cunt = ;
dp[i] = ;
for(int j = i ; j <= max_num ; j += i)
{
cunt += num[j];
if(j > i) dp[i] = (dp[i] - dp[j] + MOD) % MOD;
}
dp[i] = (dp[i] + qpow( , cunt) - + MOD) % MOD;
ans = (ans + (dp[i] * qpow(i , k)) % MOD ) % MOD;
}
printf("%d\n",(int)ans);
}
return ;
}
Zoj 3868 GCD Expectation的更多相关文章
- zoj.3868.GCD Expectation(数学推导>>容斥原理)
GCD Expectation Time Limit: 4 Seconds Memory Limit: 262144 KB ...
- 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]gcd期望
题意:求n个数组成的集合的所有非空子集的gcd的期望 大致思路:对于一个数x,设以x为约数的数的个数为cnt[x],所组成的非空集合个数有2^cnt[x]-1个,这其中有一些集合的gcd是x的倍数的, ...
- ZOJ 3868 - Earthstone: Easy Version
3868 - Earthstone: Easy Version Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld ...
- ZOJ 3846 GCD Reduce//水啊水啊水啊水
GCD Reduce Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge You are given a sequ ...
- 数学+高精度 ZOJ 2313 Chinese Girls' Amusement
题目传送门 /* 杭电一题(ACM_steps 2.2.4)的升级版,使用到高精度: 这次不是简单的猜出来的了,求的是GCD (n, k) == 1 最大的k(1, n/2): 1. 若n是奇数,则k ...
- Help Me Escape (ZOJ 3640)
J - Help Me Escape Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB ...
- ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)
ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...
随机推荐
- HighCharts基本使用实例(入门)
HighCharts 摘要 HighCharts是眼下最为流行的图表插件,应用范围广泛,眼下支持曲线图.区域图.3D图.柱状图.饼图.散列图.混合图等,而且还支持一些拓展的特殊图表,如:仪表图.极地图 ...
- malloc()与calloc差别
Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slig ...
- [D3] 11. Basic D3 chart interactivity on(), select(this), classed(class, trueorfalse)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- android85 短信防火墙
系统收到短信是有广播的,广播中包含了短信的号码和内容 ###短信防火墙 * 系统发送短信广播时,是怎么把短信内容存入广播的,我们就只能怎么取出来 * 如果短信过长,那么发送时会拆分成多条短信发送,那么 ...
- hadoop错误org.apache.hadoop.yarn.exceptions.YarnException Unauthorized request to start container
错误: 14/04/29 02:45:07 INFO mapreduce.Job: Job job_1398704073313_0021 failed with state FAILED due to ...
- Spring XD 1.1 M2 and 1.0.3 released---support kafka
官方地址:http://spring.io/blog/2014/12/23/spring-xd-1-1-m2-and-1-0-3-released On behalf of the Spring XD ...
- Mybatis高级应用
Mybatis是一个半自动的框架.相对于hibernate全自动模式,mybatis为开发人员提供了更加灵活的对sql语句操作的控制能力,有利于dba对相关的sql操作进行优化,同时也方便开发者构建复 ...
- samba环境搭建
1.安装samba软件 sudo apt-get install samba cifs-utils samba-common 2.创建与windows共享目录 mkdir share chmod 77 ...
- Python实战:Python爬虫学习教程,获取电影排行榜
Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...
- SharePoint Dialog 使用
SharePoint中弹出模态窗口对体验提高太大了 方法为: 父页面中调用子页面: function showDialog() { var options = { ...