K satisfied number
Time Limit:1000MS Memory Limit:65536K
Total Submit:2 Accepted:2
Description
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.
Input
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 ).
Output
For each test case output the answer on a single line.
Sample Input
1 1
2 0
5423 8
3243421 5
2 1
Sample Output
10
9
260649
1928206
26
Source
8th SCUPC
1 #include<stdio.h> 2 #include<string.h> 3 const int mod=2008512; 4 int abs(int a){return a>0?a:-a;} 5 struct Matrix 6 { 7 int a[10][10]; 8 Matrix operator *(Matrix &l) 9 { 10 Matrix temp; 11 memset(temp.a,0,sizeof(temp.a)); 12 for(int i=0;i<10;i++) 13 for(int j=0;j<10;j++) 14 for(int k=0;k<10;k++) 15 temp.a[i][j]=(temp.a[i][j]+(int)(((a[i][k]%mod)*(long long)(l.a[k][j]))%mod))%mod; 16 return temp; 17 } 18 }M; 19 Matrix pow(Matrix &t,int k) 20 { 21 Matrix temp; 22 if(k==1) return t; 23 temp=t*t; 24 if(k&1) return pow(temp,k/2)*t; 25 return pow(temp,k/2); 26 } 27 int main() 28 { 29 int n,m; 30 while(scanf("%d%d",&n,&m)!=EOF) 31 { 32 if(n==1) 33 { 34 puts("10"); 35 continue; 36 } 37 for(int i=0;i<10;i++) 38 for(int j=0;j<10;j++) 39 if(abs(i-j)<=m) M.a[i][j]=1; 40 else M.a[i][j]=0; 41 Matrix temp=pow(M,n-1); 42 int ans=0; 43 for(int i=1;i<10;i++) 44 for(int j=0;j<10;j++) 45 ans=(ans+temp.a[i][j])%mod; 46 printf("%d\n",ans); 47 } 48 }