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.dom
Note that gcd(x1,x2,…,xm) is the greatest common divisor of {x1,x2,…,xm}.函數
There are multiple test cases. The first line of input contains an integerT indicating the number of test cases. For each test case:post
The first line contains two integers n,k (1 ≤ n, k ≤ 106). The second line containsn integers a1, a2,…,an (1 ≤ai ≤ 106).spa
The sum of values max{ai} for all the test cases does not exceed 2000000.code
For each case, if the expectation is E, output a single integer denotesE · (2n - 1) modulo 998244353.ip
1
5 1
1 2 3 4 5
42
題目連接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?get
problemId=5480input
題目大意:給一個集合。{xi}爲它的一個非空子集。設E爲[gcd(x1,x2,…,xm)]k 的指望,求E*(2^n - 1) mod 998244353string
題目分析:首先一個有n個元素的集合的非空子集個數爲2^n - 1,因此E的分母就是2^n - 1了。所以咱們要求的僅僅是E的分子,it
設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); } }