zoukankan      html  css  js  c++  java
  • POJ 3252 组合计数

    Round Numbers
    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 9149 Accepted: 3248
    Description

    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 range Start..Finish
    Sample Input

    2 12
    Sample Output

    6
    Source

    USACO 2006 November Silver


    /******************************
        author   : Grant Yuan
        time     : 2014/10/16 17:37
        algorithm: 组合计数
        source   : POJ 3252
        explain  : 按照G++交的,C++会跪
    *******************************/
    #include <iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    using namespace std;
    int a[35],len;
    int sum[40][40];
    void slove(int num)
    {
        a[0]=0;len=0;
        while(num){
            a[++len]=num&1;
            num=num>>1;
        }
    }
    void Cal_sum()
    {
        memset(sum,0,sizeof(sum));
        for(int i=0;i<=32;i++)
            for(int j=0;j<=i;j++)
        {
            if(j==0||i==j) sum[i][j]=1;
            else sum[i][j]=sum[i-1][j]+sum[i-1][j-1];
        }
    }
    int Get_sum1()
    {
        int l=len,res=0;
        for(int i=1;i<len-1;i++)
        {
            for(int j=i/2+1;j<=i;j++)
            {
                res+=sum[i][j];
            }
        }
        return res;
    }
    int Get_sum2()
    {
        int res=0,zero=0;
        for(int i=len-1;i>=1;i--)
        {
            if(a[i]){
                for(int j=(len+1)/2-zero-1;j<i;j++)
                    res+=sum[i-1][j];
            }
            else zero++;
        }
        return res;
    }
    int main()
    {
        int ans1,ans2,s,f;
        Cal_sum();
        while(~scanf("%d%d",&s,&f)){
            ans1=0;ans2=0;
            slove(s);
            ans1+=Get_sum1();ans1+=Get_sum2();
            slove(f+1);
            ans2+=Get_sum1();ans2+=Get_sum2();
            printf("%d %d
    ",ans1,ans2);
            printf("%d
    ",ans2-ans1);
        }
        return 0;
    }
    


  • 相关阅读:
    【Android Studio安装部署系列】十、Android studio打包发布apk安装包
    【Android Studio安装部署系列】九、Android Studio常用配置以及快捷键
    【Android Studio安装部署系列】八、Android Studio主题皮肤更换
    【Android Studio安装部署系列】七、真机运行项目
    【Android Studio安装部署系列】六、在模拟器上运行项目
    【Android Studio安装部署系列】五、新建你的第一个项目:HelloWorld
    【Android Studio安装部署系列】三、Android Studio项目目录结构
    【Android Studio安装部署系列】二、Android Studio开发环境搭建
    todo----maven profiles用法
    maven profiles多环境配置
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254412.html
Copyright © 2011-2022 走看看