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

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 rangeStart.. Finish

Sample Input

2 12

Sample Output

6

emmm思路总体差不多 就是先把输入转化为二进制,然后固定0的个数 用组合数做

后来没考虑到数不能超出finish卡了一下 再后来感觉有点想混了

原来好像直接就想 算0 的个数比n的位数的一半多就可以了

但是发现小于n的数里面 边界条件也会变化的

看了题解

思路:

先求位数小于n的roundnumber

尽管排列组合,结果肯定不会超过n的

然后算位数刚好等于n的roundnumber

先固定最高位 因为肯定是1 并且不能变动

往下数 后一位如果是0 那么也不能变动

如果是1 那么假设这一位是0 剩下的位数再对剩余的zero的个数进行排列组合

AC代码【poj用c++会挂】

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <cmath>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std; const int maxn = 35;
int start, finish;
int cntnuma, cntnumb, c[maxn][maxn];
int binarya[maxn], binaryb[maxn]; void Cmn()
{
c[0][0] = c[1][0] = c[1][1] = 1;
for(int i = 2; i < maxn; i++){
c[i][0] = 1;
for(int j = 1; j < i; j++){
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
}
c[i][i] = 1;
}
} void digtobinary(int n, int *binary)
{
binary[0] = 0;
while(n){
binary[++binary[0]] = n % 2;
n /= 2;
}
return;
} int solve(int n, int *binary)
{
//if(n <= 1) return 0;
//int len = digtobinary(n, binary);
int st;
digtobinary(n, binary); int len = binary[0];
int ans = 0;
//小于len的可以随便填肯定不会超过finish
for(int i = 1; i < len - 1; i++){
//if(i % 2) st = i / 2 + 1;
//else st = i / 2;
for(int j = i / 2 + 1; j <= i; j++){
ans += c[i][j];
}
}
int zero = 0;
//if(len % 2) st = len / 2 + 1;
//else st = len / 2;
for(int i = len - 1; i >= 1; i--){
if(!binary[i]){
zero++;
}
else{
for(int j = (len + 1) / 2 - zero - 1; j <= i - 1; j++){
ans += c[i - 1][j];
}
}
} return ans;
} int main()
{
Cmn();
while(scanf("%d%d",&start,&finish)!= EOF){
int ansa = solve(start, binarya);
int ansb = solve(finish + 1, binaryb);
cout<< ansb - ansa<< endl;
}
return 0;
}

poj3252Round Numbers【组合数】【数位dp】的更多相关文章

  1. BZOJ_3209_花神的数论题_组合数+数位DP

    BZOJ_3209_花神的数论题_组合数+数位DP Description 背景 众所周知,花神多年来凭借无边的神力狂虐各大 OJ.OI.CF.TC …… 当然也包括 CH 啦. 描述 话说花神这天又 ...

  2. Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)

    D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...

  3. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  4. Balanced Numbers (数位dp+三进制)

    SPOJ - BALNUM 题意: Balanced Numbers:数位上的偶数出现奇数次,数位上的奇数出现偶数次(比如2334, 2出现1次,4出现1次,3出现两次,所以2334是 Balance ...

  5. Codeforces #55D-Beautiful numbers (数位dp)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  6. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  7. poj3252 Round Numbers(数位dp)

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

  8. CodeForces 55D Beautiful numbers(数位dp)

    数位dp,三个状态,dp[i][j][k],i状态表示位数,j状态表示各个位上数的最小公倍数,k状态表示余数 其中j共有48种状态,最大的是2520,所以状态k最多有2520个状态. #include ...

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位dp)

    题目链接:https://ac.nowcoder.com/acm/contest/163/J 题目大意:给定一个数N,求区间[1,N]中满足可以整除它各个数位之和的数的个数.(1 ≤ N ≤ 1012 ...

  10. Codeforces Beta Round #51 D. Beautiful numbers(数位dp)

    题目链接:https://codeforces.com/contest/55/problem/D 题目大意:给你一段区间[l,r],要求这段区间中可以整除自己每一位(除0意外)上的数字的整数个数,例如 ...

随机推荐

  1. ASCII码与16进制的互相转换(表)

    所谓的ASCII和16进制都只是概念上的东西,在计算机中通通是二进制 转换应该是输出的转换,同样是一个数,在计算机内存中表示是一样的,只是输出不一样ASCII是针对字符的编码,几乎是键盘上的字符的编码 ...

  2. Mac下配置svn服务器

    Mac OS X 系统已经内置了svn支持,所以需要做的就只是配置,可以用svnadmin –vsersion(svnserve –version)查看.希望能对 您配置 SVN.进行开发版本控制有所 ...

  3. [AX]AX2012 R2 HR Jobs, Positions, Department和Workers

    部门.作业(Job的官方翻译)和位置(Position的官方翻译)是AX人力资源管理的基本组织元素,Job和Position在AX有的地方又称作工作和职位,其实这个翻译更为恰当. Job定义的是一个工 ...

  4. ios开发之--UIButton中imageView和titleLabel的位置调整

    在使用UIButton时,有时候需要调整按钮内部的imageView和titleLabel的位置和尺寸.在默认情况下,按钮内部的imageView和titleLabel的显示效果是图片在左文字在右,然 ...

  5. HttpClient(二)-- 模拟浏览器抓取网页

    一.设置请求头消息 User-Agent模拟浏览器 1.当使用第一节的代码 来 访问推酷的时候,会返回给我们如下信息: 网页内容:<!DOCTYPE html> <html> ...

  6. Unity中Surface Shader执行过程

  7. [XPath] XPath 与 lxml (五)XPath 实例

    本文继续沿用第三章的 XML 示例文档. 选取价格高于30的 price 节点 # 从父节点进行筛选 >>> root.xpath('//book[price>30]/pric ...

  8. mybais 之parameterType ="list"

    <!-- 根据货品编号获取商品价格和库存 --> <select id="getGoodsPriceAndStockByGoodsNo" resultMap=&q ...

  9. jinja语法

    <!--base.html--> <!DOCTYPE html> <html lang="en"> <head> <!--ht ...

  10. codeforces水题100道 第二十题 Codeforces Round #191 (Div. 2) A. Flipping Game (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/327/A题意:你现在有n张牌,这些派一面是0,另一面是1.编号从1到n,你需要翻转[i,j]区间的 ...