K-digit problem of Blue Bridge cup, Blue Bridge cup
Problem 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 data, KL <= 106;
For 50% of the data, K <= 16, L <= 10;
For 100% of data, 1 <= K, L <= 100.
Analysis: At the beginning, I thought that I could solve this problem by listing all the situations in a full row after filtering out numbers adjacent to each other. However, I found that it is difficult to select a number without repeating it, since l is up to 100, that is to say, a maximum of 100 + loops are required to select a combination of numbers. Later, I found that there was a data connection between adjacent hexadecimal systems,The sum of all K-good numbers obtained by placing j at the I-digit is obtained by removing the sum of all K-good numbers in the hexadecimal number of I-1 and the two adjacent conditions of j,
Consider using DP to answer this question.
The Code is as follows:
#include<iostream>#include<algorithm>using namespace std;long long d[105][105];const int INF = 1000000007;long long sum;int main(){int k,l,i,j,t;cin>>k>>l;for(j=0;j<k;j++)d[1][j]=1;for(i=2;i<=l;i++)for(j=0;j<k;j++)for(t=0;t<k;t++)if(t!=j-1&&t!=j+1){d[i][j]+=d[i-1][t];d[i][j]%=INF;}sum=0;for(j=1;j<k;j++){sum+=d[l][j];sum%=INF;}cout<<sum<<endl;return 0;}