设dp[n]为n个数字排列时候的答案,那么可以得到dp方程

  dp[n]=Σdp[n-i]*c(n-1,i-1)*(i-1)!*i^2(1<=i<=n)

  然后上式可以化成卷积形式,分治FFT即可。复杂度O(nlogn^2)

  

  代码

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int P = *(<<)+;
const int N = << ;
const int G = ;
const int NUM = ; LL wn[NUM],cnt[N],jc[N],dp[N],dp2[N],inv[N];
LL a[N], b[N];
char A[N], B[N];
int len,i;
LL quick_mod(LL a, LL b, LL m)
{
LL ans = ;
a %= m;
while(b)
{
if(b & )
{
ans = ans * a % m;
b--;
}
b >>= ;
a = a * a % m;
}
return ans;
} void GetWn()
{
for(int i=; i<NUM; i++)
{
int t = << i;
wn[i] = quick_mod(G, (P - ) / t, P);
}
}
void Rader(LL a[], int len)
{
int j = len >> ;
for(int i=; i<len-; i++)
{
if(i < j) swap(a[i], a[j]);
int k = len >> ;
while(j >= k)
{
j -= k;
k >>= ;
}
if(j < k) j += k;
}
} void NTT(LL a[], int len, int on)
{
Rader(a, len);
int id = ;
for(int h = ; h <= len; h <<= )
{
id++;
for(int j = ; j < len; j += h)
{
LL w = ;
for(int k = j; k < j + h / ; k++)
{
LL u = a[k] % P;
LL t = w * (a[k + h / ] % P) % P;
a[k] = (u + t) % P;
a[k + h / ] = ((u - t) % P + P) % P;
w = w * wn[id] % P;
}
}
}
if(on == -)
{
for(int i = ; i < len / ; i++)
swap(a[i], a[len - i]);
LL Inv = quick_mod(len, P - , P);
for(int i = ; i < len; i++)
a[i] = a[i] % P * Inv % P;
}
}
void Conv(LL a[], LL b[], int n)
{
NTT(a, n, );
NTT(b, n, );
for(int i = ; i < n; i++)
a[i] = a[i] * b[i] % P;
NTT(a, n, -);
}
void solve(long long l,long long r)
{
int m,i;
if (l==r)
{
dp[l]=(dp[l]+jc[l-]*l%P*l%P)%P;
return;
}
m=(l+r)>>;
solve(l,m); len=;
while (len<=r-l+) len<<=;
for (i=l;i<=r;i++)
{
if (i<=m)
a[i-l]=dp[i]*inv[i]%P;
else
a[i-l]=; if (i<r)
b[i-l+]=inv[i-l]*jc[i-l]%P*(i-l+)%P*(i-l+)%P;
else
b[i-l+]=;
}
for (i=r-l+;i<=len;i++)
{
a[i]=;b[i]=;
}
b[]=; Conv(a,b,len); for (i=m+;i<=r;i++)
{
if (i->)
dp[i]=(dp[i]+a[i-l]*jc[i-])%P;
} solve(m+,r);
}
int main()
{
GetWn();
jc[]=;inv[]=;
for (i=;i<=;i++)
{
jc[i]=jc[i-]*i %P;
inv[i]=quick_mod(jc[i],P-,P);
}
solve(,);
int n;
while (scanf("%d",&n)==)
printf("%I64d\n",dp[n]);
}

hdu5322 Hope的更多相关文章

  1. hdu5322 Hope(dp+FFT+分治)

    hdu5322 Hope(dp+FFT+分治) hdu 题目大意:n个数的排列,每个数向后面第一个大于它的点连边,排列的权值为每个联通块大小的平方,求所有排列的权值和. 思路: 考虑直接设dp[i]表 ...

  2. HDU5322 Hope(DP + CDQ分治 + NTT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5322 Description Hope is a good thing, which can ...

  3. hdu5322 Hope(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Hope Time Limit: 10000/5000 MS (Java/Othe ...

随机推荐

  1. mark down pad2

    邮箱:Soar360@live.com授权秘钥:GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDHzWdD6xhMNLGVpbP2M5SN6bnxn ...

  2. java类加载过程

    类(型)的生命周期--装载.连接.初始化.卸载 Java虚拟机通过装载.连接和初始化一个Java类型,使该类型可以被正在运行的Java程序所使用. 1.       装载 装载阶段包括三个基本动作: ...

  3. C# 64位系统中类型所占空间大小

    Boolean   8Byte DateTime 8Byte Decimal  16Byte String 引用地址空间8Bypte Int 4Bypte 类所占空间大小 (byte):各个filed ...

  4. 【Java 基础篇】【第三课】表达式、控制结构

    这两天再看敏捷开发流程,我这个算是敏捷博客吗? 哈哈o(∩_∩)o package a.b; public class Three { static void Expression() { Syste ...

  5. C#中集合汇总

    平时敲代码,只关注如何使用,没有深入去研究一些本质性的东西,靠死记硬背,不去真正理解,其实最后是很难记住的. 对于C#常见的集合,自己平时好像只有用到List,Dictionary,ArrayList ...

  6. android监听屏幕打开关闭广播无响应的情况

    android在屏幕打开和关闭的时候会发出广播,但是如果receiver配置在AndroidManifest.xml中时,receiver是接受不到任何广播的. <receiver androi ...

  7. xcode5 和code6中push后方法执行的先后问题

    在xocde5中 执行的顺序是 prepareForSegue  .viewDidLoad. didSelectRowAtIndexPath,在xcode6中 执行的顺序是  prepareForSe ...

  8. oracle 把一个用户的表结构导入到另一个用户下

    create table AM_CONTENTS as select * from bizdata008.AM_CONTENTS where 1=2

  9. Apache使用简介

    Apache使用简介 1.全局配置配置信 1) 配置持久连接 KeepAlive <On|Off>             #是否开启持久连接功能 MaxKeepAliveRequest ...

  10. Volume rendering

    Volume rendering Reconstruction filter UCDAVIS