The code is short and extremely complex, with a high degree of complexity.
First, it is easy to abstract the model, to find the number of jitter subsequence of length n, then we start the egg ache.
State F[i][j] Represents the length of I starting with J, and the first bit is the number of descending jitter subsequence. Obviously, the answer is actually the number, because it doesn't count as the first one to rise.
State transition equation:
(1) First of all, we consider the number of jitter sub-sequences at the beginning of j-1, obviously such number is f[i][j-1];
(2) Then we consider the number of jitter sequences starting with J, then we ask that the second x must be smaller than J, that is, x=j-1, but obviously the first bit of this sequence rises, we need to flip it over, J-1 becomes (i-1)-(j-1) +1=i-j+1, the answer is f[ I-1][I-J+1].
F[I][J]=F[I][J-1]+F[I-1][I-J+1]
Initialize the f[2][2]=1;
Is the egg sore over? Far from it, the answer is actually f[n+1][n+1]*2, because only in this way can the first place be less than n+1 [1,n]. You must also use a scrolling array, otherwise mle;code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,p,f[2][5001];
int main ()
{
int ans,i,j,x;
scanf ("%d%d", &n,&p);
F[0][2]=1;
for (I=3;i<=n+1;++i)
{
x=i&1;
for (J=1;J<=I;++J)
f[x][j]= (f[!x][i-j+1]+f[x][j-1])%p;
}
Ans= (f[x][n+1]*2)%p;
printf ("%d\n", ans);
}
However, the Ben we see in many of God's blogs is this (by Hzwer seniors)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define LL Long long
#define INF 1000000000
using namespace std;
inline ll read ()
{
ll 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*10+ch-' 0 '; Ch=getchar ();}
return x*f;
}
int n,p;
int f[2][4505];
int main ()
{
n=read ();p =read ();
F[1][1]=1;
for (int i=2;i<=n;i++) for
(int j=1;j<=n;j++)
{
int x=i&1;
F[X][J]=F[X][J-1]+F[X^1][I-J];
f[x][j]%=p;
}
printf ("%d", n==1?1%p:f[n&1][n]*2%p);
return 0;
}
In fact, the truth is also very simple, Huang all I have moved to the pan left one ...