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. c/c++的一些小知识点3

    ---恢复内容开始--- ---恢复内容结束---

  2. mysql时间相减的问题

    MySQL中时间不能直接相减,如果日.分.时不同,相减结果是错误的 mysql> select t1,t2,t2-t1 from mytest;   +--------------------- ...

  3. 关于Future

    1 为什么需要Callable和Future Runnable没有返回值,也不抛异常,这样主线程不能知道子线程的执行结果. 为了解决这个问题就有了Callable和Future.Callable提供的 ...

  4. What happens when we continue stacking deeper layers on a “plain” convolutional neural network?

    http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture9.pdf The deeper model performs worse, but ...

  5. python+NLTK 自然语言学习处理五:词典资源

    前面介绍了很多NLTK中携带的词典资源,这些词典资源对于我们处理文本是有大的作用的,比如实现这样一个功能,寻找由egivronl几个字母组成的单词.且组成的单词每个字母的次数不得超过egivronl中 ...

  6. dva+antd写的一个react例子

    github地址 https://github.com/shenggen1987/dva-cli-demo 效果图片

  7. Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)

    [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/de ...

  8. 【python】将excel转成json

    excel格式如下: 转换后如下 {"BD": 1375.0, "BE": 829.0, "BF": 3.0, "BG" ...

  9. 使用curl / wget命令上传下载FTP

    curl可以在shell下轻松上传下载ftp上的文件,相比ftp命令更具有优势,因为它能在单命令条件下,下载或者上传一个ftp文件,甚至可以删除文件. 下面看实例: 1.列出ftp服务器上的目录列表: ...

  10. shell基础part2

    shell基础 一.bash中的变量 1.变量的定义:变量是计算机的内存单元,其中存放的值是可以改变的. 2.变量的设定规则:变量名不能以数字开头:变量的等号两边不能有空格,变量的值如果想有空格必须用 ...