数位dp汇总

hdu3555:

链接:http://hdu.hustoj.com/showproblem.php?pid=3555

问1~n中有多少个数字x, 数字x中包含49;

分析:

   基本的数位dp,不过最近在网上看到记忆化搜索来写数位dp,写法相当简单。

  dp[len][0] 表示长度为len, 并且前一个数字不为4符合条件的数字个数; (前一个指的是,从末尾开始,长度为len之前的那个数字)

  dp[len][1]表示长度为len, 并且前一个数字为4符合条件的数字个数;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int a[20];
long long d[20][2];

long long dfs(int len , bool state, bool f){  // state 表示前一个数字是否为4,  f表示是否可以直接返回答案;
    if(!len) return 1;
    if(f && d[len][state]!=-1) return d[len][state];
    int fmax = f ? 9 : a[len-1];
    long long res = 0;
    for(int i = 0; i <= fmax; ++i){
        if(state && i == 9) continue;
        res += dfs(len-1, i==4, f||(i!=fmax));
    }
    if(f){
        d[len][state] = res;
    }
    return res;
}

long long solve(long long n){
    int cnt = 0;
    while(n){
        a[cnt++] = n%10;
        n/= 10;
    }
    return dfs(cnt, false, false);
}

int main()
{
    memset(d, -1, sizeof(d));
    d[0][0] = d[0][1] = 1;
    int T;
    scanf("%d", &T);
    while(T--){
        long long n;
        scanf("%I64d", &n);
      //  printf("%d\n", solve(0));
        printf("%I64d\n", n-solve(n)+1);
    }
}
hdu 2089

链接:http://hdu.hustoj.com/showproblem.php?pid=2089

类似上一个

dp[i][0]表示长度为len, 并且前一个数字不为6符合条件的数字个数

dp[i][1]表示长度为len, 并且前一个数字为6符合条件的数字个数

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int n, m, d[8][2], a[8];

int dfs(int len, bool state, bool f){ // state表示前一个以6开始, f表示是否能够直接返回;
    if(!len) return 1;
    if(f && d[len][state] != -1) return d[len][state];
    int fmax = f ? 9 : a[len-1];
    int res = 0;
    for(int i = 0; i <= fmax; ++i){
        if(i == 4) continue;
        if(state && i == 2) continue;
        res += dfs(len-1, i==6, f||(i!=fmax));
    }
    if(f){
        d[len][state] = res;
    }
    return res;
}

int solve(int x){
    int cnt = 0;
    while(x){
        a[cnt++] = x%10;
        x/=10;
    }
    return dfs(cnt, false, false);
}

int main()
{
    memset(d, -1, sizeof(d));
    while(~scanf("%d%d", &n, &m)){
        if(!n || !m) break;
        printf("%d\n", solve(m)-solve(n-1));
    }
}



:windy数

http://www.lydsy.com/JudgeOnline/problem.php?id=1026

题目描述:

windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,
在A和B之间,包括A和B,总共有多少个windy数?

题目分析:

    相邻两个指的是一个数字中任意两个相邻的数字只差都要大于2;

   同理按照之前的方法, dp[len][state]  len表示长度为len的数字中包含的windy数的数量;

                                         state表示前一个数字的值, 如果没有我这里用10表示;

ac代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int a[15], d[15][11];

int Pow(int x){
    int res = 1;
    for(int i = 0; i < x; ++i){
        res *= 10;
    }
    return res;
}
int getv(int x){
    int res = 0;
    for(int i = x-1; i >= 0; --i){
        res = res*10+a[i];
    }
    return res+1;
}

int dfs(int len, int state, bool f){
    if(!len) return 1;
    if(f && d[len][state] != -1) return d[len][state];
    int fmax = f ? 9 : a[len-1];
    int res = 0;
    for(int i = 0; i <= fmax; ++i){
        if(state == 10){
            if(i == 0) res += dfs(len-1, 10, f||(i!=fmax));
            else res += dfs(len-1, i, f||(i!=fmax));
        } else {
            if(abs(state-i) >= 2) res += dfs(len-1, i, f||(i!=fmax));
        }
    }
    if(f) d[len][state] = res;
    return res;
}

int solve(int x){
    int cnt = 0;
    while(x){
        a[cnt++] = x%10;
        x/=10;
    }
    return dfs(cnt, 10, false);
}

int main()
{
    int a, b;
    memset(d, -1, sizeof(d));
    while(scanf("%d%d", &a, &b) != EOF){
        printf("%d\n", solve(b)-solve(a-1));
    }
}


hdu 3652

地址:http://acm.hdu.edu.cn/showproblem.php?pid=3652

和上面类似的方法,最近写了很多数位dp的题,记忆化搜索做这些,都是比较简洁,清爽的

dp[len][prev][pn] ; //  表示 长度为len, %13的值为prev,  pn表示状态; pn==0表示前一个数字不为1, pn==1表示前一个数字为1, pn==2表示前面已经出现13;

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int a[12], d[12][13][3];

int dfs(int len, int prev, int pn, bool f){  // pn=1, pn=2表示已经出现13;
    if(!len) return (pn==2&&prev==0) ? 1 : 0;
    if(f && d[len][prev][pn] != -1) return d[len][prev][pn];
    int fmax = f ? 9 : a[len-1];
    int res = 0;
    for(int i = 0; i <= fmax; ++i){
        int nv = (prev*10+i)%13;
        if(pn==2) res += dfs(len-1, nv, 2, f||i!=fmax);
        else if(pn == 1 && i == 3){
            res += dfs(len-1, nv, 2, f||i!=fmax);
        } else {
            res += dfs(len-1, nv, i==1, f||i!=fmax);
        }
    }
    if(f) d[len][prev][pn] = res;
    return res;
}

int solve(int n){
    int cnt = 0;
    while(n){
        a[cnt++] = n%10;
        n/=10;
    }
    return dfs(cnt, 0, 0, false);
}

int main()
{
    int n;
    memset(d, -1, sizeof(d));
    while(~scanf("%d", &n)){
        printf("%d\n", solve(n));
    }
}

hdu 4734:

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 4605;

int a[12], d[12][maxn];   // d[len][state]表示长度为len不大于state的值有多少;

int dfs(int len, int state, bool f){  
    if(!len) return state >= 0;
    if(state < 0) return 0;
    if(f && d[len][state] != -1) return d[len][state];
    int fmax = f ? 9 : a[len-1];
    int res = 0;
    for(int i = 0; i <= fmax; ++i){
        res += dfs(len-1, state-i*(1<<(len-1)), f||(i!=fmax));
    }
    if(f) d[len][state] = res;
    return res;
}

int solve(int x, int v){
    int cnt = 0;
    while(x){
        a[cnt++] = x%10;
        x/=10;
    }
    //
    int res = 0;
    int b[11];
    int cnt2 = 0;
    while(v){
        b[cnt2++] = v%10;
        v/=10;
    }
    for(int i = 0; i < cnt2; ++i){
        res += b[i]*(1<<i);
    }
    return dfs(cnt, res, false);
}

int main()
{
    int T;
    scanf("%d", &T);
    memset(d, -1, sizeof(d));
    int cnt = 0;
    while(T--){
        int a, b;
        scanf("%d%d", &a, &b);
        printf("Case #%d: %d\n", ++cnt, solve(b, a));
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值