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 只有一行,两个用空格隔开的 ...
随机推荐
- Opencv step by step - 图像变换
这里举出三个案例: #include <cv.h> #include <highgui.h> void image_smooth(IplImage * image) { cvN ...
- abstract virtaul override new 及多态
abstract abstract 修饰符可以和类.方法.属性.索引器及事件一起使用.在类声明中使用 abstract 修饰符以指示某个类只能是其他类的基类.标记为抽象或包含在抽象类中的成员必须通过从 ...
- 傻傻分不清楚的php脚本路径
闲话就不说了,还是直接提出今天的问题,准确的说,对多个相似的 有关当前脚本信息的全局变量常量的区分. 先贴上代码: <?php echo $_SERVER['PHP_SELF']; echo ...
- JS闭包文章--(翻译)Callbacks in Loops
原文地址:http://tobyho.com/2011/11/02/callbacks-in-loops/ 某些时候,你需要在循环里创建一个回调函数.我们来试试给页面里每个链接增加点击事件. var ...
- 第二十二课:js事件原理以及addEvent.js的详解
再看这篇博客之前,希望你已经对js高级程序编程一书中的事件模块进行了详读,不然我只能呵呵了. document.createEventObject,在IE下创建事件对象event. elem.fire ...
- 一头扎进EasyUI2
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 一头扎进EasyUI第6讲 .日历组件 <div class="easyui-calendar&quo ...
- js获取url查询参数
function getURLParamInfo(key) { if (location.search == "") { return undefined; } var a = l ...
- 使用Git进行代码管理心得
关于使用Git for Windows来clone和上传项目 首先到Git for Windows的官网下载并安装 在本地用来保存clone文件的文件夹右键,选择Git Bash Here: 在打开的 ...
- FZU5BOYS-Beta版本冲刺计划及安排
1.下一阶段需要改进完善的功能 话题模块(分类参考Citeulike论文网站),文章/计划的删除功能 2.下一阶段新增的功能 1)推荐模块(冷启动问题,拟爬取部分豆瓣数据,部分伪专家数据(我们团队), ...
- keep_on _coding——js_good_parts
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...