HDU 4588 统计二进制加法进位次数

本文介绍了一个有趣的问题:如何统计从一个数加到另一个数的过程中,二进制表示下总的进位次数。通过分析二进制加法的特点,文章提供了一种高效的算法实现,用于解决这个问题。

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

Count The Carries


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?

 

统计用二进制从a加到b的进位次数

规律题,先统计出从a到b每一位上1的个数a[i],打表可发现每一位上0和1出现的规律

对第i位,进位次数为a[i]/2,每次进位会使高一位上多一个1,即a[i+1]+=a[i]/2


#include <cstdio>
#include <cstring>

int aa[100],bb[100];

void gettable(int n,int m)
{
	memset(aa,0,sizeof(aa));
	memset(bb,0,sizeof(bb));
	int t=1;
	n+=1;
	m+=1;
	for(int i=0;i<64;i++)
    {
        if(t>n)
			break;
        t*=2;
        aa[i]+=(n-n%t)/2;

        if(n%t>t/2)
			aa[i]+=n%t-t/2;

    }
    t=1;
    for(int i=0;i<64;i++)
    {
        if(t>m)
			break;
        t*=2;
        bb[i]+=(m-m%t)/2;
        if(m%t>t/2)
			bb[i]+=m%t-t/2;
    }
}

int main()
{
	int a,b;
	long long ans;
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		gettable(a-1,b);
		for(int i=0;i<64;i++)
		{
			bb[i]-=aa[i];
		}
		ans=0;
		for(int i=0;i<64;i++)
		{
			ans+=bb[i]/2;
			bb[i+1]+=bb[i]/2;
		}
		printf("%I64d\n",ans);
	}

}


Eclipse二进制文件是指由Eclipse集成开发环境生成的可执行文件。在Linux下,Eclipse生成的二进制文件通常是没有扩展名的,如上述引用所示。在使用Eclipse编译和运行项目,如果项目名包含扩展名(例如.hdu.c),则Eclipse可能无法正确识别该文件为二进制执行文件。 这可能导致在运行出现找不到二进制文件的错误。解决办法有两种:一是避免使用带有扩展名的项目名,另一种是手动创建一个运行配置,将命令写死以确保正确识别为二进制文件。 需要注意的是,上述讨论中的EclipseParser库是一个用于验证和读取Eclipse二进制文件内容的DLL库,它是通过按照二进制格式编写的。这个库可以验证和读取Eclipse二进制文件中的EGRID、INIT、UNRST等内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [linux下eclipse c++运行不了提示找不到二进制文件的解决方法](https://blog.youkuaiyun.com/bjrxyz/article/details/8974483)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Eclipse油藏数值模拟软件的二进制文件格式解析](https://blog.youkuaiyun.com/slofslb/article/details/119176891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值