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 只有一行,两个用空格隔开的 ...
随机推荐
- 记录毕业论文 LanguageTool 二次开发时用到的网站
LanguageTool Development LanguageTool Supported Languages Share your knowledge about LT - LanguageTo ...
- blur效果,模糊效果css
.blur { -webkit-filter: blur(5px); -moz-filter: blur(5px); -o-filter: blur(5px); -ms-filter: b ...
- ios9+xcode7 适配笔记
升级了xcode7,最近ios9上马,又到了草泥马的时间,apple开放团队每次系统更新,都是无数个草泥马的适配夜晚,现在ios9上线以前的app竟然启动crash,这是要闹哪样. 1.微信和微博的s ...
- 自动备份SQL数据库到云存储Storage
如何自动备份SQL数据库到Storage呢. 前提条件需要SQL Server2012 SP1 CU2或更高版本 1. 备份SQL Azure数据库到云存储Storage 1)在SQL Server ...
- Grunt-cli的执行过程以及Grunt加载原理
通过本篇你可以了解到: 1 grunt-cli的执行原理 2 nodeJS中模块的加载过程 Grunt-cli原理 grunt-cli其实也是Node模块,它可以帮助我们在控制台中直接运行grunt命 ...
- java文件中文在MyEclipse中打开变成了乱码
导入其他人的项目,出现乱码状况 以下是解决方案之一:
- G-nav-02
/*header: Navigation public style*/header:before, header:after ,.navigation:before, .navigation:afte ...
- php empty()和isset()的区别
在使用 php 编写页面程序时,我经常使用变量处理函数判断 php 页面尾部参数的某个变量值是否为空,开始的时候我习惯了使用 empty() 函数,却发现了一些问题,因此改用 isset() 函数,问 ...
- hdu2594 KMP
2个字符长合并在一起即可.要注意next[n]的值要小于初始的两个字符串的长度; //next[]存的是之前相同的长度. //也是位置,只是s[i]不一定和s[next[i]]相同 //但是i之前的和 ...
- hdu1542矩阵的并 线段树+扫描线
求矩阵的并,也就是要求所有的面积.那可以吧总的图形按照矩阵来切割.使其为一块一块. 输入的时候用坐标表示,这里扫描线从下到上扫描.初始时让下面的边为1,上面的为-1: 用一条先从下面开始想上扫描.遇到 ...