热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HDU-5829:RikkawithSubset(NTT)

Asweknow,Rikkaispooratmath.Yutaisworryingaboutthissituation,sohegivesRikkasomem
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?

InputThe 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).OutputFor 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

题意:给定一个数组,F(k)表示所有集合s的前min(K,s)大之和。求所有F(k)。

思路:先得到方程f(x),然后一般来说一个组合数*一个指数,可以直接转化一下用NTT加速;或者用第二类斯特林转化,再套NTT或FFT卷积。

 

 

关键在于找到某两个系数之和为定值,然后分别以其为“基”构造函数,然后取卷积这两个函数。

#include
#define rep(i,x,y) for(int i=x;i<=y;i++)
using namespace std;
#define ll long long
#define MOD Mod
const int G=3;
const int maxn=268576;
const int Mod=998244353;
int qpow(int v,int p)
{
    int ans=1;
    for(;p;p>>=1,v=1ll*v*v%Mod)
      if(p&1)ans=1ll*ans*v%Mod;
    return ans;
}
void rader(int y[], int len) {
    for(int i=1,j=len/2;i1;i++) {
        if(i<j) swap(y[i],y[j]);
        int k=len/2;
        while(j>=k) j-=k,k/=2;
        if(jk;
    }
}
void NTT(int y[],int len,int opt) {
    rader(y,len);
    for(int h=2;h<=len;h<<=1) {
        int wn=qpow(G,(MOD-1)/h);
        if(opt==-1) wn=qpow(wn,Mod-2);
        for(int j=0;jh) {
            int w=1;
            for(int k=j;k2;k++) {
                int u=y[k];
                int t=(ll)w*y[k+h/2]%MOD;
                y[k]=(u+t)%MOD;
                y[k+h/2]=(u-t+MOD)%MOD;
                w=(ll)w*wn%MOD;
            }
        }
    }
    if(opt==-1) {
        int t=qpow(len,MOD-2);
        for(int i=0;iMOD;
    }
}
int inv[maxn],A[maxn],B[maxn],a[maxn],f[maxn],p2[maxn];
int main() {
    int T,N;
    f[0]=inv[0]=p2[0]=1;
    rep(i,1,100000) p2[i]=(ll)p2[i-1]*2%Mod;
    rep(i,1,100000) f[i]=(ll)f[i-1]*i%Mod;
    inv[100000]=qpow(f[100000],Mod-2);
    for(int i=100000-1;i>=0;i--) inv[i]=(ll)inv[i+1]*(i+1)%Mod;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        int len=1; while(len<=N*2) len<<=1;
        rep(i,0,len) A[i]=B[i]=0;
        rep(i,1,N) scanf("%d",&a[i]);
        sort(a+1,a+N+1); reverse(a+1,a+N+1);
        rep(i,0,N-1){
             A[i]=inv[i];
             B[i]=(ll)f[N-i-1]*p2[i]%Mod*a[N-i]%Mod;
        }

        NTT(A,len,1); NTT(B,len,1);
        rep(i,0,len-1) A[i]=(ll)A[i]*B[i]%Mod; //乘完,不能只乘到N
        NTT(A,len,-1); int ans=0;
        rep(i,1,N){
            (ans+=(ll)inv[i-1]*A[N-i]%Mod)%=Mod;
            printf("%d ",ans);
        }
        puts("");
    }
    return 0;
}

 


推荐阅读
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 本文由编程笔记#小编整理,主要介绍了关于数论相关的知识,包括数论的算法和百度百科的链接。文章还介绍了欧几里得算法、辗转相除法、gcd、lcm和扩展欧几里得算法的使用方法。此外,文章还提到了数论在求解不定方程、模线性方程和乘法逆元方面的应用。摘要长度:184字。 ... [详细]
  • 一.元祖类型 (tuple)1.什么是元祖?用途:用于存放多个值,当存放的多个值只有读的需求没有改变的需求时,用元祖最合适.定义方式:在()内用逗号分隔开的多个任意类型的值t(1, ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • 用户视图(查看运行状态或其他参数)系统视图(配置设备的系统参数)system-viewEntersystemview,returnuservi ... [详细]
  • 基于词向量计算文本相似度1.测试数据:链接:https:pan.baidu.coms1fXJjcujAmAwTfsuTg2CbWA提取码:f4vx2.实验代码:imp ... [详细]
  • 数学和统计方法sum对数组中全部或某轴向的元素求和。零长度的数组的sum为0。mean算术平均数。零长度的数组的mean为NaN。importnumpyas ... [详细]
author-avatar
永无止境
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有