ZOJ3868:GCD expectation

Source: Internet
Author: User
Tags greatest common divisor

Edward has a set n of integers { a 1, a 2,..., a n }. He randomly picks a nonempty subset { x 1, x 2,..., x m } (each nonempty subset have equal probability to be picked ), and would like to know the expectation of [gcd( x 1, x 2,..., x m )] k .

Note that gcd( x 1, x 2,..., x m ) is the greatest common divisor of { x 1, x 2,..., x }.

Input

There is multiple test cases. The first line of input contains an integer indicating the number of the T test cases. For each test case:

The first line contains the integers n , k (1≤ n , k ≤106). The second line contains n integers a 1, a 2,..., a n (1≤ a i ≤106).

The sum of Values max{} for all the a i test cases does not exceed 2000000.

Output

For each case, if E the expectation are, output a single integer denotes E • (2 n -1) modulo 998244353.

Sample Input
15 11 2 3 4 5
Sample Output
42
For the sequence of n numbers, all non-empty-set, the expectation is that the GCD k-th-square
Output expectation multiplied by (2^n-1) value
The probability of 1 in the title is 26/31,2 probability is 2/32,3,4,5 probability is 1/32
The expectation is 42/32, so the answer is 42, which means that our goal is to find the desired molecular part.
For the sequence of N, there must be a 2^n-1 non-empty set, where the largest gcd is not greater than the original sequence of Max, then we use the array fun to record its expectations
For example, in the title, expect 1 of 26, expect 2 of 2, expect for 3,4,5 of only 1
We can split to calculate, first for 1, the expectation for a multiple of 5, then the five of all non-empty set for 2^5-1 species, get s= (2^5-1) * *;
The expectation for 2,2 should be 2, but in the expectation of 1 of all the subsets, we repeated the 2 expectations, more so we should subtract the expected number of repetitions, now 2 of the expectation should be 1, then for the multiples of 2, two, 2, 4, the composition of the non-empty set of 2^2-1, so get s+ = (2^2-1) * *
for 3,4,5;
 
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm >using namespace std, #define LS 2*i#define rs 2*i+1#define up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x; i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define LL long longconst double pi = acos (-1.0); # Define Len 1000005#define mod 998244353const LL inf = 1<<30; LL t,n,k; LL A[len]; LL TWO[LEN],FUN[LEN],CNT[LEN],VIS[LEN],MAXN;    ll Power (ll X, ll y) {ll ans = 1;        W (y) {if (y&1) ans= (ans*x)%mod;        x= (x*x)%mod;    y/=2; } return ans;    int main () {LL i,j;    scanf ("%lld", &t);    Two[0] = 1;    Up (i,1,len-1) two[i] = (two[i-1]*2)%mod;        W (t--) {mem (cnt,0);        MEM (vis,0);        scanf ("%lld%lld", &n,&k);        MAXN = 0;       Up (i,0,n-1) {     scanf ("%lld", &a[i]);                if (!vis[a[i]]) {Vis[a[i]] = 1;            Cnt[a[i]] = 1;            } else cnt[a[i]]++;        MAXN = max (Maxn,a[i]);        } fun[1] = 1;        Up (I,2,MAXN) fun[i] = Power (i,k);        Up (I,1,MAXN) {for (j = i+i; j<=maxn; j+=i) fun[j]= (fun[j]-fun[i])%mod;        } LL ans = (two[n]-1) *fun[1]%mod;            Up (I,2,MAXN) {LL cc = 0;            for (j = i; j<=maxn; j+=i) {if (Vis[j]) cc+=cnt[j];            } LL tem = (two[cc]-1) *fun[i]%mod;        Ans = (ans+tem)%mod;    } printf ("%lld\n", (ans+mod)%mod); } return 0;}


ZOJ3868:GCD expectation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.