zoukankan      html  css  js  c++  java
  • hnu Counting ones 统计1-n 二进制中1的个数

    Counting ones
    Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
    Total submit users: 18, Accepted users: 16
    Problem 13030 : No special judgement
    Problem description
    Carl is right now the happiest child in the world: he has just learned this morning what the bi- nary system is. He learned, for instance, that the binary representation of a positive integer k is a string anan-1 ... a1a0 where each ai is a binary digit 0 or 1, starting with an = 1, and such that k = Σai × 2i(i from 0 to n). It is really nice to see him turning decimal numbers into binary numbers, and then adding and even multiplying them.
    Caesar is Carl's older brother, and he just can't stand to see his little brother so happy. So he has prepared a challenge: "Look Carl, I have an easy question for you: I will give you two integers A and B, and you have to tell me how many 1's there are in the binary representation of all the integers from A to B, inclusive. Get ready". Carl agreed to the challenge. After a few minutes, he came back with a list of the binary representation of all the integers from 1 to 100. "Caesar, I'm ready". Caesar smiled and said: "Well, let me see, I choose A = 1015 and B = 1016. Your list will not be useful".
    Carl hates loosing to his brother so he needs a better solution fast. Can you help him?


    Input
    A single line that contains two integers A and B (1 ≤ A ≤ B ≤ 1016).

    Output
    Output a line with an integer representing the total number of digits 1 in the binary representation of all the integers from A to B, inclusive.

    Sample Input
    Sample input 1
    1000000000000000 10000000000000000
    
    Sample input 2
    2 12
    
    Sample input 3
    9007199254740992 9007199254740992
    
    Sample Output
    Sample output 1
    239502115812196372
    
    Sample output 2
    21
    
    Sample output 3
    1
    Problem Source
    ICPC Latin American Regional 2013

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<cstring>
     4 #include<cstdlib>
     5 using namespace std;
     6 typedef long long LL;
     7 
     8 LL ans[65];
     9 LL fac[65];
    10 LL a[66],alen;
    11 void init()
    12 {
    13     fac[1]=ans[1]=1;
    14     fac[2]=ans[2]=2;
    15     for(int i=3;i<=60;i++)
    16     {
    17         fac[i] = 2*fac[i-1]+ans[i-1]-1;
    18         ans[i]=ans[i-1]*2;
    19     }
    20 }
    21 void get(LL n)
    22 {
    23     alen = 0;
    24     while(n)
    25     {
    26         a[++alen]=(n&1);
    27         n=n>>1;
    28     }
    29 }
    30 LL solve()
    31 {
    32     LL sum =0;
    33     LL k = 0;
    34     for(int i=alen;i>=1;i--)
    35     {
    36         if(a[i]==1)
    37         {
    38             sum = sum+fac[i]+k*ans[i];
    39             k++;
    40         }
    41     }
    42     return sum;
    43 }
    44 int main()
    45 {
    46     LL n,m;
    47     init();
    48     while(scanf("%I64d%I64d",&n,&m)>0)
    49     {
    50         get(m);
    51         LL sum = solve();
    52         get(n-1);
    53         sum = sum-solve();
    54         printf("%I64d
    ",sum);
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    分享25个高质量的移动设备wordpress主题(Mobile theme)
    一个强大专业的响应式jQuery幻灯效果插件:Royal Slider
    超过30个超棒的漂亮iphone 4s墙纸
    帮助你自动生成已经过去的时间的jQuery插件 Smart Time Ago
    分享20个响应式web设计的必备jQuery插件
    2012年度最新免费web开发设计资源荟萃
    myEclipse 下载
    extjs 配置与表格使用
    mysql 4.0.26 安装流程
    spring+ehcache 实现原理
  • 原文地址:https://www.cnblogs.com/tom987690183/p/4004360.html
Copyright © 2011-2022 走看看