zoukankan      html  css  js  c++  java
  • uestc 1307 windy数 数位DP

    主要注意下0的情况就可以!!!

    链接:http://acm.uestc.edu.cn/problem.php?pid=1307

    代码如下:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define ll __int64
     5 using namespace std;
     6 int bit[10],dp[20][20];
     7 int dfs(int pos,int pre,bool h,bool f)
     8 {
     9     if(pos==-1) return h==0;
    10     if(!f&&dp[pos][pre]!=-1&&!h) return dp[pos][pre];
    11     int ans=0;
    12     int e=f?bit[pos]:9;
    13     for(int i=0;i<=e;i++){
    14         if(h) ans+=dfs(pos-1,i,h&&i==0,f&&(i==bit[pos]));
    15         else if(abs(pre-i)>=2)ans+=dfs(pos-1,i,h,f&&(i==bit[pos]));
    16     }
    17     if(!f&&!h) dp[pos][pre]=ans;
    18     return ans;
    19 }
    20 int cal(int n)
    21 {
    22     int m=0;
    23     while(n){
    24         bit[m++]=n%10;
    25         n/=10;
    26     }
    27     return dfs(m-1,0,1,1);
    28 }
    29 int main()
    30 {
    31     int n,m;
    32     memset(dp,-1,sizeof(dp));
    33     while(scanf("%d%d",&n,&m)!=EOF){
    34         printf("%d
    ",cal(m)-cal(n-1));
    35     }
    36 }
    View Code

  • 相关阅读:
    setsid
    dup
    信号量
    linux标准输入输出
    linux守护进程范例
    c++字符串操作
    浏览器缓存
    bfc
    苹果手机自制铃声
    vue-cli 源码解读
  • 原文地址:https://www.cnblogs.com/xin-hua/p/3304332.html
Copyright © 2011-2022 走看看