[SDOI2010] 地精部落(简单dp)

本文搬运自本人高中时期CSDN博客,若图片加载不出来,可到原文查看:https://blog.csdn.net/zhangtingxiqwq/article/details/142138324

https://www.luogu.com.cn/problem/P2467

一开始想错方向,小丑了

f(i,j,0/1)f(i,j,0/1) 表示还剩 ii 个,上一个在剩余数里面排名为 jj ,之前是上升/下降的方案数,转移显然

滚一下前缀和就好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 4300
int n, m, i, j, k, T, mo;
int f[N][2], g[N][2];

signed main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// srand(time(NULL));
// T=read();
// while(T--) {
//
// }
n = read(); mo = read();
for(i = 1; i <= n; ++i) f[i][0] = f[i][1] = 1;
for(i = n - 1; i >= 1; --i) {
memcpy(g, f, sizeof(f));
for(j = 1, k = 0; j <= i; ++j)
(k += g[j][1]) %= mo, f[j][0] = k;
for(j = i, k = 0; j >= 1; --j)
(k += g[j + 1][0]) %= mo, f[j][1] = k;
}
printf("%lld", (f[1][0] + f[1][1]) % mo);
return 0;
}