zoukankan      html  css  js  c++  java
  • poj-3252 Round Numbers

    Round Numbers
     

    Time Limit: 2000MS Memory Limit: 65536K

    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).

    奶牛没有手指和拇指(?),所以他们不能通过剪刀石头布的方式来做决策,比如决定谁先被喂奶。他们甚至不能掷硬币,因为用蹄子扔硬币很困难。

    所以他们要求助于“round number”匹配的方法。第一头奶牛选择一个小于2,000,000,000的数,第二头奶牛做同样的事。如果两个数都是“round numbers”,第一头奶牛获胜,否则第二头奶牛获胜。

    当一个正数的二进制表示中0的个数大于等于1的个数时,它是一个"round number".

    很明显,奶牛们要花一段时间把数字们转化为二进制,所以胜利者过一段时间才会被选出。 Bessie想作弊,她觉得如果她知道一个给定的范围的“round numbers”的个数,她就可以做到。

    请你帮助她写一个程序来告诉她一个输入的范围内有多少个“round numbers”

    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
    一行:一个整数代表起始到终止范围的round numbers的个数
     

    Sample Input

    2 12

    Sample Output

    6

    Solution

    数位dp,dp[i][j]表示考虑到第i位(二进制位),剩下的位中0的个数与1的个数的差为j的数的个数。
    j可能为负,需要统一加上一个数(32吧)
    这道题需要考虑前导0,因为如果前面都是0的话,后面的0就不能算在里面
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int dp[37][73],a[37];
    inline int read()
    {
    	register int ans=0,f=1;char ch=getchar();
    	while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    	while(isdigit(ch)) {ans=ans*10+ch-'0';ch=getchar();}
    	return ans*f;
    }
    int dfs(int wi,int cha,bool lim,bool fir)      //枚举到哪一位,sum0-sum1,限制,是否有前导0 
    {
    	if(wi<1)
          return cha==32;
    	if(!lim&&!fir&&dp[wi][cha]>-1)
    	  return dp[wi][cha];
    	int o=lim? a[wi]:1;
    	int ans=0;
    	if(fir)
    	{
    		if(wi!=1)
    		  ans+=dfs(wi-1,cha,lim&&a[wi]==0,fir);
    		else
    		  ans+=dfs(wi-1,cha-1,lim&&a[wi]==0,fir);
    	}
    	else
    	  ans+=dfs(wi-1,cha-1,lim&&a[wi]==0,fir);
    	if(o>=1)
    	  ans+=dfs(wi-1,cha+1,lim&&a[wi]==1,0);         //a[wi]==1/0
    	if(!lim&&!fir)
    	  dp[wi][cha]=ans;
    	return ans; 
    } 
    int sol(int x)
    {
    	int w=0,ans=0;
    	while(x)
    	{
    		a[++w]=x&1;
    		x/=2;
    	}
    	for(int i=0;i<=w;i++)
    	  ans+=dfs(w,i+32,1,1);
    	return ans;
    }
    int main()
    {
    	int s,f;
    	s=read();f=read();
    	for(int i=0;i<37;++i)
    	  for(int j=0;j<73;++j)
    	    dp[i][j]=-1;
    	int a=sol(f);
    	int b=sol(s-1);
    	printf("%d",a-b);
    	return 0;
    }
     
  • 相关阅读:
    网口 光口 电口都是接什么的?
    webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口
    webService学习之路(二):springMVC集成CXF快速发布webService
    全国计算机技术与软件专业技术资格(水平)考试网上报名平台
    Axis2创建WebService实例
    使用axis2构建webservice
    JavaSE----API之集合(Collection、List及其子类、Set及其子类、JDK1.5新特性)
    SGU
    REST技术第四步 多个參数注解问题
    深入源代码解析Android中的Handler,Message,MessageQueue,Looper
  • 原文地址:https://www.cnblogs.com/charlotte-o/p/7612412.html
Copyright © 2011-2022 走看看