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

Source

USACO 2006 November Silver

Solution

简化版题意:求出一个区间[a,b]中有多少个“Round Number”,一个数是“Round Number”当且仅当它的二进制表示法中0的个数>=1的个数,其中\(1 \leqslant A, B \leqslant 2,000,000,000\)。

我们可以根据题意先写一个简单的暴力:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; inline int gi()
{
int f = 1, x = 0;
char c = getchar(); while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
} while (c >= '0' && c <= '9')
{
x = x * 10 + c - '0';
c = getchar();
} return f * x;
}
//以上不解释 inline bool pd(int x)//判断一个数是不是"Round Number"
{
int y = 0, z = 0;//y是存二进制数中1的个数,z是存0的个数 while (x > 0)
{
if (x & 1)//如果这一位是0
{
++y;//y就加1
}
else
{
++z;//否则z就加1
} x = x >> 1;//x除以2
} return z >= y;//这个语句的意思是:如果z>=y,就返回true,否则返回false。
} int a, b, sum;//a、b是题目中的意思,sum是答案 int main()
{
a = gi(), b = gi();//输入a、b for (int i = a; i <= b; i++)//从a到b枚举
{
if (pd(i))//如果i是“Round Number"
{
++sum;//sum就加一
}
} printf("%d", sum);//最后输出sum return 0;//结束
}

因为\(1 \leqslant A, B \leqslant 2,000,000,000\),很明显,以上代码小数据能AC,但是大数据会TLE。

因此,我们要使用一个更加高效的算法——数位DP。

什么是数位DP呢?可以参考这篇文章:http://www.cnblogs.com/real-l/p/8540124.html

回到这一题:

我们设Rn[n,m]表示区间[n,m]中Round Number的个数,我们利用前缀和,就有:

Rn[a,b] = Rn[0, b] - Rn[0, a - 1]

记忆化搜索思路:

设dp[a][n0][n1]表示从高往低到达第a位时含有n0个0和n1个1在后面任意填时该状态下的总个数。

注意加一个变量flag来判断是否含有前导0。

直接DP思路:

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

Code

记忆化搜索:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; inline int gi()
{
int f = 1, x = 0;
char c = getchar(); while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
} while (c >= '0' && c <= '9')
{
x = x * 10 + c - '0';
c = getchar();
} return f * x;
} int dp[35][35][35], wei[35]; int dfs(int a, int n0, int n1, int ddd, int flag)
{
if (a == 0)
{
if (flag == 0)//这里不判断flag==0也可以,判不判断的区别在于是不是把0算上,判断就不把0算上了
{
if (n0 >= n1)
{
return 1;
}
else
{
return 0;
}
} return 0;
} if (ddd == 0 && dp[a][n0][n1] != -1)
{
return dp[a][n0][n1];
} int ed = ddd ? wei[a] : 1, ans = 0, nu0, nu1; for (int i = 0; i <= ed; i++)
{
if (flag && i == 0)
{
nu0 = nu1 = 0;
}
else
{
if (i == 0)
{
nu0 = n0 + 1, nu1 = n1;
}
else
{
nu0 = n0, nu1 = n1 + 1;
}
} ans = ans + dfs(a - 1, nu0, nu1, ddd && i == ed, flag && i == 0);
} if (ddd == 0)
{
dp[a][n0][n1] = ans;
} return ans;
} int solve(int x)
{
int tot = 0; while (x)
{
wei[++tot] = x & 1; x = x >> 1;
} return dfs(tot, 0, 0, 1, 1);
} int a, b; int main()
{
memset(dp, -1, sizeof(dp)); a = gi(), b = gi(); printf("%d\n", solve(b) - solve(a - 1)); return 0;
}

动态规划代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; inline int gi()
{
int f = 1, x = 0;
char c = getchar(); while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
} while (c >= '0' && c <= '9')
{
x = x * 10 + c - '0';
c = getchar();
} return f * x;
} int dp[40][40]; inline int getans(int x)
{
int t = x, wei[40], len = 0, sum = 0, n0 = 0, n1 = 0;; while (t)
{
wei[++len] = t & 1; t = t >> 1;
} for (int i = len - 1; i >= 1; i--)//这里先把第len位变为0,然后一次枚举最高的位数在第i位
{
for (int j = 0; j <= i - 1; j++)
{
if (j >= i - j)
{
sum = sum + dp[i - 1][j];
}
}
} n1 = 1; for (int i = len - 1; i >= 1; i--)//这里是在第len位为1的情况下进行dp
{
if (wei[i] == 1)
{
if (i == 1)
{
if (n0 + 1 >= n1)
{
++sum;
}
}
else
{
for (int j = 0; j <= i - 1; j++)
{
if (j + n0 + 1 >= n1 + i - 1 - j)
{
sum = sum + dp[i - 1][j];
}
}
} ++n1;
}
else
{
++n0;
}
} return sum;
} int a, b; int main()
{
a = gi(), b = gi(); memset(dp, 0, sizeof(dp)); dp[1][1] = 1, dp[1][0] = 1; for (int i = 1; i <= 32; i++)
{
for (int j = 0; j <= i; j++)
{
dp[i + 1][j] = dp[i + 1][j] + dp[i][j], dp[i + 1][j + 1] = dp[i + 1][j + 1] + dp[i][j];
}
} printf("%d", getans(b + 1) - getans(a)); return 0;
}

题解【POJ3252】Round Numbers的更多相关文章

  1. [BZOJ1662][POJ3252]Round Numbers

    [POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...

  2. POJ3252 Round Numbers —— 数位DP

    题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Su ...

  3. poj3252 Round Numbers(数位dp)

    题目传送门 Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16439   Accepted: 6 ...

  4. poj3252 Round Numbers

    Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7625   Accepted: 2625 Des ...

  5. poj3252 Round Numbers (数位dp)

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  6. POJ3252 Round Numbers 题解 数位DP

    题目大意: 求区间 \([x,y]\) 范围内有多少数的二进制表示中的'0'的个数 \(\ge\) '1'的个数. 解题思路: 使用 数位DP 解决这个问题. 我们设状态 f[pos][num0][n ...

  7. POJ3252 Round Numbers 【数位dp】

    题目链接 POJ3252 题解 为什么每次写出数位dp都如此兴奋? 因为数位dp太苟了 因为我太弱了 设\(f[i][0|1][cnt1][cnt0]\)表示到二进制第\(i\)位,之前是否达到上界, ...

  8. POJ3252 Round Numbers(不重复全排列)

    题目问区间有多少个数字的二进制0的个数大于等于1的个数. 用数学方法求出0到n区间的合法个数,然后用类似数位DP的统计思想. 我大概是这么求的,确定前缀的0和1,然后后面就是若干个0和若干个1的不重复 ...

  9. poj3252 Round Numbers[数位DP]

    地址 拆成2进制位做dp记搜就行了,带一下前导0,将0和1的个数带到状态里面,每种0和1的个数讨论一下,累加即可. WA记录:line29. #include<iostream> #inc ...

随机推荐

  1. 数据结构与算法之比较排序【Java】

    比较排序与非比较排序的对比 常见的快速排序.归并排序.堆排序.冒泡排序等属于比较排序.在排序的最终结果里,元素之间的次序依赖于它们之间的比较.每个数都必须和其他数进行比较,才能确定自己的位置.在冒泡排 ...

  2. pythonCSV内置模块应用

    一.Python内置模块CSV CSV,即逗号分隔值(也称字符分隔值,因为分隔符可以不是逗号),是一种常用的文本格式,用以存储表格数据,包括数字或者字符.如下图所示: CSV类似于Excel格式 很多 ...

  3. 我的翻译--一个针对TP-Link调试协议(TDDP)漏洞挖掘的故事

    前言 我写这篇文章原本是为了简化WiFi渗透测试研究工作.我们想使用去年由Core Security发布的WIWO,它可以在计算机网络接口和WiFi路由器之间建立一个透明的通道. 研究的第一步,就是选 ...

  4. HashMap的一些学习

    1.equals和==的对比==用于比较引用和比较基本数据类型时具有不同的功能:A:比较基本数据类型,如果两个值相同,则结果为true而在比较引用时,如果引用指向内存中的同一对象,结果为true; e ...

  5. Dubbo之服务注册

    在上一篇文章Dubbo之服务暴露分析中介绍了当远程暴露时,如果有注册中心,需要在服务暴露后再将服务注册到注册中心.该篇将介绍该功能的有关步骤. 注册的起点 在RegistryProtocol.expo ...

  6. python 方法和函数

    代码 def func(): pass class Foo(object): def func(self): pass # 执行方式一 # obj = Foo() # obj.func() # 方法 ...

  7. 数据预处理 | python 第三方库 imblearn 处理样本分布不均衡问题

    说明:目前 只记录了 过采样 和 欠采样 的代码部分 1 样本分布不均衡描述: 主要出现在与分类相关的建模问题上,不均衡指的是不同类别的样本量差异非常大. 样本量差距过大会影响到建模结果 2 出现的场 ...

  8. SQL Server 疑难杂症--转换科学计数法的数值字符串为decimal类型

    今天在操作数据库时,需要将字符串转换成Decimal类型.代码如下: select cast('0.12' as decimal(18,2)); select convert(decimal(18,2 ...

  9. 创建登录WEB UI页面的Business role.

    1: Define business role 2:  business role 中可以指定 config key,  该config key可以用于UI configurationo determ ...

  10. phpstorm实现分屏展示代码

    第一种 选择你要分屏的页面 [Window]—>[Editor Tabs]—>[Split Vertically]or[Split Horizontally]  第二种 把鼠标箭头放到你想 ...