POJ-3252 Round Numbers

本博客详细介绍了奶牛如何通过选择小于两亿的圆整数来做出决策的过程。通过转换为二进制形式并分析其中的零与一的数量关系,来确定哪只奶牛赢得决策。文章提供了实现这一过程的代码片段,包括状态转移矩阵的使用,并解决了在计算过程中遇到的问题,如下标溢出和特殊条件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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
————————————————————集训18.3的分割线————————————————————
思路:从十进制填数字变成了二进制填数字。难度稍微提升了。状态:
1. 长度
2. 0的个数和1的个数之差
3. 1是否出现过
在之前的填数字中,最高位可以是0,因为我们认为所有数字的位数都是最大数的位数,然后从最高位向下填。但是现在不行了,在第一个1前面的0都是无效的。只能在第一个1出现之后再统计。
另外要注意防止下标出现负值(1比0多)
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
int dp[35][70][2];
int num[35], cnt;

int dfs(int len, int diff, bool one, bool bnd)
{
	if(len == 0) {
		if(diff >= 35) {
			return 1;
		}
		return 0;
	}
	if(!bnd && dp[len][diff][one] != -1)
		return dp[len][diff][one];
	int lim = (bnd ? num[len] : 1);
	int ret = 0;
	for(int i = 0; i <= lim; i++) {
		int nd;
		if(one||i)//这里WA了一次,因为用了one(前一个数是否出现1)来判断
			nd = (i == 0 ? 1 : -1);
		else
			nd = 0;
		ret += dfs(len-1, diff + nd, one || i, bnd&&i==lim);
	}
	if(!bnd) dp[len][diff][one] = ret;
	return ret;
}

int Solve(int x)
{
	cnt = 0;
	while(x) {
		num[++cnt] = x&1;
		x >>= 1;
	}
	return dfs(cnt, 35, false, true);//以35作为初始值,防止下标为负
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	int l, r;
	memset(dp, -1, sizeof(dp));
	while(~scanf("%d%d", &l, &r)) {
		printf("%d\n", Solve(r) - Solve(l-1));
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值