zoj.3868.GCD Expectation(数学推导>>容斥原理)
GCD Expectation
Time Limit: 4 Seconds Memory Limit: 262144 KB
Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, x2,…,xm} (each nonempty subset has equal probability to be picked), and would like to know the expectation of [gcd(x1, x2,…,xm)]k.
Note that gcd(x1, x2,…,xm) is the greatest common divisor of {x1, x2,…,xm}.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers n, k (1 ≤ n, k ≤ 106). The second line contains n integers a1, a2,…,an (1 ≤ ai ≤ 106).
The sum of values max{ai} for all the test cases does not exceed 2000000.
Output
For each case, if the expectation is E, output a single integer denotes E · (2n - 1) modulo 998244353.
Sample Input
1
5 1
1 2 3 4 5
Sample Output
42
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include<map>
#include <set>
#include <vector>
#include <math.h>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 1000005
#define mod 998244353
const LL inf = <<;
LL t,n,k;
LL a[Len];
LL two[Len],fun[Len],cnt[Len],vis[Len],maxn;
LL power(LL x, LL y)
{
LL ans = ;
w(y)
{
if(y&) ans=(ans*x)%mod;
x=(x*x)%mod;
y/=;
}
return ans;
}
int main()
{
LL i,j;
scanf("%lld",&t);
two[] = ;
up(i,,Len-) two[i] = (two[i-]*)%mod;
w(t--)
{
mem(cnt,);
mem(vis,);
scanf("%lld%lld",&n,&k);
maxn = ;
up(i,,n-)
{
scanf("%lld",&a[i]);
if(!vis[a[i]])
{
vis[a[i]] = ;
cnt[a[i]] = ;
}
else cnt[a[i]]++;
maxn = max(maxn,a[i]);
}
fun[] = ;
up(i,,maxn) fun[i] = power(i,k);
up(i,,maxn)
{
for(j = i+i; j<=maxn; j+=i) fun[j]=(fun[j]-fun[i])%mod;
}
LL ans = (two[n]-)*fun[]%mod;
up(i,,maxn)
{
LL cc = ;
for(j = i; j<=maxn; j+=i)
{
if(vis[j]) cc+=cnt[j];
}
LL tem = (two[cc]-)*fun[i]%mod;
ans = (ans+tem)%mod;
}
printf("%lld\n",(ans+mod)%mod);
}
return ;
}
对于N的序列,肯定有2^N-1个非空子集,其中其最大的GCD不会大于原序列的max,那么我们用数组fun来记录其期望
例如题目中的,期望为1的有26个,期望为2的有2个,期望为3,4,5的都只有1个
我们可以拆分来算,首先对于1,期望为1,1的倍数有5个,那么这五个的全部非空子集为2^5-1种,得到S=(2^5-1)*1;
对于2,2的期望应该是2,但是在期望为1的时候所有的子集中,我们重复计算了2的期望,多以我们应该减去重复计算的期望数,
现在2的期望应该作1算,那么对于2的倍数,有两个,2,4,其组成的非空子集有2^2-1个,所以得到S+=(2^2-1)*1
对于3,4,5同理;
zoj.3868.GCD Expectation(数学推导>>容斥原理)的更多相关文章
- 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 Expectation (容斥+莫比乌斯反演)
GCD Expectation Time Limit: 4 Seconds Memory Limit: 262144 KB Edward has a set of n integers {a1 ...
- Zoj 3868 GCD Expectation
给一个集合,大小为n , 求所有子集的gcd 的期望和 . 期望的定义为 这个子集的最大公约数的K次方 : 每个元素被选中的概率是等可能的 即概率 p = (发生的事件数)/(总的事件数); 总的事件 ...
- zoj[3868]gcd期望
题意:求n个数组成的集合的所有非空子集的gcd的期望 大致思路:对于一个数x,设以x为约数的数的个数为cnt[x],所组成的非空集合个数有2^cnt[x]-1个,这其中有一些集合的gcd是x的倍数的, ...
- ZOJ 3702 Gibonacci number(数学推导)
公式推导题,G(0) = 1,G(1) = t,给出一个 i 和 G(i),要求求出G(j)的值: G(0) = 0*t + 1 G(1) = 1*t + 0; 观察t的系数和常数值可以知道二者都遵循 ...
- 借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5
上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.p ...
- 关于不同进制数之间转换的数学推导【Written By KillerLegend】
关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...
- UVA - 10014 - Simple calculations (经典的数学推导题!!)
UVA - 10014 Simple calculations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- 『sumdiv 数学推导 分治』
sumdiv(POJ 1845) Description 给定两个自然数A和B,S为A^B的所有正整数约数和,编程输出S mod 9901的结果. Input Format 只有一行,两个用空格隔开的 ...
随机推荐
- GDB调试汇编栈堆过程的学习
前期调试 我的程序代码是: 首先,用gcc g gdb.c -o gdb -m32产生32位汇编. 输入gdb gdb进入gdb调试器 在main函数处设置一个断点:b main 用disassemb ...
- MVC5 + EF6 + Bootstrap3 (14) 分部视图PartialView
Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-partialview.html 系列教程:MVC5 + E ...
- 7.HBase In Action 第一章-HBase简介(1.2.1 典型的网络搜索问题:Bigtable的起原)
Search is the act of locating information you care about: for example, searching for pages in a text ...
- java heep space错误解决办法
1.双击tomcat 2.Open launch configuration 3.Argument 4. VM arguments中添加:-Xmx1024M -Xms512M -XX:MaxPermS ...
- 在Ubuntu-14.04.3配置并成功编译Android6_r1源码
折腾了一周,终于把Android6_r1的源码编译成功.先上图,这是在ubuntu中运行的Android模拟器: 由于我是在win8中安装虚拟机VMware,然后在虚拟机中安装Ubuntu进行编译,所 ...
- JavaScript基础2---控制权DOM操作
文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM将HTML文档呈现为带有元素,属性和文本的树结构(节点树). HTML文档可以说由节点构成的 ...
- gradle构建android项目
工具: Android Studio2.0 gradle-2.10 一.Android常识 在做Android开发的时候我们首先必须要有一个SDK.一般SDK的主要作用就是将硬件和软件进行分离,做软件 ...
- WCF 入门 (21)
前言 再不写一篇就太监了,哈哈. 第21集 WCF里面的Binding Bindings in WCF 其实不太了解为什么第21集才讲这个Binding,下面都是一些概念性的东西,不过作为一个入门视频 ...
- Spring 事务配置管理,简单易懂,详细 [声明式]
Spring 事务配置说明 Spring 如果没有特殊说明,一般指是跟数据存储有关的数据操作事务操作:对于数据持久操作的事务配置,一般有三个对象,数据源,事务管理器,以及事务代理机制: Spring ...
- 【C#】依赖于SharpZipLib的Zip压缩工具类
上班第二天下班,课外作业,实现一个ZIP压缩的工具类.本来想用Package,但是写完了才发现不能解压其他工具压缩的zip包,比较麻烦,因此本工具类依赖了第三方的库(SharpZipLib vers ...