多校训练8,有官方题解

主要之前没写过ntt,感觉不是很懂原根

先贴一份当模板吧

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const int mo=;
const int g=;
int e[],a[*],b[*],jc[],d[],ni[],r[*];
int w[][],ans[],rg[];
int cas,m,n;
bool cmp(int a,int b)
{
return a>b;
} ll quick(ll x,int y)
{
ll s=;
while (y)
{
if (y&) s=s*x%mo;
x=x*x%mo; y>>=;
}
return s;
}
void ntt(int *a, int f)
{
for (int i=; i<n; i++)
if (i<r[i]) swap(a[i],a[r[i]]);
int now=;
for (int i=; i<n; i<<=)
{
int p=w[++now][f];
for (int j=; j<n; j+=i<<)
{
int w=;
for (int k=; k<i; k++)
{
int u=a[k+j],v=1ll*w*a[j+k+i]%mo;
a[j+k]=(u+v)%mo;
a[j+k+i]=(u-v+mo)%mo;
w=1ll*w*p%mo;
}
}
}
} int main()
{
freopen("1.in","r",stdin);
int now=(mo-)/,ng=quick(g,mo-),l=;
while (now%==)
{
w[++l][]=quick(g,now);
w[l][]=quick(ng,now);
rg[l]=quick(<<l,mo-);
now>>=;
}
jc[]=d[]=ni[]=;
for (int i=; i<=; i++)
{
jc[i]=1ll*jc[i-]*i%mo;
ni[i]=quick(jc[i],mo-);
d[i]=*d[i-]%mo;
}
scanf("%d",&cas);
while (cas--)
{
scanf("%d",&m);
for (int i=; i<=m; i++)
scanf("%d",&e[i]);
sort(e+,e++m,cmp);
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for (int i=; i<=m; i++)
a[i]=1ll*jc[i-]*d[m-i]%mo*e[i]%mo;
for (int i=; i<=m; i++)
b[m-i]=ni[i];
/* for (int i=m+1; i<2*m+1; i++)
{
int s=0;
for (int j=0; j<=i; j++) s=(s+1ll*a[j]*b[i-j]%mo)%mo;
cout <<1ll*s*ni[i-m-1]%mo<<" ";
}
cout <<endl;
break;*/
int l=;
for (n=; n<=*m+; n<<=) l++;
for (int i=; i<n; i++) r[i]=(r[i>>]>>)|((i&)<<(l-));
ntt(a,); ntt(b,);
for (int i=; i<n; i++) a[i]=1ll*a[i]*b[i]%mo;
ntt(a,);
for (int i=m+; i<*m+; i++)
ans[i-m]=1ll*a[i]*ni[i-m-]%mo*rg[l]%mo;
for (int i=; i<=m; i++)
ans[i]=(ans[i-]+ans[i])%mo;
for (int i=; i<=m; i++)
printf("%d ",ans[i]);
cout <<endl;
}
return ;
}

hdu5829的更多相关文章

  1. HDU5829 NTT

    以下这份代码并没有过.但感觉没有问题.不是蜜汁WA就是蜜汁T. #include <cstdio> #include <iostream> #include <cstri ...

  2. hdu5829 Rikka with Subset

    首先考虑本题的$O(n^2)$做法. $Part1$ 对原序列从大到小排序后,考虑每个数字对最终答案的贡献,有第x个数字对答案的贡献十分难以计算,所以考虑计算数字x是集合第K大的方案数,作为数字x对$ ...

  3. 多项式相关&&生成函数相关&&一些题目(updating...)

    文章目录 多项式的运算 多项式的加减法,数乘 多项式乘法 多项式求逆 多项式求导 多项式积分 多项式取对 多项式取exp 多项式开方 多项式的除法/取模 分治FFT 生成函数 相关题目 多项式的运算 ...

随机推荐

  1. Vue组件间通信:一个例子学会Vue组件-Vue.js学习总结)(转载)

    详情请点击 http://www.jianshu.com/p/9ad1ba89a04b

  2. jQuery添加、移除、改变class属性

    jQuery中一般有3个关于改变元素class的函数addClass.removeClass.toggleClass addClass描述: 为每个匹配的元素添加指定的样式类名$('div').add ...

  3. SSH整合需要的jar包

    [struts相关jar] commons-fileupload-1.2.2.jar commons-io-2.0.1.jar commons-lang3-3.1.jar freemarker-2.3 ...

  4. Powershell快速入门

    Powershell快速入门 来源: https://blog.csdn.net/u011054333/article/details/72567590 https://blog.csdn.net/u ...

  5. [NOIP2017 TG D1T2]时间复杂度

    题目大意:略 题解:模拟 卡点:1.数组忘清空 (考场代码风格独特...) C++ Code: #include<cstdio> #include<cstring> #incl ...

  6. HTML页面为什么设置了UTF-8仍然中文乱码

    如题,其实问题很简单,在用EditPlus写html页面的时候,发现设置为UTF-8的时候仍然出现了乱码,这是一个很奇怪的问题,而且我完全考虑了浏览器的解析问题,将title放在了了meta标签之后, ...

  7. java replace方法

    一:前言 replace自己老是忘记参数是那个替换那个,自己就把replace方法全部给弄了一遍 二:内容 package org.replaceDemo; public class ReplaceD ...

  8. HDU 5881--Tea 思维规律

    感谢http://blog.csdn.net/black_miracle/article/details/52567718 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子 ...

  9. 最大流算法 ISAP 模板 和 Dinic模板

    ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...

  10. OpenStack搭建glance

    1.创建数据库 mysql -uroot -p create database glance; grant all privileges on glance.* to glance@'localhos ...