给一个集合,大小为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的更多相关文章

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

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

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

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

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

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

  4. zoj[3868]gcd期望

    题意:求n个数组成的集合的所有非空子集的gcd的期望 大致思路:对于一个数x,设以x为约数的数的个数为cnt[x],所组成的非空集合个数有2^cnt[x]-1个,这其中有一些集合的gcd是x的倍数的, ...

  5. ZOJ 3868 - Earthstone: Easy Version

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

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

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

  7. 数学+高精度 ZOJ 2313 Chinese Girls' Amusement

    题目传送门 /* 杭电一题(ACM_steps 2.2.4)的升级版,使用到高精度: 这次不是简单的猜出来的了,求的是GCD (n, k) == 1 最大的k(1, n/2): 1. 若n是奇数,则k ...

  8. Help Me Escape (ZOJ 3640)

    J - Help Me Escape Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB ...

  9. ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)

    ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...

随机推荐

  1. c# 扩展方法奇思妙用集锦

    本文转载:http://www.cnblogs.com/ldp615/archive/2009/08/07/1541404.html 其中本人觉得很经典的:c# 扩展方法奇思妙用基础篇五:Dictio ...

  2. 数据库:JDBC编程

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于运行SQL语句的Java API.能够为多种关系数据库提供统一訪问.它由一组用Java语言编写的类和接口 ...

  3. 【剑指Offer学习】【面试题40:数组中仅仅出现一次的数字】

    题目:一个整型数组里除了两个数字之外.其它的数字都出现了两次,请敲代码找出这两个仅仅出现一次的数字. 要求时间复杂度是O(n),空间复杂度是O(1). 举例说明 比如输入数组{2, 4, 3, 6, ...

  4. [Angular 2] Component relative paths

    Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component ...

  5. 【JavaScript设计模式系列---开篇预览】

    转:http://www.cnblogs.com/Darren_code/archive/2011/08/31/JavascripDesignPatterns.html 2011-08-31 23:5 ...

  6. Android实战技巧之六:PreferenceActivity使用详解

    一.写作前面 当我们做应用的时候,需要用户配置一些信息,而这就是通常所说的应用设置. 对于Android系统来说,系统本身的设置带来的用户体验和习惯已经深入人心,在我们的应用中同样用到类似的设置页, ...

  7. hdu2034java

    人见人爱A-B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. Java基础知识强化之集合框架笔记60:Map集合之TreeMap(TreeMap<Student,String>)的案例

    1. TreeMap(TreeMap<Student,String>)的案例 2. 案例代码: (1)Student.java: package cn.itcast_04; public ...

  9. SQL排序 空值的后面

    按sort排序,sort为空的在后面 end),sort

  10. Js判断对象是否为空,Js判断字符串是否为空

    Js判断对象是否为空,Js判断字符串是否为空,JS检查字符串是否为空字符串 >>>>>>>>>>>>>>>&g ...