Multi-stage decision-making problem of dynamic programming Blue Bridge Cup K good number

Source: Internet
Author: User

Problem description

If an arbitrarily contiguous two-bit in the K-binary representation of a natural number n is not an adjacent number, then we say that this number is a K-good number. Ask for the number of K good numbers in the L-bit K-binary number. For example k = 4,l = 2, all k good numbers are 11, 13, 20, 22, 30, 31, 33 total 7. Since this is a large number, please output it to 1000000007 after the modulo value.

Input format

The input contains two positive integers, K and L.

Output format

Outputs an integer that represents the value after which the answer is modulo 1000000007.

Sample input

4 2

Sample output

7

Data size and conventions

For 30% of data, KL <= 106;

For 50% data, K <=, L <= 10;

For 100% of data, 1 <= k,l <= 100.

The idea of multi-stage decision-making in dynamic programming is to get a part of the solution every time a decision is made (i.e. one stage), then the complete solution appears when all the decisions are done.

Let's take this example to see how to implement the problem.

The first simple way to understand the test instructions is the number of digits that are not adjacent to each number in the entire number string that is required.

Simply put, reduce the scale of the problem first , so that you can think about the whole topic.

For example, if the length is 1, then numbers other than 0 can be filled in.

If the length is 2, I must know two things at this time, first it is not adjacent to the number of what (the previous bit), the second one a number with it as the end of it all the total number of satisfied conditions. Can find that I have to complete the second step is to use the first step.

That is, each stage is determined by the previous stage.

If the length is 3, the solution of the phase of length 2 must also be used.

We use a two-dimensional array diagram to represent such a process.

Of course, there is a pit point, if you want to use the previous phase of sum minus a few non-conforming values, it is likely to appear because the value is too large, the value of the modulus has been subtracted from two large values and negative, then the best solution is to use the full loop Plus, do not appear minus.

The code is as follows:

1#include <iostream>2#include <cstdio>3 #defineMAXN 1054 #defineMOD%10000000075 using namespacestd;6 Long LongDP[MAXN][MAXN];7 intMain ()8 {9     Long Longi,j,k,c,l,sum=0;TenCin>>k>>l; One     //Initialize the first lattice Adp[0][1]=0;  -      for(i=1; i<k;i++) -dp[i][1]=1; thesum=k-1; -      for(i=2; i<=l;i++) -{//Lattice -          for(j=0; j<k;j++) +         { -             if(j==0) +             { ADp[j][i]= (dp[j][i]+ (dp[j][i-1] mod) mod; at                  for(c=2; c<k;c++)  -Dp[j][i]= (dp[j][i]+ (dp[c][i-1] MoD) mod;//must loop plus, subtract some values with sum minus overflow!!  -             } -             Else if(j==k-1) -             { -                  for(c=0; c<k-2; C + +) inDp[j][i]= (dp[j][i]+ (dp[c][i-1] mod) mod; -Dp[j][i]= (dp[j][i]+ (dp[j][i-1] mod) mod; to             } +             Else -             { the                  for(c=0; c<k;c++) *                 { $                     if(c!=j-1&&c!=j+1)Panax NotoginsengDp[j][i]= (dp[j][i]+ (dp[c][i-1] mod) mod; -                 } the             } +             //cout<<dp[j][i]<< ""; A          }  thesum=0; +           for(j=0; j<k;j++) -sum= (sum) mod+(Dp[j][i]) MOD) MOD; $         //cout<<endl; $         //cout<<sum<<endl; -     } -cout<<sum<<Endl; the     return 0; -}

Multi-stage decision-making problem of dynamic programming Blue Bridge Cup K good number

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.