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. 字符串中包含汉字和\u,显示出汉字来

    针对py2,py3不需要这样.#coding=utf8import sysreload(sys)sys.setdefaultencoding('utf8') strx2='你好\u4e2d\u56fd ...

  2. [原]unity3D bug记录

    1.u3d 发出的xcode工程 崩溃出现这种信息 Display::DisplayLinkItem::dispatch() 到AppController.mm 修改成这样 - (void) Repa ...

  3. numpy, pandas, matplotlib等常用库的学习手册

    pandas介绍: 待续 参考资料: 中文:https://www.cnblogs.com/skying555/p/5914391.html 英文:http://www.datadependence. ...

  4. 用JAVA发送一个XML格式的HTTP请求

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...

  5. Memcache未授权访问漏洞

    Memcached 分布式缓存系统,默认的 11211 端口不需要密码即可访问,黑客直接访问即可获取数据库中所有信息,造成严重的信息泄露. 0X00 Memcache安装 1. 下载Mencache的 ...

  6. Win10 如何安装 Ubuntu

    在 Microsoft Store 中安装 Ubuntu ( 如下图1 ) 把开发者模式打开 ( 如下图2 ) 把 WSL ( Windows下的Linux子系统 ) 打开并重启电脑 ( 如下图3 )

  7. 国内CDN加速现状

    什么是CDN CDN的全称是Content Delivery Network,即内容分发网络.是位于网络层与应用层之间的网络应用,其目的是通过在现有的Internet中增加一层新的网络架构,将网站的内 ...

  8. Python调用7zip命令实现文件批量解压

    Python调用7zip命令实现文件批量解压 1.输入压缩文件所在的路径 2.可以在代码中修改解压到的文件路径和所需要解压的类型,列入,解压文件夹下面所有的mp4格式的文件 3.cmd 指的就是Pyt ...

  9. 从PC端(Ubuntu)挂载nfs网络文件系统ARM9+Linux板子上

    挂载概念 在windows操作系统中,挂载通常是指给磁盘分区(包括被虚拟出来的磁盘分区)分配一个盘符. 在linux操作系统中,它指将一个设备(通常是存储设备)挂接到一个已存在的目录上.(这个目录可以 ...

  10. 在taro中跳转页面的时候执行两遍componentDidMount周期的原因和解决方法

    在做taro跳转的时候,发现在跳转后的页面会走两遍componentDidMount周期,查看了github上的issues,发现是跳转路由带参为中文引起的,只要把中文参数进行urlencode解决 ...