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 ...
随机推荐
- Windows下一个比较完美的线程池实现(使用线程池实现的Http上传下载实现)
http://blog.csdn.net/fishjam/article/details/8632049 http://download.csdn.net/user/fishjam
- MongoDB直接执行js脚本
有时候很大一段命令要执行,中间有错就得重新写,这样超麻烦的,不妨存放于js文件里,然后通过shell命令执行,重复利用率高,修改方便. 比如创建test.js print('=========WECO ...
- String.format(转)
转自:http://blog.csdn.net/lonely_fireworks/article/details/7962171 方便自己查阅. 常规类型的格式化 String类的format()方法 ...
- Partial Tree---hdu5534(完全背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意:有n个节点,让这n个节点形成一棵树,这棵树的种类有很多种,现在告诉n-1个f[i],表示度 ...
- Group GridView:用于.Net的分组显示的GridView
我的项目需要一个可以分组显示的GridView,我不会写,上网找了一圈,最终在国外的网站上找到的这个,比较符合我的要求,但它的分页得重写,它写了能分页,但我发现它的分页功能事实上并没有实现,也不知道是 ...
- Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum =
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [代码片段]OSTU算法
用在片上系统上的 //OSTU求图像的阈值 u8 otsuThreshold(u8 **img, u16 height, u16 width) { // int width = frame->w ...
- 通过SessionID和用户名来保证同一个用户不能同时登录(单点登录)
可以通过SessionID和用户名来保证同一个用户不能同时登录的问题,下面程序模仿了QQ的登录,当登录后判断当前帐号是否已经登录,如果登录.则踢掉以前登录的用户. 1.通过Application全局变 ...
- echo 命令
echo 命令 -n:表示不换行: -e:对特殊字符的解释执行,比如说变色: 例: [root@bogon ~]# echo -n "test"test[root@bogon ~] ...
- [logstash-input-file]插件使用详解(转)
最小化的配置文件 在Logstash中可以在 input{} 里面添加file配置,默认的最小化配置如下: 1 2 3 4 5 6 7 8 9 10 11 input { file ...