HDU5322 Hope(DP + CDQ分治 + NTT)

时间:2023-02-06 23:57:11

题目

Source

http://acm.hdu.edu.cn/showproblem.php?pid=5322

Description

Hope is a good thing, which can help you conquer obstacles in your life, just keep fighting, and solve the problem below.

In mathematics, the notion of permutation relates to the act of arranging all the members of a set into some sequence or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting. These differ from combinations, which are selections of some members of a set where order is disregarded. For example, written as tuples, there are six permutations of the set {1,2,3}, namely: (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1). These are all the possible orderings of this three element set. As another example, an anagram of a word, all of whose letters are different, is a permutation of its letters. In this example, the letters are already ordered in the original word and the anagram is a reordering of the letters.
There is a permutation A1,A2,...An, now we define its value as below:
For each Ai, if there exists a minimum j satisfies j>i and Aj>Ai , then connect an edge between Ai and Aj , so after we connect all the edges, there is a graph G, calculate the product of the number of nodes in each component as an integer P. The permutation value is P * P.Now, Mr. Zstu wants to know the sum of all the permutation value of n. In case the answer is very big, please output the answer mod 998244353.
Just in case some of you can’t understand, all the permutations of 3 are
1 2 3
1 3 2
2 3 1
2 1 3
3 1 2
3 2 1

Input

There are multiple test cases.
There are no more than 10000 test cases.
Each test case is an integer n(1≤n≤100000).

Output

For each test case, output the answer as described above.

Sample Input

1
2

Sample Output

1
5

分析

题目大概说,对于1到n这n个数的任何一个排列A可以这样计算其价值:对所有下标i找到最小的j满足j>i且A[j]>A[i],然后i和j之间连边,最后所有连通块个数之积的平方就是该排列的价值。问所有排列的价值和是多少。

  • 首先要得出这么一个结论:从下标1...x,如果下标x的数是最大的话,那么1...x-1就与x一起组成一个连通块了。
  • 然后,$dp[i]表示i个互不相同的数的所有排列的价值和$
  • 通过枚举最大数的位置j来转移:$dp[i]\ =\ \sum A_{i-1}^{j-1}j^2dp[i-j]$
  • 可以整理成卷积形式:$dp[i]\ =\ (i-1)! \times \sum (j^2\times ((i-j)!)^{-1}dp[i-j])$
  • 然后于是就能用FFT计算了,特别的是结果模998244353,直接用NTT即可;还有要利用CDQ分治加速,累加左半边已经求得的dp值对右半边的影响。时间复杂度$O(nlog^2n)$。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 262144 const int P=998244353; // 119 * 2 ^ 23 + 1
const int G=3; long long mul(long long x,long long y){
return (x*y-(long long)(x/(long double)P*y+1e-3)*P+P)%P;
}
long long qpow(long long x,long long k,long long p){
long long ret=1;
while(k){
if(k&1) ret=mul(ret,x);
k>>=1;
x=mul(x,x);
}
return ret;
} long long wn[25];
void getwn(){
for(int i=1; i<=21; ++i){
int t=1<<i;
wn[i]=qpow(G,(P-1)/t,P);
}
} int len;
void NTT(long long y[],int op){
for(int i=1,j=len>>1,k; i<len-1; ++i){
if(i<j) swap(y[i],y[j]);
k=len>>1;
while(j>=k){
j-=k;
k>>=1;
}
if(j<k) j+=k;
}
int id=0;
for(int h=2; h<=len; h<<=1) {
++id;
for(int i=0; i<len; i+=h){
long long w=1;
for(int j=i; j<i+(h>>1); ++j){
long long u=y[j],t=mul(y[j+h/2],w);
y[j]=u+t;
if(y[j]>=P) y[j]-=P;
y[j+h/2]=u-t+P;
if(y[j+h/2]>=P) y[j+h/2]-=P;
w=mul(w,wn[id]);
}
}
}
if(op==-1){
for(int i=1; i<len/2; ++i) swap(y[i],y[len-i]);
long long inv=qpow(len,P-2,P);
for(int i=0; i<len; ++i) y[i]=mul(y[i],inv);
}
}
void Convolution(long long A[],long long B[],int n){
for(len=1; len<(n<<1); len<<=1);
for(int i=n; i<len; ++i){
A[i]=B[i]=0;
} NTT(A,1); NTT(B,1);
for(int i=0; i<len; ++i){
A[i]=mul(A[i],B[i]);
}
NTT(A,-1);
} long long fact[MAXN]={1},fact_ine[MAXN]={1}; long long A[MAXN],B[MAXN];
long long d[MAXN]={1}; /*
d[i] = fact[i-1] * Σj*j * fact_ine[i-j]*d[i-j]
*/
void cdq(int l,int r){
if(l==r) return;
int mid=l+r>>1;
cdq(l,mid);
for(int i=l; i<=mid; ++i) A[i-l]=mul(fact_ine[i],d[i]);
for(int i=1; i<=r-l; ++i) B[i]=mul(i,i);
for(int i=mid-l+1; i<=r-l; ++i) A[i]=0;
Convolution(A,B,r-l+1);
for(int i=mid+1; i<=r; ++i){
d[i]+=mul(A[i-l],fact[i-1]);
d[i]%=P;
}
cdq(mid+1,r);
} int main(){
for(int i=1; i<=100000; ++i){
fact[i]=mul(fact[i-1],i);
fact_ine[i]=qpow(fact[i],P-2,P);
}
getwn();
cdq(0,100000);
int n;
while(~scanf("%d",&n)){
printf("%I64d\n",d[n]);
}
return 0;
}

HDU5322 Hope(DP + CDQ分治 + NTT)的更多相关文章

  1. Tsinsen A1493 城市规划(DP &plus; CDQ分治 &plus; NTT)

    题目 Source http://www.tsinsen.com/A1493 Description 刚刚解决完电力网络的问题, 阿狸又被领导的任务给难住了. 刚才说过, 阿狸的国家有n个城市, 现在 ...

  2. 【BZOJ-3456】城市规划 CDQ分治 &plus; NTT

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=3456 Solution 这个问题可以考虑dp,利用补集思想 N个点的简单图总数量为$2^{ ...

  3. bzoj 2244 &lbrack;SDOI2011&rsqb;拦截导弹(DP&plus;CDQ分治&plus;BIT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2244 [题意] 给定n个二元组,求出最长不上升子序列和各颗导弹被拦截的概率. [思路] ...

  4. 【bzoj3672】&lbrack;Noi2014&rsqb;购票 斜率优化dp&plus;CDQ分治&plus;树的点分治

    题目描述  给出一棵以1为根的带边权有根树,对于每个根节点以外的点$v$,如果它与其某个祖先$a$的距离$d$不超过$l_v$,则可以花费$p_vd+q_v$的代价从$v$到$a$.问从每个点到1花费 ...

  5. 斜率dp cdq 分治

    f[i] = min { f[j] + sqr(a[i] - a[j]) } f[i]= min { -2 * a[i] * a[j] + a[j] * a[j] + f[j] } + a[i] * ...

  6. BZOJ 2726&colon; &lbrack;SDOI2012&rsqb;任务安排&lpar; dp &plus; cdq分治 &rpar;

    考虑每批任务对后面任务都有贡献, dp(i) = min( dp(j) + F(i) * (T(i) - T(j) + S) ) (i < j <= N)  F, T均为后缀和. 与j有关 ...

  7. bzoj1492--斜率优化DP&plus;cdq分治

    显然在某一天要么花完所有钱,要么不花钱. 所以首先想到O(n^2)DP: f[i]=max{f[i-1],(f[j]*r[j]*a[i]+f[j]*b[i])/(a[j]*r[j]+b[j])},j& ...

  8. bzoj 2726 &lbrack;SDOI2012&rsqb;任务安排(斜率DP&plus;CDQ分治)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2726 [题意] 将n个任务划分成若干个块,每一组Mi任务花费代价(T+sigma{ t ...

  9. 斜率dp&plus;cdq分治

    写在前面 这个东西应该是一个非常重要的套路......所以我觉得必须写点什么记录一下,免得自己忘掉了 一直以来我的斜率dp都掌握的不算很好......也很少主动地在比赛里想到 写这个的契机是noi.a ...

随机推荐

  1. 便捷的方式在手机上查看Unity3D的Console Log&lpar;调试信息&rpar;

    Logs Viewer 功能描述 Using this tool you can easily check your editor console logs inside the game itsel ...

  2. 协程、异步IO

    协程,又称微线程,纤程.英文名Coroutine,协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器 ...

  3. 基于Berkeley DB实现的持久化队列

    转自:http://guoyunsky.iteye.com/blog/1169912 队列很常见,但大部分的队列是将数据放入到内存.如果数据过多,就有内存溢出危险,而且长久占据着内存,也会影响性能.比 ...

  4. &lbrack;bootstrap&rsqb; 栅格系统和布局

    1.简介 栅格系统(grid systems),也称为“网格系统”,运用固定的格子设计版面布局,风格工整简洁.是从平面栅格系统演变而来. Bootstrap建立在12列栅格系统.布局.组件之上.以规则 ...

  5. c&num; partial类

    partial类就是说明这个类是写在几个文件里面的,这里只是一部分. partial是一个类修饰符,用于把类定义拆分为几个部分,便于代码管理,如class ClassA{void A(){;}void ...

  6. Linux下进程通信的八种方法

    Linux下进程通信的八种方法:管道(pipe),命名管道(FIFO),内存映射(mapped memeory),消息队列(message queue),共享内存(shared memory),信号量 ...

  7. UVA 3890 Most Distant Point from the Sea(二分法&plus;半平面交)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11358 [思路] 二分法+半平面交 二分与海边的的距离,由法向量可 ...

  8. 查看Windows服务器安装了那些SQL Server组件

    如何查看Windows服务器安装了那些SQL Server组件呢? 最近就遇到这样一个需求,需要知道Windows服务器是否安装了Replication组件,那么有几种方法查看Windows服务器安装 ...

  9. 获取linux工具命令源码

    总结: 通过先通过which找到命令路径path rpm -qf  path 获取源码名称n rpm -qi n   获取源码地址 [root@d mongoexport]# rpm --helpUs ...

  10. Python - Django - ORM 实例

    准备工作: 首先创建一个名为 Py_Django 的数据库 新建项目,名为 mysite0 创建完成后需要进行几项配置 mysite0/settings.py 下 首先是 html 文件相关 其次是数 ...