码迷,mamicode.com
首页 > 其他好文 > 详细

[hdu2089][不要62]

时间:2018-10-09 20:04:05      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:==   code   +=   read   pac   ref   memset   turn   getchar   

hdu2089

思路

数位dp模板题。从高位往低位进行搜索,用pos记录当前位置,lst记录上个位置的数字,bz记录上个位置是否是6,limit来记录上个位置是否达到了上界(如果达到了,就需要对当前位置的上界进行处理)

代码

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
ll read() {
    ll x=0,f=1;char c=getchar();
    while(c<'0'||c>'9') {
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9') {
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
int f[20][2];
int a[20];
int dfs(int pos,int lst,int bz,int limit) {
    if(pos==0) return 1;
    if(f[pos][bz]!=-1) return f[pos][bz];
    int up=limit?a[pos]:9;
    int ans=0;
    for(int i=up;i>=0;--i) {
        if(i==4||(lst==6&&i==2)) continue;
        ans+=dfs(pos-1,i,i==6,limit&&i==up);
    }
    if(!limit) f[pos][bz]=ans;
    return ans;
}
int solve(int x) {
    memset(a,0,sizeof(a));
    memset(f,-1,sizeof(f));
    while(x){ 
        a[++a[0]]=x%10;
        x/=10;
    }
    return dfs(a[0],0,0,1);
}
int main() {
    int l,r;
    while(1) {
        l=read();
        r=read();
        if(!l&&!r) break;
        printf("%d\n",solve(r)-solve(l-1));
    }
    return 0;
}

[hdu2089][不要62]

标签:==   code   +=   read   pac   ref   memset   turn   getchar   

原文地址:https://www.cnblogs.com/wxyww/p/9762174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!