ALGO-3 Java k good number Blue Bridge cup

Source: Internet
Author: User

 PackageMain;ImportJava.io.InputStream;ImportJava.util.Scanner; Public classMain { Public Static LongConstant = 1000000007;  Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); LongK =In.nextlong (); LongL =In.nextlong (); //dp[i][j] = num Indicates the number of digits J of position I has num possibility        LongDp[][] =New Long[103] [103];//Two-dimensional arrays are used to hold all possible numbers//Initialize         for(inti=0;i<k;i++) {dp[1][i] = 1; }         for(inti=2;i<=l;i++)        {             for(intj=0;j<k;j++)            {                 for(intf=0;f<k;f++)                {                    if((f-j!=-1) && (f-j!=1) ) {Dp[i][j]+ = Dp[i-1][f]; DP[I][J]= dp[i][j]%constant; }                }            }        }        Longresult = 0; //Starting with 1, remove the beginning of the 0 section         for(inti=1;i<k;i++) {result+ = dp[(int) L][i]; Result= result%constant;    } System.out.println (Result); }    }
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.

Solution Ideas:

The point of the K-binary representation of any of the adjacent two-bit is not adjacent to the number, refers to the K-decimal number using the number of 0~k-1 to form the natural number, that is, 4 binary: can only contain 0,1,2 three number. At the same time for the adjacent two digits is not adjacent to the number is 2, 3 adjacent so can not appear 23 this number.

This problem uses the method of dynamic programming , through the way of partial solution, first find out the 1 digits of K-system, there are several possible, and then get the 2-digit number of possible, in order to obtain the results. In this case the array dp[][] is used to store the number of calculations, dp[i][j] = num Indicates the number of digits J of the I-bit has the possibility of Num.

Steps:

Step1: First initialized, the first row from 0 to K-1 column filled with the number 1, representing 1 possibilities

Step2: Second, three-layer loop nesting, the outer two-layer loop represents the number of rows, Note: Where the line starts from 2, the most inner row from 0 to k-1 traversal, refers to the range, the number of possible

Step3: The core equations of dynamic programming are:

DP[I][J] + = dp[i-1][f];
DP[I][J] = dp[i][j]%constant;

Side to solve the side of the mold, or the last may be due to the number is too large error.

ALGO-3 Java k good number Blue Bridge cup

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.