Nine degree OJ 1552 seat problem (DP)

Source: Internet
Author: User

A one-dimensional array that was just started, but because of the large number behind it, to take a model of it, so a one-dimensional array subtraction operation, the result is wrong. So we still have to use a two-dimensional array, with dp[n][0] to indicate the number of the nth-digit method, Dp[n][1] indicates the number of total methods of female students in the nth position. Then the boy's state transfer equation is dp[n][0] = dp[n-1][0] + dp[n-1][1]; It means that as long as the last one to put boys, regardless of the front is what can be put, all meet the subject of the condition, the girl's state transfer equation for dp[n][1] = dp[n-1][1] + dp[n-2][0]; It means that when the nth place girls, then the first n-1 bit must be a girl to meet the conditions, so to add n-1-bit female number, but there is a situation dp[n-1][1] there is not included, is ".... Men and women "This situation, because this situation in dp[n-1][1] is not satisfied with the requirements, then add this less, so to add n-2 boys. Be careful when initializing the data. The key is to read the question carefully!

1#include <cstdio>2#include <iostream>3#include <cstring>4 5 using namespacestd;6 Const intN =1005;7 Const Long LongMoD =1000000007;8 Long Longdp[n][2];//Dp[i][0] Indicates the number of total methods for boys in the first position of the row9 intMain ()Ten { One     intN; AMemset (DP,0,sizeof(DP)); -dp[1][0] =1;//Boys -dp[1][1] =0;//Schoolgirl thedp[2][0] =1; -dp[2][1] =1; -dp[3][0] =2; -dp[3][1] =2; +      for(inti =4; i < N; i++) -     { +dp[i][0] = Dp[i-1][0] + dp[i-1][1]; Adp[i][0] %=MoD; atdp[i][1] = Dp[i-1][1] + dp[i-2][0]; -dp[i][1] %=MoD; -     } -      while(~SCANF ("%d", &N)) -     { -          inprintf"%lld\n", (dp[n][0] + dp[n][1]) %MoD); -     }     to     return 0; +}

Nine degree OJ 1552 seat problem (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.