Algorithm training K good numbers, algorithm training

Source: Internet
Author: User

Algorithm training K good numbers, algorithm training
QuestionProblem description

If the K-base of a natural number N indicates that any adjacent two digits are not adjacent numbers, then we say that this number is a good K number. Evaluate the number of good K numbers in the K-digit K-number. For example, if K is 4 or L is 2, the number of all K elements is 7, including 11, 13, 20, 22, 30, 31, and 33. Because the number is large, please output the value after modulo 1000000007.

Input Format

The input contains two positive integers, K and L.

Output Format

Output an integer that indicates the value after the modulo operation on 1000000007.

Sample Input

4 2

Sample output

7

Data scale and conventions

For 30% of the data, K ^ L <= 10 ^ 6;

For 50% of the data, K <= 16, L <= 10;

For 100% of data, 1 <= K, L <= 100.

Ideas:

At first, I had a hard time worrying about the question, because I had always considered that any two adjacent persons were not "same" numbers, and finally I had no choice but to go over Baidu for dynamic planning? Okay, adjacent ......
Let's talk about the idea of dynamic planning:
Nums [I] [j] stores the number of I-Bits. The first number is the number of K-bits of j, calculate the result of two bits and the result of three bits... Finally, the result of l-bit is obtained. The K-in is just a range.
At last, take it seriously.

Code
import java.util.*;import java.math.*;import java.util.regex.*;public class Main {    final static int MOD = 1000000007;    final static int INF = 0x3f3f3f3f;    final static int NUM = 100;    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int k,l;        int ans;        while(sc.hasNext()) {            k=sc.nextInt();l=sc.nextInt();            ans=0;            int[][] nums = new int[l][k];            for(int i=0;i<l;i++)for(int j=0;j<k;j++)nums[i][j]=0;            for(int j=0;j<k;j++)                nums[0][j]=1;            for(int i=1;i<l;i++)                for(int j=0;j<k;j++)                    for(int x=0;x<k;x++) {                        if(x!=j-1 && x!=j+1) {                            nums[i][j]+=nums[i-1][x];                            nums[i][j]%=MOD;                        }                    }            for(int j=1;j<k;j++) {                ans+=nums[l-1][j];                ans%=MOD;            }            System.out.println(ans);        }    }}

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.