A non-negative interger is a K satisfied number if and only if this interger does not exist two adjacent digits, the absolute value of the difference between them is greater than K. For example: 0, 2, 5, 36 and 236 are 3 satisfied number, and are 4 satisfied number too. but 237 is not a 3 satisfied number, while it is a 4 satisfied number, for its second digit is 3 and its third digit is 7, and the absolute value of the difference between 3 and 7 is 4, which is greater than 3. Now give two interger N and K. Please tell me how many K satisfied numbers there are for all the N-digit numbers (can not have leading 0). Because the result may be very large, we make the result module 2008512 for the output. The input contains many test cases. For each test case, the input contains only one line with two integers N ( 1 <= N <= 10^9 ) and K ( 0 <= K <= 9 ). For each test case output the answer on a single line. 1 1 2 0 5423 8 3243421 5 2 1
考虑这样一个图g,节点为0、1、2、3、4、5、6、7、8、9。若|i-j|<=m,g[i][j]=1;当n大于1时,矩阵的n-1次幂中的每个元素都是对应n个节点的一条路径,每个元素的值是这个路径的走法个数。(考虑矩阵的乘法的性质。)这道题就迎刃而解了。注意n==1时,要特殊处理,以及数是没有前导零的。 #include<stdio.h> |