hdu5322 Hope
设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的更多相关文章
- hdu5322 Hope(dp+FFT+分治)
hdu5322 Hope(dp+FFT+分治) hdu 题目大意:n个数的排列,每个数向后面第一个大于它的点连边,排列的权值为每个联通块大小的平方,求所有排列的权值和. 思路: 考虑直接设dp[i]表 ...
- HDU5322 Hope(DP + CDQ分治 + NTT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5322 Description Hope is a good thing, which can ...
- hdu5322 Hope(dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Hope Time Limit: 10000/5000 MS (Java/Othe ...
随机推荐
- 蓝牙BLE ATT剖析(二)-- PDU
一.Error Handling Error Response The Error Responseis used to state that a given request cannot be pe ...
- ArcGIS API for Silverlight代码中使用Template模板
原文:ArcGIS API for Silverlight代码中使用Template模板 在项目开发中,会遇到点选中聚焦闪烁效果,但是因为在使用Symbol的时候,会设置一定的OffSetX和OffS ...
- [LeetCode]题解(python):062 Unique path
题目来源 https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x ...
- Selenium2学习-011-WebUI自动化实战实例-009-JavaScript 在 Selenium 自动化中的应用实例之一(赋值)
通常在编写 Selenium 的 WebUI 自动化脚本时,有些元素不易定位元素,或有些元素为隐藏的(此时用 WebElement.getText() 获取其值的时候,返回的结果为空),对日常的 UI ...
- windows7 密码保护 共享文件
windows7 密码保护 共享文件 2台windows7之间设置文件共享,本想使用ftp,但是配置指定用户连接,配置权限比较繁琐. 所以就想到使用window7的文件共享,并设置密码,共享整个硬盘的 ...
- iOS 的 XMPPFramework 简介
XMPPFramework是一个OS X/iOS平台的开源项目,使用Objective-C实现了XMPP协议(RFC-3920),同时还提供了用于读写XML的工具,大大简化了基于XMPP的通信应用的开 ...
- typedef 和 const
1. typedef 允许你为各种数据类型定义新名字 #include <stdio.h>typedef char *ptr_to_char; //这让我看起来,好奇怪,不好读void m ...
- POJ2262问题描述
Goldbach's Conjecture Description In 1742, Christian Goldbach, a German amateur mathematician, sen ...
- Android TimePickerDialog样式配置与TimePicker模式选择
习惯性的,把要说的内容先总结一下: TimePicker有两种模式:spinner 和clock,可通过如下方式配置: <TimePicker android:timePickerMode = ...
- 呛口大话APP 移动端到底怎么玩
[上海站]活动概况 时间:2016年04月09日13:30-16:30 地点:上海市黄浦区黄陂北路227号中区广场105室WE+联合办公空间 主办:APICloud.七牛.听云 报名网址:http:/ ...