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 integerT 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 containsn 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 denotesE · (2n - 1) modulo 998244353.

Sample Input

1
5 1
1 2 3 4 5

Sample Output

42

Author: LIN, Xi

Source: The 15th Zhejiang University Programming Contest

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?

problemId=5480

题目大意:给一个集合。{xi}为它的一个非空子集。设E为[gcd(x1,x2,…,xm)]k  
的期望,求E*(2^n - 1) mod 998244353

题目分析:首先一个有n个元素的集合的非空子集个数为2^n - 1,所以E的分母就是2^n - 1了。因此我们要求的仅仅是E的分子,

设F(x)为gcd(xi) = x的个数,那么ans = (1^k) * F(1) + (2^k) * F(2) + ... + (ma^k) * F(ma)

以下的问题就是怎样高速的计算F(x)了。对于一个集合,先计算出x的倍数的个数,nlogn就可以。然后就是基础的容斥。如果如今要求gcd为1的,那就减去gcd为2的,gcd为3的,注意到6同一时候是2和3的倍数,也就是6的倍数被减了两次,所以要加上gcd为6的,前面的系数刚好是数字相应的莫比乌斯函数,看到这题非常多用dp来容斥的,事实上本质和莫比乌斯函数一样,可是莫比乌斯函数写起来真的非常easy。2333333

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MOD = 998244353;
int const MAX = 1e6 + 5;
ll two[MAX];
int p[MAX], mob[MAX], num[MAX], cnt[MAX];
bool noprime[MAX];
int n, k, ma, pnum; void Mobius()
{
pnum = 0;
mob[1] = 1;
for(int i = 2; i < MAX; i++)
{
if(!noprime[i])
{
p[pnum ++] = i;
mob[i] = -1;
}
for(int j = 0; j < pnum && i * p[j] < MAX; j++)
{
noprime[i * p[j]] = true;
if(i % p[j] == 0)
{
mob[i * p[j]] = 0;
break;
}
mob[i * p[j]] = -mob[i];
}
}
} ll qpow(ll x, ll n)
{
ll res = 1;
while(n != 0)
{
if(n & 1)
res = (res * x) % MOD;
x = (x * x) % MOD;
n >>= 1;
}
return res;
} void pre()
{
Mobius();
two[0] = 1;
for(int i = 1; i < MAX; i++)
two[i] = two[i - 1] * 2ll % MOD;
} int main()
{
pre();
int T;
scanf("%d", &T);
while(T --)
{
memset(num, 0, sizeof(num));
memset(cnt, 0, sizeof(cnt));
ma = 0;
int tmp;
scanf("%d %d", &n, &k);
for(int i = 0; i < n; i++)
{
scanf("%d", &tmp);
cnt[tmp] ++;
ma = max(ma, tmp);
}
for(int i = 1; i <= ma; i++)
for(int j = i; j <= ma; j += i)
num[i] += cnt[j]; //求i的倍数的个数
ll ans = 0;
for(int i = 1; i <= ma; i++) //枚举gcd
{
ll sum = 0;
for(int j = i; j <= ma; j += i) //容斥
sum = (MOD + sum % MOD + mob[j / i] * (two[num[j]] - 1) % MOD) % MOD;
ans = (MOD + ans % MOD + (sum * qpow(i, k)) % MOD) % MOD;
}
printf("%lld\n", ans);
}
}

ZOJ 3868 GCD Expectation (容斥+莫比乌斯反演)的更多相关文章

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

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

  2. 51nod 1355 - 斐波那契的最小公倍数(Min-Max 容斥+莫比乌斯反演)

    vjudge 题面传送门 首先我们知道斐波那契数列的 lcm 是不太容易计算的,但是它们的 gcd 非常容易计算--\(\gcd(f_x,f_y)=f_{\gcd(x,y)}\),该性质已在我的这篇博 ...

  3. cf900D. Unusual Sequences(容斥 莫比乌斯反演)

    题意 题目链接 Sol 首先若y % x不为0则答案为0 否则,问题可以转化为,有多少个数列满足和为y/x,且整个序列的gcd=1 考虑容斥,设\(g[i]\)表示满足和为\(i\)的序列的方案数,显 ...

  4. 【二分+容斥+莫比乌斯反演】BZOJ2440 完全平方数

    Description 求第k个没有完全平方因子的数,k<=1e9. Solution 这其实就是要求第k个µ[i](莫比乌斯函数)不为0的数. 然而k太大数组开不下来是吧,于是这么处理. 二分 ...

  5. bzoj 2005 & 洛谷 P1447 [ Noi 2010 ] 能量采集 —— 容斥 / 莫比乌斯反演

    题目:bzoj 2005 https://www.lydsy.com/JudgeOnline/problem.php?id=2005   洛谷 P1447 https://www.luogu.org/ ...

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

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

  7. Codeforces.547C.Mike and Foam(容斥/莫比乌斯反演)

    题目链接 \(Description\) 给定n个数(\(1\leq a_i\leq 5*10^5\)),每次从这n个数中选一个,如果当前集合中没有就加入集合,有就从集合中删去.每次操作后输出集合中互 ...

  8. HDU 5942 Just a Math Problem 容斥 莫比乌斯反演

    题意:\( g(k) = 2^{f(k)} \) ,求\( \sum_{i = 1}^{n} g(i) \),其中\( f(k)\)代表k的素因子个数. 思路:题目意思很简单,但是着重于推导和简化,这 ...

  9. Zoj 3868 GCD Expectation

    给一个集合,大小为n , 求所有子集的gcd 的期望和 . 期望的定义为 这个子集的最大公约数的K次方 : 每个元素被选中的概率是等可能的 即概率 p = (发生的事件数)/(总的事件数); 总的事件 ...

随机推荐

  1. 398 Random Pick Index 随机数索引

    给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...

  2. Hbase源码分析:RPC概况

    RPC是hbase中Master,RegionServer和Client三者之间通信交流的纽带.了解hbase的rpc机制能够为通过源码学习hbase奠定良好的基础.因为了解了hbase的rpc机制能 ...

  3. [ ZJOI 2010 ] 网络扩容

    \(\\\) Description 给定一张有向图,每条边都有一个容量 \(C\) 和一个扩容费用 \(W\). 这里扩容费用是指将容量扩大 \(1\) 所需的费用.求: 在不扩容的情况下, \(1 ...

  4. leetcode692 Top K Frequent Words

    思路: 堆.实现: #include <bits/stdc++.h> using namespace std; class Solution { public: inline bool c ...

  5. 介绍Git的17条基本用法

    本文将介绍Git的17条基本用法.本文选自<Python全栈开发实践入门>. 1.初始化Git仓库 Git仓库分为两种类型:一种是存放在服务器上面的裸仓库,里面没有保存文件,只是存放.gi ...

  6. ImmutableJS

    引用大神的一句话:(具体是谁自己问度娘) Shared mutable state is the root of all evil(共享的可变状态是万恶之源) -- Pete Hunt   JavaS ...

  7. HTML5——移动端的点击、拖拽

    移动端浏览器不支持mouse事件 https://www.cnblogs.com/joyco773/p/6519668.html https://www.cnblogs.com/yjhua/p/525 ...

  8. canvas一周一练 -- canvas绘制中国银行标志(4)

    运行效果: <!DOCTYPE html> <html> <head> </head> <body> <canvas id=" ...

  9. The Runtime Interaction Model for Views-UI布局事件处理流程

    The Runtime Interaction Model for Views Any time a user interacts with your user interface, or any t ...

  10. logging,numpy,pandas,matplotlib模块

    logging模块 日志总共分为以下五个级别,这五个级别自下而上进行匹配debug->info->warning->error->critical,默认的最低级别warning ...