zoukankan      html  css  js  c++  java
  • poj3252Round Numbers【组合数】【数位dp】


    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;
    }
    



  • 相关阅读:
    【本人译作推荐】Windows 8应用开发:C#和XAML卷(原名:Building Windows 8 Apps with C# and XAML)
    ODAC的安装以及Entity Framework for Oracle 基本配置
    ExtJs API 下载以及部署
    ArcGis(01)——地图切片以及发布底图服务
    ubuntu下安装Vmare Workstation,并安装mac补丁
    sencha app build 到 Capturing theme image不执行
    Ext Sencha Cmd 6 环境安装
    SqlServer CTE 递归查询 Oracle递归查询
    ibatis.net调用oracle存储过返回游标SYS_REFCURSOR结果集
    SpringMVC从入门到精通之第四章
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643444.html
Copyright © 2011-2022 走看看