Algorithm note _077: Blue Bridge cup practice K good number (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description 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 Formatoutputs an integer that represents the value after which the answer is modulo 1000000007. Sample Input4 2Sample Output7data size and conventions

For 30% of data, KL <= 106;

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

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

  2 Solutions

This paper mainly examines the application of dynamic planning ideas.

The specific code is as follows:

ImportJava.util.Scanner; Public classMain { Public Static intMoD = 1000000007;  Public int[] DP =New int[102] [102];//dp[3][4] = num indicates that the 3rd digit size in the L-bit K-decimal number is 4, so that the 3-digit number is in num case         Public voidPrintresult (intKintL) { for(inti = 0;i < k;i++) dp[1][i] = 1;//target number lowest bitInitialize to 0~k-1 in turn, with each number appearing only once         for(inti = 2;i <= l;i++) {//the position of the number, the highest bit is L, the lowest bit is 1             for(intj = 0;j < k;j++) {                   for(intF = 0;f < k;f++) {                    if(F-1! = J && F + 1! =j) {Dp[i][j]+ = Dp[i-1][f]; DP[I][J]%=MoD; }                }            }        }        intsum = 0;  for(inti = 1;i < k;i++) {//removal of the highest bit of 0Sum + =Dp[l][i]; Sum%=MoD;        } System.out.println (sum); }         Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); intK =In.nextint (); intL =In.nextint ();    Test.printresult (K, L); }}

Resources:

1. algorithm Training k good number

Algorithm note _077: Blue Bridge cup practice K good number (Java)

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.