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

POJ 3252 Round Numbers (数位DP)

时间:2015-01-22 15:26:49      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:acm   c语言   算法   编程   数位dp   

题目地址:POJ 3252

dp[i][j]代表第i位,1比0多j个的时候的个数,因为j会出现负数,所以可以都加上40再储存。然后就是简单的数位DP了。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=100000000;
const int INF=0x3f3f3f3f;
const double eqs=1e-8;
LL dp[50][100], c[100];
LL dfs(int cnt, int dif, int maxd, int zero)
{
        if(cnt==-1) return dif<=0;
        if(maxd&&zero&&dp[cnt][dif+40]!=-1) return dp[cnt][dif+40];
        int i, r=maxd?1:c[cnt], x;
        LL ans=0;
        for(i=0;i<=r;i++){
                if(i) x=dif+1;
                else if(zero) x=dif-1;
                else x=dif;
                ans+=dfs(cnt-1,x,maxd||i<r,zero||i);
        }
        if(maxd&&zero) dp[cnt][dif+40]=ans;
        return ans;
}
LL Cal(LL x)
{
        int i, cnt=0;
        while(x){
                c[cnt++]=x&1;
                x>>=1;
        }
        return dfs(cnt-1,0,0,0);
}
int main()
{
        LL a, b;
        memset(dp,-1,sizeof(dp));
        while(scanf("%I64d%I64d",&a,&b)!=EOF){
                printf("%I64d\n",Cal(b)-Cal(a-1));
        }
        return 0;
}


POJ 3252 Round Numbers (数位DP)

标签:acm   c语言   算法   编程   数位dp   

原文地址:http://blog.csdn.net/scf0920/article/details/43019749

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