zoukankan      html  css  js  c++  java
  • K satisfied number

    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

     

     

     

    考虑这样一个图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时,要特殊处理,以及数是没有前导零的。

     

     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 }

     

     

  • 相关阅读:
    python学习:匿名函数
    python学习:递归列出目录里的文件
    python学习:函数的递归调用
    ios开发解决遍历删除数组对象报错
    ansible入门
    git入门-分支
    Swift 使用CollectionView 实现图片轮播封装就是这样简单
    iOS复杂动画之抽丝剥茧(Objective-C & Swift)
    大数据
    点击UITableView的cell展开收缩
  • 原文地址:https://www.cnblogs.com/tiankonguse/p/2601593.html
Copyright © 2011-2022 走看看