"Sdoi" "DP" "Scrolling array" "bzoj1925" Goblin Tribe

Source: Internet
Author: User

1925: [Sdoi2010] Goblin Tribe time limit: ten Sec Memory Limit: MB
Submit: 814 Solved: 494
[Submit] [Status] [Discuss] 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 Input4 7Sample Output3HINT



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

Source

First round Day2


This question is really god ... The amount of code and the amount of thinking is completely inversely proportional to ...

Idea: more complex. The first is to abstract the model, which is the number of scenarios for a jitter sequence of length n. F[I][J] indicates that the length of J begins with the number of I, and the start is the descent scheme, the answer is multiply by 2 (consider the number of scenarios that start is rising), and then take a deeper thought:

<1> considering the number of starts is j-1, its scheme number is f[i][j-1].

<2> considering the start of the number is J, because we are asking is to start falling, so its second bit must be smaller than it, that is, j-1, but so the beginning of the j-1 sequence becomes ascending, so to flip it over, so that j-1 becomes (i-1)-(j-1) +1=i-j+1.

So the DP equation is f[i][j]=f[i][j-1]+f[i][i-j+1] (1<=j<=i), f[2][2]=1 when initialized. You need to use the scrolling array (the principle of reuse), or the MLE.

Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm>using namespace std;int n,p,x,f[2][5001];int main () {scanf ("%d%d", &n,&p); f[0][ 2]=1;for (int i=3; i<=n+1; i++) {x=i&1;for (int j=1; j<=i; j + +) f[x][j]= (f[x][j-1]+f[!x][i-j+1])%p;} printf ("%d\n", (f[x][n]*2)%p); return 0;}



"Sdoi" "DP" "Scrolling array" "bzoj1925" Goblin Tribe

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.