多校训练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. HDU 1338 Game Prediction

    http://acm.hdu.edu.cn/showproblem.php?pid=1338 Problem Description Suppose there are M people, inclu ...

  2. 教你一步一步在linux中正确的安装Xcache加速php

    #第一步,下载Xcache wget http://xcache.lighttpd.net/pub/Releases/3.1.0/xcache-3.1.0.tar.gz #第一步非常简单,如果你下载不 ...

  3. 【bzoj4627】[BeiJing2016]回转寿司 离散化+树状数组

    题目描述 给出一个长度为n的序列,求所有元素的和在[L,R]范围内的连续子序列的个数. 输入 第一行包含三个整数N,L和R,分别表示寿司盘数,满意度的下限和上限. 第二行包含N个整数Ai,表示小Z对寿 ...

  4. BZOJ4456 ZJOI2016旅行者(分治+最短路)

    感觉比较套路,每次在长边中轴线处切一刀,求出切割线上的点对矩形内所有点的单源最短路径,以此更新每个询问,递归处理更小的矩形.因为若起点终点跨过中轴线是肯定要经过的,而不跨过中轴线的则可以选择是否经过中 ...

  5. 2017南开ACM校赛(网络赛) 民间题解

    orz 首先说一下这个只是民间题解,可能会有很多错误 程序还没有评测,所以可能存在问题 C题比赛的时候没想到..后来发现是个模板题,所以没有代码 希望这份题解能对读者有所启发吧... A题 直接倒序枚 ...

  6. Phaser的timer用法

    1. 延迟timer,相当于setTimeout game.time.events.add(Phaser.Timer.SECOND*5,this.delayOver,this); 2. 循环timer ...

  7. 停课day5

    一转眼,已经停课五天了. 高二大佬们已经都走了,在机房里面呆着,有时感觉很孤寂. 但是为了能学好竞赛,这些都是在所不惜的. 好像多打打比赛啊,可是cf要FQ,洛谷之类的比赛还不勤. 哎,先去学一发SP ...

  8. hdu1281(棋盘游戏,车的放置)

    Problem Description 给定一个n * m的棋盘,在棋盘里放尽量多的国际象棋中的车,使他们不能相互攻击 已知有些格子不能放置,问最多能放置多少个车 并计算出必须棋盘上的必须点. Inp ...

  9. MySQL使用笔记(二)数据库基本操作

    By francis_hao    Dec 11,2016 数据库是什么 数据库是什么呢?对于MySQL来说,数据库是存储数据库对象的容器,参考[1]中的简单解释是:数据库是一个拥有特定排放顺序的文件 ...

  10. The 13th Zhejiang Provincial Collegiate Programming Contest - D

    The Lucky Week Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the headmaster of the Marja ...