码迷,mamicode.com
首页 > 其他好文 > 详细

hnu Counting ones 统计1-n 二进制中1的个数

时间:2014-10-02 21:15:53      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   for   strong   

 

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\n",sum);
55     }
56     return 0;
57 }

 

hnu Counting ones 统计1-n 二进制中1的个数

标签:des   style   blog   color   io   os   ar   for   strong   

原文地址:http://www.cnblogs.com/tom987690183/p/4004360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!