Rikka with Subset

Rikka with Subset

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:


Yuta has n numbers A[1]~A[n] and a number K. For any none empty subset S of the numbers, the value of S is equal to the sum of the largest min(|S|,k) numbers in S. The value of the array A is equal to the sum of the value of all none empty subset of the numbers.


Now Yuta shows the n numbers, And he wants to know the value of the array for each K in [1,n].


It is too difficult for Rikka. Can you help her?

Input

The first line contains a number t(1<=t<=10), the number of the testcases.


For each testcase, the first line contains a number n(1<=n<=100000), the number of numbers Yuta has. The second line contains n number A[1]~An.

Output

For each testcase, print a line contains exactly n numbers, the ith number is the value of the array when K=i. The answer may be very large, so you only need to print the answer module 998244353.

Sample Input

2

3

1 1 1

5

1 2 3 4 5

Sample Output

7 11 12

129 201 231 239 240

思路:

先将\(a_0\)~\(a_{n-1}\)按从大到小排序,令\(A_i=2^{n-i}inv_{i!},B_i=a_{n-i-1}(n-i-1)!,\)\(C=AB,ans_i=inv_{2^{i+1}}C_{n-i-1}inv_{(i+1)!}+ans_{i-1},ans_{-1}=0\)

证明:

求出所有数为第\(k\)位时的贡献:\(x_i\)

\(x_i=\sum_{i=k-1}^{n-1}C_i^{k-1}2^{n-i-1}a_i\\=\sum_{i=k-1}^{n-1}\frac{i!}{(i-k+1)!(k-1)!}2^{n-i-1}a_i\\=\frac{1}{(k-1)!}\sum_{i=k-1}^{n-1}\frac{i!}{(i-k+1)!}2^{n-i-1}a_i\\=\frac{1}{(k-1)!}\sum_{i=0}^{n-k}\frac{(i+k-1)!}{i!}2^{n-i-k}a_{i+k-1}\\=\frac{1}{(k-1)!2^k}\sum_{i=0}^{n-k}\frac{(i+k-1)!}{i!}2^{n-i}a_{i+k-1}\\=\frac{1}{(k-1)!2^k}\sum_{i=0}^{n-k}\frac{2^{n-i}}{i!}a_{n-(n-k-i)-1}(n-(n-k-i)-1)!\\=\frac{1}{(k-1)!2^k}\sum_{i=0}^{n-k}A_iB_{n-i-1}\\最后用个前缀和就可以了\mathbb{qwq}\)

\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)

#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
# define read read1<int>()
# define Type template<typename T>
Type inline T read1(){
    T n=0;
    char k;
    bool fl=0;
    do (k=getchar())=='-'&&(fl=1);while('9'<k||k<'0');
    while(47<k&&k<58)n=(n<<3)+(n<<1)+(k^48),k=getchar();
    return fl?-n:n;
}
# define f(i,l,r) for(int i=(l);i<=(r);++i)
# define fre(k) freopen(k".in","r",stdin);freopen(k".ans","w",stdout)
# define ll int64_t
class Array{
    private:
        vector<int>a;
    public:
        Array(){}
        Array(const int size):a(size,0){}
        void push(int n){a.push_back(n);}
        Array(int* l,int* r){while(l!=r)push(*l),++l;}
        inline int size(){return a.size();}
        inline int& operator [] (const int x){return a[x];}
        void resize(int n){a.resize(n);}
        void clear(){a.clear();}
        void swap(){reverse(a.begin(),a.end());}
};
int qkpow(int b,int m,int mod){
    int tem=b,ans=1;
    for(;m;m>>=1,tem=(ll)tem*tem%mod)
        if(m&1)ans=(ll)ans*tem%mod;
    return ans;
}
const int mod=998244353,g=3;
int* NTT(const int len,Array& a,const bool Ty,int* r=NULL){
    if(!r){
        r=new int[len];
        r[0]=0;int L=log2(len);
        f(i,0,len-1)
            r[i]=(r[i>>1]>>1)|((i&1)<<L-1);
    }
    f(i,0,len-1)
        if(i<r[i])swap(a[i],a[r[i]]);
    for(int i=1;i<len;i<<=1){
        int T=qkpow(Ty?g:332748118,(mod-1)/(i<<1),mod);
        for(int W=i<<1,j=0;j<len;j+=W){
            ll omega=1;
            for(int k=0;k<i;++k,omega=omega*T%mod){
                ll x(a[j+k]),y(omega*a[i+j+k]%mod);
                a[j+k]=x+y;(a[j+k]>mod)&&(a[j+k]-=mod);
                a[i+j+k]=x-y+mod;(a[i+j+k]>mod)&&(a[i+j+k]-=mod);
            }
        }
    }
    return r;
}
Array operator * (Array x,Array y){
    int n=x.size()-1,m=y.size()-1;
    int limit=1;
    while(limit<=n+m)limit<<=1;
    Array ans;
    x.resize(limit+1);
    y.resize(limit+1);
    int *r;
    r=NTT(limit,x,1);
    NTT(limit,y,1,r);
    f(i,0,limit)x[i]=(ll)x[i]*y[i]%mod;
    NTT(limit,x,0,r);
    int tem=qkpow(limit,mod-2,mod);
    f(i,0,n+m)ans.push((ll)x[i]*tem%mod);
    return ans;
}
Array& operator *= (Array& x,Array y){
    return x=x*y;
}
int inv2=499122177,inv[100001],fac[100001],a[100001];
Array x,y;
int s;
void init(){
    inv[0]=inv[1]=fac[0]=fac[1]=1;
    for(int i=2;i<=100000;++i)
        inv[i]=qkpow(fac[i]=(ll)fac[i-1]*i%mod,mod-2,mod);
}
void work(){
    x.clear();y.clear();
    s=read;
    for(int i=1;i<=s;++i)a[i]=read;
    sort(a+1,a+s+1);
    reverse(a+1,a+s+1);
    for(int i=0;i<s;++i)x.push((ll)qkpow(2,s-i,mod)*inv[i]%mod);
    for(int i=0;i<s;++i)y.push((ll)a[i+1]*fac[i]%mod);
    y.swap();
    x*=y;
    ll tem=0,r=inv2;
    for(int i=1;i<=s;++i){
        printf("%lld ",tem=(tem+r*x[s-i]%mod*inv[i-1])%mod);
        r=r*inv2%mod;
    }
    putchar('\n');
}
int main(){
    init();
    for(int T=read;T--;work());
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/SYDevil/p/12070010.html