Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10844 | Accepted: 3987 |
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 ofN 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
Output
Sample Input
2 12
Sample Output
6
题意很简单,就是求一段区间内的数转化为二进制后0比1大的数有多少个。
这个用记忆化搜索很简单,代码简单点,只需要注意是否在高位是否有1即可,把这个作为一个判断标准。然后既然是有0和1的数量的话,所以我们必须要维护两个变量,分别为0和1的个数。再加上记忆搜索必备的上限和位置,五个参数可以解决这个问题。
具体解释见代码:
/************************************************************************* > File Name: Round_numbers.cpp > Author: Zhanghaoran > Mail: chilumanxi@xiyoulinux.org > Created Time: 2015年11月06日 星期五 18时14分47秒 ************************************************************************/ #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; long long dp[40][40][40]; //三维表示在到第i个位置的时候,0和1个数对应的数满足题目条件的有多少个 int num[12]; long long work(int pos, int num_0, int num_1, int fir_ok, int flag){ if(pos == 0){ //如果已经访问到最后一个位置的话,那么判断两种情况,第一种是还没有置1,也就是全是0,这种情况满足条件,返回1,另外一种情况是已经置1了,那么返回是否满足条件 if(!fir_ok){ return 1; } if(num_0 >= num_1) return 1; return 0; } if(fir_ok && !flag && dp[pos][num_0][num_1] != -1){ //记忆搜索,之前已经访问计算过这一状态的情况,无需计算直接返回 return dp[pos][num_0][num_1]; } long long ans = 0; int temp = flag ? num[pos] : 1; //置pos这一数位的值 for(int i = 0; i <= temp; i ++){ if(fir_ok){ //如果还没有置过1的话,那么分为两种情况 if(i == 0){ //继续置0,那么对下一位继续相同的讨论 ans += work(pos - 1, num_0 + 1, num_1, 1, flag && temp == i); } else{ //置1,高位开始有1,对下一数位进行有1的情况的讨论 ans += work(pos - 1, num_0, num_1 + 1, 1, flag && temp == i); } } else{ //如果已经有1的话,那么分别对置1和置0两种情况进行dfs就可以 if(i == 0){ ans += work(pos - 1, num_0, num_1, 0, flag && temp == i); } else{ ans += work(pos - 1, num_0, num_1 + 1, 1, flag && temp == i); } } } if(fir_ok && !flag) //记录搜索的结果 dp[pos][num_0][num_1] = ans; return ans; } long long calc(long long x){ int i = 1; while(x){ num[i ++] = x & 1; x >>= 1; } return work(i - 1, 0, 0, 0, 1); } int main(void){ long long a, b; scanf("%lld%lld", &a, &b); memset(dp, -1, sizeof(dp)); cout << calc(b) - calc(a - 1) << endl; }