[bzoj1925] [Sdoi2010] [Goblin Tribe] (Sequential dynamic planning)

Source: Internet
Author: User

Description legend a long time ago, the Earth lived a mysterious creature: Goblin. Goblins like to live in the Endless Mountains. Specifically, an n-length mountain range h can be divided into n segments from left to right, each with a unique height of hi, where hi is a positive integer between 1 and N. If a mountain range is higher than all the mountains adjacent to it, the mountain range is a mountain. The mountains at the edge have only one contiguous mountain range, others have two segments (i.e. left and right). Similarly, if a mountain range is lower than all its adjacent mountains, the mountain range is a valley. Goblins have a common hobby-drinking, pubs can be set up in the valley. The Goblin's Tavern is always noisy during the day and night, and the scent of the fine wine can float to a radius of the place. Goblin is also a very alert creature, they can set up a lookout on each mountain, and take turns as lookout work to ensure that the first time to know the invasion of foreign enemies. The Goblins hope that each section of the N-section of the mountain can be built into one of the observatory or tavern, and only the entire mountain range that satisfies this condition may be inhabited by the goblins. Now you want to know how many kinds of mountain ranges may be inhabited by N in length. Two Mountains A and b are different when and only if there is an I, which makes Ai≠bi. Since this number may be large, you are only interested in dividing it by the remainder of P. Input contains only one row, two positive integers N, P. Output contains only one row, a non-negative integer, indicating the result of the answer you have asked for after P is taken. Sample Input
4 7
Sample Output
3
HINT



For 20% of data, meet n≤10;
For 40% of data, meet n≤18;
For 70% of data, meet n≤550;
For 100% of data, meet 3≤n≤4200,p≤109

Solution

At first, it turned the problem into a 1~n arrangement that satisfies the monotone jitter sequence.

It is generally possible to guess that the state is using F[I][J] to indicate that the current sequence length is I, with J as the end or beginning, or falling or rising.

But there is a problem, how to deal with the current number and the previous number of possible duplication, can only search the question

The solution is wonderful.

The definition of the state F[i][j] is changed to the sequence of length I, the number of the end is the number of J in this sequence, the scheme that is being monocytogenes

That's the way it is.

Next research shifts

We consider the nature of the arrangement

F[N][K],G[N][K], as defined above, except that the former represents a rising, the latter represents this falling

There is a very ingenious way to derive the conversion relationship between F and G.

That is, reverse the sequence F represents, and change the number of I in the sequence to n-i+1

Well, it can be concluded that f[n][k]=g[n][n-k+1]→g[n][k]=f[n][n-k+1] (that is, the relationship between falling and rising, rising and falling)

and G[n-1][k]=f[n-1][n+1-1-k]=f[n-1][n-k]→g[n-1][k]=f[n-1][n-k]

Also, for the f[n][k already to be obtained], must have been transferred by all G[n-1][i] (i∈[1,k), is

Can get,

The

Think of the topic card space, we used scrolling array over

Code

#include <stdio.h>intn,md,f[2][4210],ans;intMain () {scanf ("%d%d",&n,&MD); if(n==1) {printf ("%d\n",1%MD);return 0;} f[0][2]=1;  for(intI=3; i<=n;i++)         for(intj=1; j<=i;j++) F[i&1][j]= (f[i&1][j-1]+f[! ( i&1)][i-j+1])%MD;  for(intj=1; j<=n;j++) ans= (ans+f[n&1][J])%MD; printf ("%d\n", (ans<<1)%MD);}

[bzoj1925] [Sdoi2010] [Goblin Tribe] (Sequential dynamic planning)

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.