Description

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

15 11 2 3 4 5

Sample Output

42

这个题目和之前做的莫比乌斯很像。

首先d | gcd的情况的方案数比较好做。

这个很容易想到是2^num[d]-1。其中num[d]表示数列里面d的倍数的个数。

然后就是无脑莫比乌斯了,

不过第一次我把快速幂放在了第二层for循环里面T了一发,导致重复计算了quickPow(d, k)。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
#define LL long long
#define MOD 998244353 using namespace std; const int maxN = 1e6+;
int n, k, len;
int pow2[maxN], num[maxN];
int prime[maxN], u[maxN];
bool vis[maxN]; //莫比乌斯反演
//F(n)和f(n)是定义在非负整数集合上的两个函数
//并且满足条件F(n) = sum(f(d)) (d|n)
//那么f(n) = sum(u(d)*F(n/d)) (d|n)
//case 1: if d = 1, u(d) = 1
//case 2: if d = p1*p2...pk, (pi均为互异素数), u(d) = (-1)^k
//case 3: else, u(d) = 0;
//性质1: sum(u(d)) (d|n) = 1 (n == 1) or 0 (n > 1)
//性质2: sum(u(d)/d) (d|n) = phi(n)/n
//另一种形式:F(d) = sum(f(n)) (d|n) => f(d) = sum(u(n/d)*F(n)) (d|n)
//线性筛选求莫比乌斯反演函数代码
void mobius()
{
memset(vis, false,sizeof(vis));
u[] = ;
int cnt = ;
for(int i = ; i < maxN; i++)
{
if(!vis[i])
{
prime[cnt++] = i;
u[i] = -;
}
for(int j = ; j < cnt && i*prime[j] < maxN; j++)
{
vis[i*prime[j]] = true;
if(i%prime[j])
u[i*prime[j]] = -u[i];
else
{
u[i*prime[j]] = ;
break;
}
}
}
} void init()
{
mobius();
pow2[] = ;
for (int i = ; i < maxN; ++i)
pow2[i] = *pow2[i-]%MOD;
} void input()
{
int tmp;
len = ;
scanf("%d%d", &n, &k);
memset(num, , sizeof(num));
for (int i = ; i < n; ++i)
{
scanf("%d", &tmp);
len = max(len, tmp);
num[tmp]++;
}
for (int i = ; i <= len; ++i)
{
for (int j = i*; j <= len; j += i)
num[i] += num[j];
}
} //快速幂m^n
LL quickPow(LL x, int n)
{
if (n == )
return ;
if (x == )
return ;
LL a = ;
while (n)
{
a *= n& ? x : ;
a %= MOD;
n >>= ;
x *= x;
x %= MOD;
}
return a;
} void work()
{
int ans = , val;
for (int i = ; i <= len; ++i)
{
val = quickPow(i, k);
for (int j = i; j <= len; j += i)
{
ans += ((LL)u[j/i]*val%MOD)*(pow2[num[j]]-)%MOD;
ans = (ans%MOD+MOD)%MOD;
}
}
printf("%d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
init();
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
input();
work();
}
return ;
}

ACM学习历程—ZOJ 3868 GCD Expectation(莫比乌斯 || 容斥原理)的更多相关文章

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

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

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

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

  3. ACM学习历程—HYSBZ 2818 Gcd(欧拉函数 || 莫比乌斯反演)

    Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...

  4. Zoj 3868 GCD Expectation

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

  5. ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)

    Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal ...

  6. ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)

    Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...

  7. ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)

    Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...

  8. ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

    Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...

  9. ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)

    Description Pattern lock security is generally used in Android handsets instead of a password. The p ...

随机推荐

  1. 如何基于EasyDSS体系的全套SDK完成各种场景下的视频应用需求

    需求背景 回顾EasyDSS的发展过程,基本上保持的是先局部后系统.先组件后平台的发展方式,一步一步夯实每一个细节功能点,从最基础.最兼容的音视频数据的拉流获取,到高效的.全兼容的数据推流,再到流媒体 ...

  2. Java语言实现简单FTP软件------>连接管理模块的实现:主机与服务器之间的连接与关闭操作(八)

    (1)FTP连接 运行FTP客户端后,首先是连接FTP服务器,需要输入FTP服务器的IP地址及用户名.密码以及端口号后点击连接按钮开始连接FTP服务器,连接流程图如下图所示. 点击"连接&q ...

  3. Cordova-安装Cordova过程详细解

    官方网站Apache Cordova 前提是你电脑上 1:全局安装了Node 2:全局安装了npm 3:安装了java,并配置好环境 4:下载安装好android-sdk,并配好环境,注意安卓虚拟机可 ...

  4. ABAP内表数据做层次XML输出

      *&---------------------------------------------------------------------**& Report  Z_BARRY ...

  5. SAP basis 二

    使用事务 SMW0 可以在数据库中创建自己的图像.选择选项"二进制数据". 可以按.GIF 格式保存图像. 使用表 SSM_CUST 中的关键字 "START_IMAGE ...

  6. Linux shell join命令详解

    Linux join命令 2012-02-09 17:49:00| 分类: SHELL | 标签:linux join 文件连接 |字号 订阅join命令 功能:“将两个文件里指定栏位同样的行连接起来 ...

  7. pinpoint agent线程模型

    pinpoint agent线程模型 以下分析基于pinpoint1.7.1版本 pinpoint agent主要使用到的异步线程有4个 DeadlockMonitorThread : 死锁监测线程, ...

  8. pinpoint-dubbo插件兼容泛化调用

    背景 dubbo插件中需要记录当前调用的接口和方法,但是在泛化调用的场景下,记录的接口和方法都变成了 com.alibaba.dubbo.rpc.service.GenericService:$inv ...

  9. html编辑器的调用

    <html><head>     <metahttp-equiv="Content-type"content="text/html; cha ...

  10. linux常见命令2

    systemctl set-hostname HOSTNAME  在centos7上设置主机名,永久有效 curl -O -L http://www.gnu.org/software/gettext/ ...