首页 > 其他 > 详细

URAL 1057 Amount of Degrees 数位DP

时间:2014-08-05 15:45:29      阅读:355      评论:0      收藏:0      [点我收藏+]

水题,随便统计一下就好

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef long long LL;
const int maxn = 100;
int lim[maxn],B,K,len;
int f[maxn][maxn];

void getlim(int num) {
    len = 0; memset(lim,0,sizeof(lim));
    while(num) {
        lim[len++] = num % B;
        num /= B;
    }
}

int dfs(int now,int cnt,int bound) {
    if(now == 0) {
        return cnt == K;
    }
    int ret = 0,m = bound ? lim[now - 1] : 1;
    int &note = f[now][cnt];
    if(!bound && note != -1) return note;
    for(int i = 0;i <= m;i++) {
        if(i == 0) ret += dfs(now - 1,cnt,bound && i == m);
        else if(i == 1) {
            if(cnt == K) continue;
            ret += dfs(now - 1,cnt + 1,bound && i == m);
        }
    }
    if(!bound) note = ret;
    return ret;
}

int solve(int num) {
    getlim(num);
    memset(f,-1,sizeof(f));
    return dfs(len,0,1);
}


int main() {
    int X,Y;
    cin >> X >> Y >> K >> B;
    cout << solve(Y) - solve(X - 1) << endl;
    return 0;
}

  

URAL 1057 Amount of Degrees 数位DP,布布扣,bubuko.com

URAL 1057 Amount of Degrees 数位DP

原文:http://www.cnblogs.com/rolight/p/3892330.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!