POJ-3252 Round Numbers

http://poj.org/problem?id=3252

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

 

题意:统计闭区间内满足某数的二进制形式0的个数>=1的个数这一条件的数字的个数。

思路:数位dp,状态为(pos位,0的个数比1的个数多多少)

0有可能比1少,故向上偏移32,[-32,32]~[0,64]。

需要前导0,因为0001101这样的数前面的0不算。

#include<cstring>
#include<iostream>
using namespace std;

int T,A,B,all;
int a[40],dp[40][70];

int dfs(int pos,int sta,bool lead,bool limit)
{
	if(pos==-1)return sta>=32;
	if(!limit&&!lead&&dp[pos][sta]!=-1)return dp[pos][sta];
	int up= (limit?a[pos]:1);
	int ans=0;
	for(int i=0;i<=up;i++)
	{
		if(lead&&i==0)ans+=dfs(pos-1,32,1,0);
		else ans+=dfs(pos-1,sta+(i==0?1:-1),0,limit&&i==a[pos]);
	}
	if(!limit&&!lead)dp[pos][sta]=ans;
	return ans;
}

int solve(int n)
{
	int pos=0;
	while(n)
	{
		a[pos++]=(n&1);
		n>>=1;
	}
	return dfs(pos-1,32,1,1);
}

int main()
{
//	freopen("input.in","r",stdin);
	memset(dp,-1,sizeof(dp));
	while(cin>>A>>B)cout<<solve(B)-solve(A-1)<<endl;
	return 0;
} 

 

全部评论

相关推荐

01-26 18:45
门头沟学院 Java
一天代码十万三:哥们实习再包一下吧,产出太笼统了,尽量体现业务
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
2024-12-30 18:02
程序员牛肉:1.可以标记一下自己的学校是985,有一些hr可能没想到你这个院校是985的。 2.简历所呈现出来的能力还是有点差的,苍穹外卖+黑马点评。这在java技术域里面也就是刚学三四个月的样子,大厂现在招人少,小厂又更加希望你能直接过来干活。就你简历上呈现出来的能力,确实是有点难找,肉眼可见的不懂技术。 第一个项目中:简单的使用redis也算是亮点嘛?使用jwt,threadlocal也算是亮点?你不就是调了几个包嘛?Nginx作为服务器也能写出来,这不是前端的活嘛? 第二个项目中:分布式锁+mq消息队列+Lua队列。真没啥好问的。属于面试官看一眼就阳痿的简历,没有任何想提问的欲望。 我给你建议是好好的挖一挖这个项目吧,其实苍穹外卖和黑马点评这两个项目很不错了,只不过是太烂大街了导致面试官没啥问的兴趣,所以不太推荐写简历上。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务