HDU5322 Hope(DP + CDQ分治 + NTT)

题目

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;
}
时间: 2024-08-06 19:27:27

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

Tsinsen A1493 城市规划(DP + CDQ分治 + NTT)

题目 Source http://www.tsinsen.com/A1493 Description 刚刚解决完电力网络的问题, 阿狸又被领导的任务给难住了. 刚才说过, 阿狸的国家有n个城市, 现在国家需要在某些城市对之间建立一些贸易路线, 使得整个国家的任意两个城市都直接或间接的连通. 为了省钱, 每两个城市之间最多只能有一条直接的贸易路径. 对于两个建立路线的方案, 如果存在一个城市对, 在两个方案中是否建立路线不一样, 那么这两个方案就是不同的, 否则就是相同的. 现在你需要求出一共有多

BZOJ 2726: [SDOI2012]任务安排( dp + cdq分治 )

考虑每批任务对后面任务都有贡献, dp(i) = min( dp(j) + F(i) * (T(i) - T(j) + S) ) (i < j <= N)  F, T均为后缀和. 与j有关的量只有t = dp(j) - F(i) * T(j) , 我们要最小化它. dp(j)->y, T(j)->x, 那么y = F(i) * x + t, 就是给一些点和一个斜率...然后最小化截距, 显然维护下凸包就可以了. 然后因为无比坑爹的出题人....时间可以为负数, 所以要用平衡树维护(

bzoj1492--斜率优化DP+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<i 其中f[j]*r[j]/(a[j]*r[j]+b[j])是第j天最多能买多少A券,B类似. 假如我们已经找到了最优的j,那么有f[i]=(f[j]*r[j]*a[i]+f[j]*b[i])/(a[j]*r[j]+b[j]) 令x[j]=f[j]*r[j]/(a[j]*r[j]+b[j]),y[j

bzoj 2726 [SDOI2012]任务安排(斜率DP+CDQ分治)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2726 [题意] 将n个任务划分成若干个块,每一组Mi任务花费代价(T+sigma{ tj }+s)*sima{ fi },j属于Mi,T为当前时间,问最小代价. [思路] 设f[i]为将前i个任务划分完成的最小费用,Ti Fi分别表示t和f的前缀和,则不难写出转移方程式: f[i]=min{ f[j]+(F[n]-F[j])*(T[i]-T[j]+s) },1<=j<=i-1 经过

斜率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] * a[i] 由于a[i]不是单调递增的,不能直接斜率dp. 考虑有cdq分治来做,复杂度(nlog2n) 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm&

BZOJ4555求和(cdq分治+NTT)

题意: 输出f(n)对998244353(7 × 17 × 223 + 1)取模的结果.1 ≤ n ≤ 100000 其中S(i,j)是第二类Stirling数,即有i个球,丢到j个盒子中,要求盒子不为空的方案总数 S(i,j)=S(i-1,j-1)+j*S(i-1,j) (前面一项表示第i个球单独放到一个盒子中,后面一项表示放到前面j个盒子中的某一个) 分析: 首先这个n不是丧心病狂的大,所以感觉可以求i=1时的结果,i=2时的结果,i=3时的结果……,于是可以不看第一个Σ 我们考虑后面的这项

bzoj 1492 [NOI2007]货币兑换Cash(斜率dp+cdq分治)

Description Input 第一行两个正整数N.S,分别表示小Y 能预知的天数以及初始时拥有的钱数. 接下来N 行,第K 行三个实数AK.BK.RateK,意义如题目中所述 Output 只有一个实数MaxProfit,表示第N 天的操作结束时能够获得的最大的金钱 数目.答案保留3 位小数. Sample Input 3 100 1 1 1 1 2 2 2 2 3 Sample Output 225.000 HINT 测试数据设计使得精度误差不会超过10-7.对于40%的测试数据,满足N

HDU5730 Shell Necklace(DP + CDQ分治 + FFT)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell necklace with n beautiful shells contains the most sincere feeling for my best lover Arrietty, but

BZOJ 2244: [SDOI2011]拦截导弹 DP+CDQ分治

2244: [SDOI2011]拦截导弹 Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度.并且能够拦截任意速度的导弹,但是以后每一发炮弹都不能高于前一发的高度,其拦截的导弹的飞行速度也不能大于前一发.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹. 在不能拦截所有的导弹的情况下,我们当然要选择使国家损失最小.也就是拦截导弹的数量最多的方案.但是