AGC024E - Sequence Growing Hard

AGC024E - Sequence Growing Hard

题目描述

Solution

我们可以把问题看成如下形式:
你有一个空序列,每次要加入一个元素 x x , x [ 1 , k ] x \in [1,k] ,使得新的序列字典序比上一个序列的字典序大。

显然如果我们加入一个数 x x ,则它后面一个位置的数一定小于 x x (相等时可以将 x x 往后移动,还是同一种方案)。

我们把这个序列看作一棵树,一开始是空的,每次在 x x 元素前面加入一个元素 y y ,看作在 x x 结点下面挂上一个结点 y y y y x x 的儿子。

于是所有的操作序列都可以唯一表示为一棵树,而这棵树是合法的当且仅当父亲结点的权值小于儿子节点的权值。

所以问题转化为了求合法的树的方案数,组合数计算,前缀和优化即可。

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int MAXN=600005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
int C[505][505],f[505][505],s[505][505],n,k,mods;
int upd(int x,int y){ return x+y>=mods?x+y-mods:x+y; }
int main()
{
	n=read(),k=read(),mods=read();
	for (int i=0;i<=n;i++) C[i][i]=C[i][0]=1;
	for (int i=1;i<=n;i++)
		for (int j=1;j<i;j++) C[i][j]=upd(C[i-1][j-1],C[i-1][j]);
	for (int i=0;i<=k;i++) f[1][i]=1,s[1][i]=i+1;
	for (int i=2;i<=n+1;i++)
		for (int j=0;j<=k;j++)
		{
			for (int x=1;x<i;x++) 
				f[i][j]=upd(f[i][j],1ll*upd(s[x][k],mods-s[x][j])*f[i-x][j]%mods*C[i-2][x-1]%mods);
			s[i][j]=(!j)?0:upd(s[i][j-1],f[i][j]);
		}
	printf("%d\n",f[n+1][0]);
	return 0;
}

发布了94 篇原创文章 · 获赞 6 · 访问量 8555

猜你喜欢

转载自blog.csdn.net/xmr_pursue_dreams/article/details/103432666