• [BZOJ1662][POJ3252]Round Numbers


    [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

    输入示例

    2 12

    输出示例

    6

    数据规模及约定

    见“试题描述

    题解

    求出 [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
    ", sum(b + 1) - sum(a));
    	
    	return 0;
    }
    
  • 相关阅读:
    Python-05知识-01Python优缺点
    Python-02进阶-06代码优化工具
    Python-02进阶-04多进程多线程
    Python-02进阶-03生成器
    Python-02进阶-02装饰器
    Python-01基础-13功能模块
    Python-01基础-12常用命令
    Python-01基础-11基础知识
    console.dir有很多浏览器,系统的兼容性问题,不要随便用!
    微信支付-签名错误
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6048289.html
Copyright © 2020-2023  润新知