poj3252 Round Numbers (数位dp)

Description

The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets to be milked first. They can't even flip a coin because it's so hard to toss using hooves.

They have thus resorted to "round number" matching. The first cow picks an integer less than two billion. The second cow does the same. If the numbers are both "round numbers", the first cow wins,
otherwise the second cow wins.

A positive integer N is said to be a "round number" if the binary representation of N has as many or more zeroes than it has ones. For example, the integer 9, when written in binary form, is 1001. 1001 has two zeroes and two ones; thus, 9 is a round number. The integer 26 is 11010 in binary; since it has two zeroes and three ones, it is not a round number.

Obviously, it takes cows a while to convert numbers to binary, so the winner takes a while to determine. Bessie wants to cheat and thinks she can do that if she knows how many "round numbers" are in a given range.

Help her by writing a program that tells how many round numbers appear in the inclusive range given by the input (1 ≤ Start < Finish ≤ 2,000,000,000).

Input

Line 1: Two space-separated integers, respectively  Start and  Finish.

Output

Line 1: A single integer that is the count of round numbers in the inclusive range  Start.. Finish

Sample Input

2 12

Sample Output

6

题意:一个数位round数,要满足这个数化成二进制后0的个数大于等于1的个数。

思路:先预处理出dp[i][j]表示前i位有j个0的方案数,然后从高位到低位数位dp就行了。


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 805
ll dp[40][40];
void init()
{
    int i,j;
    memset(dp,0,sizeof(dp));
    dp[1][1]=1;dp[1][0]=1;
    for(i=1;i<=32;i++){
        for(j=0;j<=i;j++){
            dp[i+1][j]+=dp[i][j];
            dp[i+1][j+1]+=dp[i][j];
        }
    }
}

ll solve(int x)
{
    int i,j,t=x;
    int wei[40],len=0;
    while(t){
        wei[++len]=t%2;
        t/=2;
    }

    ll sum=0;
    int num0=0,num1=0;
    for(i=len-1;i>=1;i--){   //这里先把第len位变为0,然后一次枚举最高的位数在第i位
        for(j=0;j<=i-1;j++){
            if(j>=i-j)sum+=dp[i-1][j];
        }

    }

    num1=1;
    for(i=len-1;i>=1;i--){ //这里是在第len位为1的情况下进行dp
        if(wei[i]==1){
            if(i==1){
                if(num0+1>=num1)sum++;
            }
            else{
                for(j=0;j<=i-1;j++){
                    if(j+num0+1>=num1+i-1-j ){
                        sum+=dp[i-1][j];
                    }
                }
            }
            num1++;
        }
        else num0++;
    }
    return sum;
}
int main()
{
    int n,m,i,j;
    init();
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        printf("%lld\n",solve(n+1)-solve(m));
    }
    return 0;
}

这题也可以用记忆化搜索做,搜索比直接dp要好些,而且用途广,写的时候在dfs中加一维zero,表示当前这一位是不是任然是前导0.用dp[pos][num0][num1]表示前pos位0的个数为num0,1的个数为num1方案数。


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
int dp[35][35][35];
int wei[35];
int dfs(int pos,int num0,int num1,int lim,int zero)
{
    int i,j;
    if(pos==0){        
        if(zero==0){    //这里不判断zero==0也可以,判不判断的区别在于是不是把0算上,判断就不把0算上了
            if(num0>=num1)return 1;
            else return 0;
        }
        return 0;
        /*
        if(num0>=num1)return 1;
        return 0;
        */
    }
    if(lim==0 && dp[pos][num0][num1]!=-1){
        return dp[pos][num0][num1];
    }

    int ed=lim?wei[pos]:1;
    int ans=0,nu0,nu1;
    for(i=0;i<=ed;i++){
        if(zero && i==0){
            nu0=nu1=0;
        }
        else{
            if(i==0){
                nu0=num0+1;
                nu1=num1;
            }
            else{
                nu0=num0;
                nu1=num1+1;
            }
        }
        ans+=dfs(pos-1,nu0,nu1,lim&&i==ed,zero&&i==0 );
    }
    if(lim==0){
        dp[pos][num0][num1]=ans;
    }
    return ans;
}


int solve(int x)
{
    int i,j,tot=0;
    while(x){
        wei[++tot]=x%2;
        x/=2;
    }
    return dfs(tot,0,0,1,1);
}



int main()
{
    int n,m,i,j;
    memset(dp,-1,sizeof(dp));
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        printf("%d\n",solve(n)-solve(m-1));
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给出一个$n\times m$的矩阵,每个位置上有一个非负整数,代表这个位置的海拔高度。一开始时,有一个人站在其中一个位置上。这个人可以向上、下、左、右四个方向移动,但是只能移动到海拔高度比当前位置低或者相等的位置上。一次移动只能移动一个单位长度。定义一个位置为“山顶”,当且仅当从这个位置开始移动,可以一直走到海拔高度比它低的位置上。请问,这个矩阵中最多有多少个“山顶”? 输入格式 第一行两个整数,分别表示$n$和$m$。 接下来$n$行,每行$m$个整数,表示整个矩阵。 输出格式 输出一个整数,表示最多有多少个“山顶”。 样例输入 4 4 3 2 1 4 2 3 4 3 5 6 7 8 4 5 6 7 样例输出 5 算法1 (递归dp) $O(nm)$ 对于这道题,我们可以使用递归DP来解决,用$f(i,j)$表示以$(i,j)$为起点的路径最大长度,那么最后的答案就是所有$f(i,j)$中的最大值。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码 算法2 (动态规划) $O(nm)$ 动态规划的思路与递归DP类似,只不过转移方程和实现方式有所不同。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值