ZOJ 3868 (Anti-DDoS principle + fast power), zoj3868
GCD Expectation Time Limit: 4 Seconds Memory Limit: 262144 KB
Edward has a setNIntegers {A1,A2 ,...,AN}. He randomly picks a nonempty subset {X1,X2 ,...,XM} (Each nonempty subset has equal probability to be picked), and wowould like to know the expectation [Gcd(X1,X2 ,...,XM)]K.
Note thatGcd(X1,X2 ,...,XM) Is the greatest common divisor {X1,X2 ,...,XM}.
Input
There are multiple test cases. The first line of input contains an integerTIndicating the number of test cases. For each test case:
The first line contains two integersN,K(1 ≤N,K≤ 106). The second line containsNIntegersA1,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 isE, Output a single integer denotesE· (2N-1) modulo 998244353.
Sample Input
15 11 2 3 4 5
Sample Output
42
Author: LIN, Xi
Source: The 15th Zhejiang University Programming Contest
Submit Status
Question: Give a sequence and find the k times and expectations of the subset gcd.
Refresh principle...
# Include <iostream> # include <algorithm> # include <cstdio> # include <vector> # include <cstring> # include <map> using namespace std; typedef long ll; const int maxn = 1e6 + 10; const ll mod = 998244353; # define rep (I, a, B) for (int I = (a); I <(B ); I ++) # define pb push_backint cnt [maxn], a [maxn]; ll d [maxn]; ll quick_exp (ll a, int n) {ll res = 1; while (n) {if (n & 1) res = res * a % mod; a = a * a % mod; n >>=1;} return res ;} int main () {int T; scanf ("% d", & T); while (T --) {int n, k; scanf ("% d ", & n, & k); int Mgcd = 1; for (int I = 0; I <n; I ++) {int x; scanf ("% d ", & x); Mgcd = max (Mgcd, x); a [I] = x;} memset (cnt, 0, sizeof (cnt [0]) * Mgcd + 20 ); for (int I = 0; I <n; I ++) cnt [a [I] ++; ll ans = 0; /* d [I] indicates the number of solutions where gcd of the subset is I */for (int I = Mgcd; I> 0; I --) {int tot = 0; for (int j = I; j <= Mgcd; j + = I) {tot + = cnt [j]; d [I] = (d [I]-d [j]) % mod; /* reject */}/* any non-empty sub-set with an I multiple may be 2 ^ tot-1 */d [I] = (d [I] + quick_exp (2, tot)-1) % mod; d [I] = (d [I] + mod) % mod; ans + = d [I] * quick_exp (I, k ); ans % = mod;} cout <ans <endl;} return 0 ;}