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. [原]unity3d调用android webView

    1.配置dialog xml文件: <resources>    <style name="dialogStyleWindow" parent="@an ...

  2. 安装office2010出现了错误,提示要安装MSXML6.10.1129.0解决方法

    将下面的内容复制到记事本中,然后将记事本存成.reg文件 Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\TypeLib\{F5078F1 ...

  3. 在netbeans下使用调试PHP的插件XdeBug

    本人的开发环境: wamp最新官网wampserver2.2d-x32版. 下载点:http://nchc.dl.sourceforge.net/project/wampserver/WampServ ...

  4. python线程池(threadpool)

    一.安装 pip install threadpool 二.使用介绍 (1)引入threadpool模块 (2)定义线程函数 (3)创建线程 池threadpool.ThreadPool() (4)创 ...

  5. Nginx 默认虚拟主机

    一台服务器可以配置多个网站,每个网站都称为一个虚拟主机,默认的虚拟主机可以通过 default_server 来指定:: [root@localhost ~]$ cat /usr/local/ngin ...

  6. You have tried to change the API from what has been previously approved.

    需要修改frameworks/base/下的代码,请注意 :如果修改了Android原有API的 ,需要update frameworks/base/api/current.txt.否则编译被中断并出 ...

  7. 删除sql注入

    ), );--过滤字符串 字符串及之后的数据将被替换为空 set @FilterStr='</title><style>.alx2{'; set @curTable='user ...

  8. 【小程序+ thinkphp5】 获取微信运动数据

    配置.请参看上篇文章.这里直接上代码 PHP 代码: //获取微信运动数据: public function test(){ $code = input("code"); $sig ...

  9. ipcs命令以及oracle内存段

    今天是2014-01-06,在没过春节之前重新复习一下2013年学习的内容,关于oracle内存段在我之前写的blog中有详细操作.在此记录一下ipcs命令的用法. http://blog.csdn. ...

  10. 【大数据系列】hadoop上传文件报错_COPYING_ could only be replicated to 0 nodes

    使用hadoop上传文件 hdfs dfs -put  XXX 17/12/08 17:00:39 WARN hdfs.DFSClient: DataStreamer Exception org.ap ...