[POJ3252]Round Numbers

试题描述

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

输入

Line 1: Two space-separated integers, respectively Start and Finish.

输出

Line 1: A single integer that is the count of round numbers in the inclusive range Start..Finish

输入示例

 

输出示例


数据规模及约定

见“试题描述

题解

求出 [1, a) 和 [1, b] 区间内的 round number 的个数再向减,随便 dp 或组合数乱搞一下。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 32
int C[maxn][maxn], f[maxn][maxn], g[maxn][maxn], num[maxn]; int sum(int x) {
int cnt = 0;
while(x) {
num[++cnt] = x & 1;
x >>= 1;
}
int tmp = 0, m = (cnt & 1) ? (cnt >> 1) + 1 : (cnt >> 1), ans = 0;
bool fir = 0;
for(int i = cnt; i; i--) if(num[i]) {
if(!fir) for(int j = i - 1; j; j--) ans += f[j][(j&1)?(j>>1)+1:(j>>1)];
else ans += g[i-1][max(m-tmp-1,0)];
fir = 1;
}
else tmp++;
return ans;
} int main() {
int a = read(), b = read(); C[0][0] = f[0][0] = g[0][0] = 1;
for(int i = 1; i < maxn; i++) {
C[i][0] = f[i][0] = g[i][0] = 1;
for(int j = 1; j < maxn; j++) C[i][j] = C[i-1][j-1] + C[i-1][j], f[i][j] = C[i-1][j], g[i][j] = C[i][j];
}
for(int i = 0; i < maxn; i++)
for(int j = maxn - 2; j >= 0; j--) f[i][j] += f[i][j+1], g[i][j] += g[i][j+1]; printf("%d\n", sum(b + 1) - sum(a)); return 0;
}

[BZOJ1662][POJ3252]Round Numbers的更多相关文章

  1. poj3252 Round Numbers

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

  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 (数位dp)

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

  5. POJ3252 Round Numbers 【数位dp】

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

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

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

  7. poj3252 Round Numbers[数位DP]

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

  8. POJ3252 Round Numbers 题解 数位DP

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

  9. 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP

    [BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...

随机推荐

  1. Objective-C学习笔记之block

    //定义一个函数,传入block类型参数myBlock5 int fun(int (^myBlock5)(int a,int b)) { return myBlock5(10,20); } int ( ...

  2. UML活动图与流程图的区别

    http://blog.chinaunix.net/uid-11572501-id-3847592.html UML活动图与流程图的区别 (1).流程图着重描述处理过程,它的主要控制结构是顺序.分支和 ...

  3. 点亮第一个LED灯

    1.代码: #include <reg52.h> //<reg51.h>  包含52单片机寄存器库sbit led = P1^0;    //只有地址可以被8整除的 才可以用s ...

  4. centos 6.4 系统代理上网 设置

    前面讲了yum 代理设置上网的方法,现在设置一下系统代理上网, 网上很多都不管用,已亲测管用 [root@proxy ~]# cat .bash_profile #root目下添加代理上网,蓝色代码# ...

  5. 查询centos查看系统内核版本,系统版本,32位还是64位

    [root@centos01 ~]# lsb_release -a           #查看centos 版本为6.4LSB Version: :base-4.0-amd64:base-4.0-no ...

  6. nginx+php-fpm的socket配置小结

    关于socket的介绍本文不再赘述,生产环境中常用socket方式,本文简述其配置方式. #cd /app/local/php#切换到php安装目录下 #mkdir run #chmod 777 ./ ...

  7. JavaWeb学习笔记——开发动态WEB资源(七)bookapp

    该工程的功能是实现一个bookapp 1.开发注册页面,注册使用properties文件,存储在classpath跟路径 2.注册成功跳转到登录页面 3.输入用户名密码登录,登录成功跳转到book显示 ...

  8. JVM 关闭钩子

    1.功能 在jvm中添加关闭钩子(Runtime.getRuntime().addShutdownHook(shutdownHook);)后,当jvm关闭时会执行系统中已经设置的所有通过该方法添加的钩 ...

  9. ecshop 网站标题不更新或内容不更新

    网站标题不更新,这种情况一般出在网站搬家的过程中,把以前的所有配置文件一起都搬到新的服务器上了. 网站状态: 后台店铺标题已经修改,前台不显示,数据shop_config 的shop_title能更新 ...

  10. EF 索引

    public class CustomerMap : EntityTypeConfiguration<Customer> { public CustomerMap() { this.Pro ...