Bzoj 1925 [Sdoi2010] Goblin Tribe (DP)

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

Ideas

dp+ Scrolling Array

Set F[i][j] Indicates the length is I and with 1. When J is the beginning, and the total scheme number begins with descent, there is an equation:

F[i][j]=f[i][j-1]+f[i-1][i-j]

The first part considers the length of I and begins with 1. The number of j-1 scenarios.

The second part considers the sequence starting with J, then F[i-1][j-1], but this becomes the ascending sequence, considering that the ascending sequence and descending sequence are symmetrical, take the inverse, then f[i-1][j-1]=f[i-1][i-j].

The answer is f[n][n]*2, rising and falling.

The last scrolling array optimizes space.

Code

1#include <cstdio>2 using namespacestd;3 4 Const intN = the;5 6 intf[2][n],n,m;7 8 intMain () {9scanf"%d%d",&n,&m);Tenf[1][1]=1; One      for(intI=2; i<=n;i++) A          for(intj=1; j<=i;j++) -f[i&1][j]= (f[i&1][j-1]+f[(i&1)^1][I-J])%m; -printf"%d", (f[n&1][n]*2)%m); the     return 0; -}

Bzoj 1925 [Sdoi2010] Goblin Tribe (DP)

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.