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

POJ 3252 Round Numbers 数位DP

时间:2014-08-02 12:42:53      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   for   div   amp   size   log   

注意只有高位放了1之后才能开始统计

#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;

int lim[256],len = 0;
LL f[40][40][40];

void getlim(LL N) {
    memset(lim,0,sizeof(lim));
    memset(f,-1,sizeof(f));
    len = 0;
    LL tmp = N;
    while(tmp) {
        lim[len++] = tmp & 1;
        tmp >>= 1;
    }
}

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

int main() {
    LL N,M;
    while(cin >> N >> M) {
        LL ret1,ret2;
        getlim(M); ret1 = dfs(len,0,0,1,0);
        getlim(N - 1); ret2 = dfs(len,0,0,1,0);
        cout << ret1 - ret2 << endl;
    }
    return 0;
}

  

POJ 3252 Round Numbers 数位DP,布布扣,bubuko.com

POJ 3252 Round Numbers 数位DP

标签:blog   os   io   for   div   amp   size   log   

原文地址:http://www.cnblogs.com/rolight/p/3886680.html

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