设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. Cookie操作

    1.写cookie方法 #region 登录时写入cookie public static void LoginCookieSave(string uid, string loginname, str ...

  2. Apache的HBase与cdh的hue集成(不建议不同版本之间的集成)

    1.修改hue的配置文件hue.ini [hbase] # Use full hostname with security. hbase_clusters=(Cluster|linux-hadoop3 ...

  3. java调用Http请求 -HttpURLConnection学习

    最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放 ...

  4. C#类的继承

    public class Animal { public string word=""; //virtual表示虚方法,用以被重写 public virtual void say( ...

  5. CLM

    https://github.com/TadasBaltrusaitis/CLM-framework

  6. Speed-BI 图表功能:服装订货与销售匹配分析

    在作为一个买手我们根据对市场的预测,订了一批的货回来. 我们总会有一个疑问:我的订货与市场的需求是一致的吗?是否出现了较大偏差.这时我们通过分析两个指标:订货占比与销售占比的差异,进行订货与销售的匹配 ...

  7. 开机取消显示 系统准备工具(Sysprep)

    问题: 解决办法: 1.关闭系统准备工具 3.14 2.桌面  ---  开始 ---  运行  ----  输入  XCOPY %windir%\System32\svchost.exe %wind ...

  8. T4自动生成数据库C#实体类学习(1)

    第一个测试的Demo <#@ template language="C#" debug="True" hostspecific="True&qu ...

  9. weka数据预处理

    Weka数据预处理(一) 对于数据挖掘而言,我们往往仅关注实质性的挖掘算法,如分类.聚类.关联规则等,而忽视待挖掘数据的质量,但是高质量的数据才能产生高质量的挖掘结果,否则只有"Garbag ...

  10. Socket 编程