hdu5829
多校训练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的更多相关文章
- HDU5829 NTT
以下这份代码并没有过.但感觉没有问题.不是蜜汁WA就是蜜汁T. #include <cstdio> #include <iostream> #include <cstri ...
- hdu5829 Rikka with Subset
首先考虑本题的$O(n^2)$做法. $Part1$ 对原序列从大到小排序后,考虑每个数字对最终答案的贡献,有第x个数字对答案的贡献十分难以计算,所以考虑计算数字x是集合第K大的方案数,作为数字x对$ ...
- 多项式相关&&生成函数相关&&一些题目(updating...)
文章目录 多项式的运算 多项式的加减法,数乘 多项式乘法 多项式求逆 多项式求导 多项式积分 多项式取对 多项式取exp 多项式开方 多项式的除法/取模 分治FFT 生成函数 相关题目 多项式的运算 ...
随机推荐
- To Chromium之VS调试追踪
启动的code: for(;;){...WaitForWork()}base.dll!base::MessagePumpForUI::DoRunLoop ...
- Pandoc中的Markdown语法
概述 Pandoc中支持扩展修订版本的Markdown语法 使用pandoc中支持的Markdown语法用 -f markdown 使用标准Markdown语法用 -f markdown_strict ...
- c++ 中反正单词用到了resize()
resize(n) 调整容器的长度大小,使其能容纳n个元素.如果n小于容器的当前的size,则删除多出来的元素.否则,添加采用值初始化的元素. 题目如下: 151. Reverse Words in ...
- div加了float后 四个特性
1.宽度变成0 2.左漂浮 或者右漂浮 3.后面的标签占据原来的位置 4对前面的div没有影响 他会浮动到前面div下面
- eclipse启运时显示:Workspace in use or cannot be created, choose a different one
The time when I runned Eclipse in my computer, it has this information displayed: WorkSpace *** in u ...
- php: Can't use function return value in write context
关于empty()函数, php手册中提到,php5.5之前empty()函数只支持检查变量,传入任何其他的表达式或函数都会产生语法错误. Note: Prior to PHP 5.5, empty( ...
- [Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 安卓sdk安装教程
http://blog.csdn.net/love4399/article/details/77164500
- POJ3349 Snowflake Snow Snowflakes (hash
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 48624 Accep ...
- HDU1166 敌兵布阵(树状数组实现
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...