Bzoj 1925SDOI 2010 Goblin Tribe

Source: Internet
Author: User

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 ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.