NTT学习

学习:https://blog.csdn.net/VictoryCzt/article/details/81005812

模板:https://blog.csdn.net/ransln/article/details/79462659

只是简单地学习了下,时间紧迫,主要还是找了个模板。

例题:

hdu5829

Rikka with Subset

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1343    Accepted Submission(s): 442

Problem Description

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]~A[n](0<=A[i]<=10^9).

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[n], 要求出所有的T[k],T[i]指A数组的所有子集中前k大的和的和。答案对998244353取模。

思路:

参考了https://blog.csdn.net/cqu_hyx/article/details/52194696

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Max=5e5+100;
const ll Mod = 998244353;   // 模数
const ll SpMul = 3;  // 原根
ll qpow(ll a, ll k) {
    ll res = 1LL;
    while (k > 0) {
        if (k & 1)res = res * a % Mod;
        a = a * a % Mod;
        k >>= 1;
    }
    return res;
}

void Change(ll y[], int len) {
    for (int i = 1, j = len / 2; i < len - 1; i++) {
        if (i < j)swap(y[i], y[j]);
        int k = len / 2;
        while (j >= k) {
            j -= k;
            k /= 2;
        }
        if (j < k)j += k;
    }
}


void NTT(ll y[], int len, int on) {
    Change(y, len);
    for (int h = 2; h <= len; h <<= 1) {
        ll wn = qpow(SpMul, (Mod - 1) / h);
        if (on == -1)wn = qpow(wn, Mod - 2);
        for (int j = 0; j < len; j += h) {
            ll w = 1LL;
            for (int k = j; k < j + h / 2; k++) {
                ll u = y[k];
                ll t = w * y[k + h / 2] % Mod;
                y[k] = (u + t) % Mod;
                y[k + h / 2] = (u - t + Mod) % Mod;
                w = w * wn % Mod;
            }
        }
    }
    if (on == -1) {
        ll t = qpow(len, Mod - 2);
        for (int i = 0; i < len; i++)
            y[i] = y[i] * t % Mod;
    }
}
ll inv2;
ll pow2[Max];
ll invpow2[Max];
ll a[Max], b[Max];
ll A[Max];
ll inv[Max],fac[Max],invf[Max];   //inv[i]为i的逆元,fac[i]为阶乘i!,invf[i]为阶乘i!的逆元
void init()
{
    inv[1]=1;  
    for (int i=2;i<Max;i++)  
        inv[i]=((Mod-Mod/i)*inv[Mod%i])%Mod; 
    fac[0] = 1; 
    for(int i = 1;i<Max;i++)
        fac[i] = (fac[i-1] * (ll)i)%Mod;
    invf[0] = 1;
    for(int i = 1;i<Max;i++)
        invf[i] = (invf[i-1]*inv[i])%Mod;
    inv2 = qpow(2,Mod - 2);
    pow2[0] = 1;
    invpow2[0] = 1;
    for(int i = 1;i<Max;i++)
    {
        pow2[i] = (pow2[i-1] * 2)%Mod;
        invpow2[i] = (invpow2[i-1] * inv2)%Mod;
    }
}
int main() 
{
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int len = 1;
        while(len<n*2)len<<=1;
        for(int i = 1;i<=n;i++)
            scanf("%lld",&A[i]);
        sort(A+1,A+1+n,greater<ll>());
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i = 0;i<n;i++)
            a[i] = (pow2[n-i] * invf[i])%Mod;
        for(int i = 0;i<n;i++)
            b[i] = (fac[n-i-1] * A[n-i])%Mod;
        NTT(a,len,1);
        NTT(b,len,1);
        for(int i = 0;i<len;i++)
            a[i] = (a[i] * b[i])%Mod;
        NTT(a,len,-1);
        ll ans = 0;
        ll tmp;
        for(int i = 1;i<=n;i++)
        {
            tmp = ((invpow2[i] * invf[i-1])%Mod * a[n-i])%Mod;
            ans = (ans + tmp)%Mod;
            printf("%lld ",ans);
        }
        puts("");
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值