zoukankan      html  css  js  c++  java
  • 蓝桥---K好数(dp)

    Description
    如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数。求L位K进制数中K好数的数目。例如K = 4,L = 2的时候,所有K好数为11、13、20、22、30、31、33 共7个。由于这个数目很大,请你输出它对1000000007取模后的值。
    Input
    输入包含两个正整数,K和L。
    Output
    输出一个整数,表示答案对1000000007取模后的值。
    Sample Input
    4 2
    
     
    Sample Output
    7
     
    HINT
    数据规模与约定

    对于30%的数据,KL <= 106;

    对于50%的数据,K <= 16, L <= 10;

    对于100%的数据,1 <= K,L <= 100。

    思路:

    dp[i][j],其中i代表的是数字有几位,j代表首位放j的情况有几种

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <sstream>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 //const double PI=acos(-1);
    17 #define Bug cout<<"---------------------"<<endl
    18 const int maxm=1e6+10;
    19 const int maxn=1e5+10;
    20 using namespace std;
    21 
    22 int dp[105][105];//dp[i][j],其中i表示总共有多少位(i<=L),j表示最前面的那个数字(j<K)
    23 
    24 int main()
    25 {
    26     int k,l;
    27     scanf("%d %d",&k,&l);
    28     for(int i=0;i<k;i++)////第1行初始化为1,便于下面for循环i=2时的计算 
    29         dp[1][i]=1;
    30     for(int i=2;i<=l;i++)
    31     {
    32         for(int j=0;j<k;j++)
    33         {
    34             for(int g=0;g<k;g++)
    35             {
    36                 if(g!=j-1&&g!=j+1)////左右不相邻
    37                 {
    38                     dp[i][j]+=dp[i-1][g];//循环累加上一行dp
    39                     dp[i][j]%=mod;
    40                 }    
    41             }
    42         }
    43     }
    44     LL ans=0;
    45     for(int i=1;i<k;i++)//将最后一行累加,dp[l][0]代表以0开头的数,不统计
    46     {
    47         ans+=dp[l][i];
    48         ans%=mod;
    49     }
    50     printf("%lld
    ",ans);
    51     return 0;
    52 }
  • 相关阅读:
    解释JUNIT中@BEFORECLASS和@AFTERCLASS标注的方法必须是STATIC的,而在TESTNG不必
    XXL开源社区
    java中的IO整理
    Spring MVC 原理探秘
    Servlet一次乱码排查后的总结
    正则表达式简明参考
    牛皮博客
    【转】线程安全的单例模式
    springboot下载excel(解决文件损坏问题)
    JZOJ-TGB817-SOL
  • 原文地址:https://www.cnblogs.com/jiamian/p/11790093.html
Copyright © 2011-2022 走看看