HDU 4588 统计二进制加法进位次数
来源:互联网 发布:r软件下载 编辑:程序博客网 时间:2024/06/11 15:46
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?
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);}}
0 0
- HDU 4588 统计二进制加法进位次数
- hdu 4588 (加法进位次数统计)
- HDU 4588--二进制的加法进位统计
- 加法进位次数
- HDU 4596 (Yet another end of the world)(二进制进位次数和)
- HDU 4588 Count The Carries 计算二进制进位总数
- BNUoj Carries 统计进位的次数(优化)
- 加法进位
- HDU 1250 Hat's Fibonacci 大数加法 进位10000000
- 大数的加法 进位
- 先行进位加法
- 剑指-二进制相加进位
- 实现进位的加法算法
- 进位次数(大整数)
- 【C++】计算进位的次数
- 二进制加法
- 二进制加法
- 二进制加法
- 集合ArrayList Hashtable,泛型集合
- Partition List 二分链表(重)
- UDP协议来快速无丢失传输图像程序
- Codeforces 383C . Propagating tree【树状数组,dfs】
- newLISP 递归访问目录树
- HDU 4588 统计二进制加法进位次数
- Codeforces Round #257 (Div. 2)D(最短路Dijkstra+堆优化)
- 菜鸟学51单片机之----在笔记本上模拟PC和51串口通信2
- 一张图让你明白Android Touch事件的传递机制
- 如何修复ubuntu启动时显示的“The System Running In Low Graphics Mode’ Error”问题
- ghigh
- 找工作准备
- 6离开了好看回家狂欢
- 美丽号结构化价格惠