Count The Carries

本文探讨了一个有趣的算法问题:计算从A到B之间所有整数的二进制表示进行加法运算时产生的进位次数。通过观察规律并使用特定算法,实现了高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
One day, Implus gets interested in binary addition and binary carry. He will transfer all decimal digits to binary digits to make the addition. Not as clever as Gauss, to make the addition from a to b, he will add them one by one from a to b in order. For example, from 1 to 3 (decimal digit), he will firstly calculate 01 (1)+10 (2), get 11,then calculate 11+11 (3),lastly 110 (binary digit), we can find that in the total process, only 2 binary carries happen. He wants to find out that quickly. Given a and b in decimal, we transfer into binary digits and use Implus's addition algorithm, how many carries are there?
 


Input
Two integers a, b(0<=a<=b<1000000000), about 100000 cases, end with EOF.
 


Output
One answer per line.
 


Sample Input
1 2
1 3
1 4
1 6
 


Sample Output
0  
2
3

6

题意:计算从A到B二进制之和进位的个数

找规律题目

给你a和b,让你计算从a到b之间的数的二进制数之和进位的总次数。
将每一个数拆成二进制数,那么第一位数1的总数就是从a到b第一位是1的数量之和,那么第一位进位的数量为第一位是1的总数/2;
第二位1的总数就是从a到b第二位是1的数量之和加上由第一位进位的数量;
第三位1的总数就是从a到b第三位是1的数量之和加上由第二位进位的数量;
.
.
.
如何求每一位上1的总数?0到8的二进制数如下:
8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 0     (0)
0 0 0 0 0 0 0 0 1   (1)
0 0 0 0 0 0 0 1 0   (2)
0 0 0 0 0 0 0 1 1   (3)
0 0 0 0 0 0 1 0 0   (4)
0 0 0 0 0 0 1 0 1   (5)
0 0 0 0 0 0 1 1 0   (6)
0 0 0 0 0 0 1 1 1   (7)
0 0 0 0 0 1 0 0 0   (8)
第一位是10交替出现,第二位是0011交替出现,第三位是00001111交替出现......
那么规律就出来了。

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    long long a,b,sum;
    long long s[70],x[70];
    while(cin>>a>>b)
    {
        memset(s,0,sizeof(s));
        memset(x,0,sizeof(x));
        sum=0;
        b++;
        if(a==b)
        {
             cout<<"0"<<endl;
             continue;
        }
        int n=b,k=2;
        for(int i=0;i<70;i++)
        {
            s[i]=a/k*k/2+(a%k>=k/2?a%k-k/2:0);//求第i位是1的个数总和
            x[i]=b/k*k/2+(b%k>=k/2?b%k-k/2:0);
            k*=2;
            n/=2;
            if(!n)
            break;
        }
        for(int i=0;i<70;i++)
        {
            sum+=(x[i]-s[i])/2;
            x[i+1]+=(x[i]-s[i])/2;
        }
        cout<<sum<<endl;
    }
    return 0;
}
求A到B之间二进制的1的个数(注意:不包括A,B本身1<=A<=B<=10^16

同样的规律

输入

1000000000000000 10000000000000000
2 12
9007199254740992 9007199254740992

输出

239502115812196372
21
1


统计下文中单词出现的个数存入字典,统计出现字数最多的三个单词和最少三个单词。删除原文中出现次数第二多的单词,其他单词保持顺序不变,先试试删除单词后的文章。 Sports help everyone to keep healthy, happy, and efficient. So 1 pay special attention to games,especially table-tennis. Table tennis is my favorite game. I play it almost every day Table-tennis is an ideal game for us because it brings the whole body into action. It strengthens our muscles, expands our lungs, promotes the circulation of the blood, and causes a healthy action of the skin. Besides, it is very amusing and does not cost us much money. Table-tennis is very moderate; it is not so rough as football. It is an indoor game and can be played even on rainy days. Thus, it is my favorite kind of exercise. One morning a fox sees a cock.He think,"This is my breakfast. He comes up to the cock and says,"I know you can sing very well.Can you sing for me? The cock is glad.He closes his eyes and begins to sing.The fox sees that and catches him in his mouth and carries him away. The people in the field see the fox.They cry,"Look, look!The fox is carrying the cock away. The cock says to the fox,"Mr Fox,do you understand?The people say you are carrying their cock away.Tell them it is yours.Not theirs. The fox opens his mouth and says,"The cock is mine, not yours.Just then the cock runs away from the fox and flies into the tree.多加注释,要流程图
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值