bzoj1925 [[Sdoi2010] Goblin Tribe "DP"

Source: Internet
Author: User

Portal: http://www.lydsy.com/JudgeOnline/problem.php?id=1925

One months ago, "over" the problem, but also self-deception to understand the problem, which directly led to the last night more than 2 of the T3 explosion, now want to think is a water problem, but still want to have "know how to do" premise ... Goblin Tribe This problem can be reduced to another question: the arrangement of N, tells you the number of each number compared to the previous number is large, small, or can be, to ask for such a permutation of the scheme.

First of all, I have seen a lot of other people's problems, still foggy, so I will write a little more detailed. My writing may be somewhat different from others, but the essence is exactly the same.

First, F (i, j) indicates that the arrangement of I has been considered, the last number is J, and it is the program number of the valley. Special attention here! The arrangement of I is not necessarily the number in the closed interval [1, i]! This arrangement represents only a size relationship, a relative relationship, a sense of discretization (it can also be understood to consider the number of I, the last number is the small of the J, and it is the number of programs in the valley). This is very important, be sure to understand, if you can not understand the first look down, I will give an example later. Similarly, G (I, j) indicates that the arrangement of I has been considered, the last number is J, and it is the plan number of the mountain. Then the state transfer equation is:

Why is that so? First, the value of the F array must be transferred from the G array, because if this is a valley, then the last one is the mountain. So why is it equal to the string behind it? Consider this example, 7253, which is a scenario after you fill out the last number 3. Obviously, this scheme should belong to state F (4, 2), because 4 numbers have been considered, and 3 is the 2nd of them. Then f (4, 2) This state can be transferred from G (3, 2) and G (3, 3), in 7253 This example, F (4, 2) is transferred from G (3, 2), because in 725, 5 is the 2nd small. So what's the minimum number of i-1 in the first few years? (Of course, consider the smallest, because the larger, the more likely to move to the current state, so the maximum can be as large as the i-1 small) the answer is to be small to J. Because the number of the first I is small, it is bound to be smaller than the number of i-1 in the first J small! It can be felt by the example of just 7253, 3 is the 2nd of the first 4 numbers, and 5 is the 2nd of the first 3 numbers.

After this is understood, we can find a very easy to find, very obvious conclusion: the arrangement of a conforming n, for not a number I, will be changed to n + 1-i, the new arrangement still meet the conditions, and the original mountain into a valley, the valley into a mountain, so there are:

Ii

①②, the United State

With an auxiliary variable s, it is possible for O (1) to be transferred, the last ans = (f[n][1] + f[n][2] + ... + f[n][n]) * 2, because F represents the last one for the valley, according to that obvious conclusion, you can get the equal number of the last plan for the peak. In addition to a rolling array of compressed space can be over.

#include <cstdio> #include <cstring>const int maxn = 4205;int N, p, F[2][maxn], S, ans;int main (void) {scanf ("%  D%d ", &n, &p); f[1][1] = 1;for (int i = 2; I <= n; ++i) {memset (F[i & 1], 0, sizeof f[0]); s = 0;for (int j = I-1; J --J) {s = (s + f[i & 1 ^ 1][i-j])% p;f[i & 1][j] = s;}} for (int j = 1; j <= N; ++j) {ans = (ans + f[n & 1][j])% p;} printf ("%d\n", (ans << 1)% p); return 0;}

  

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