Processing math: 100%

题解【POJ3252】Round Numbers

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的个数,其中1A,B2,000,000,000

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

因为1A,B2,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

记忆化搜索:

动态规划代码:

posted @   csxsi  阅读(225)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· SQL Server统计信息更新会被阻塞或引起会话阻塞吗?
阅读排行:
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 本地部署 DeepSeek:小白也能轻松搞定!
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
· 我们是如何解决abp身上的几个痛点
· 普通人也能轻松掌握的20个DeepSeek高频提示词(2025版)
点击右上角即可分享
微信分享提示