点击打开链接

Count The Carries

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 672    Accepted Submission(s): 230

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
 
Source

给你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<stdio.h>
#include<string.h>
long long s[77],x[77];
int main()
{
long long a,b;
while(scanf("%I64d%I64d",&a,&b)!=EOF)
{
memset(s,0,sizeof(s));
memset(x,0,sizeof(x));
b++;
int n=b;
if(a==b){printf("0\n");continue;}
for(int i=0,k=2;i<=70;i++,k*=2)
{
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);
n/=2;if(!n)break;
}
long long count=0;
for(int i=0;i<70;i++)
{
count+=(x[i]-s[i])/2;
x[i+1]+=(x[i]-s[i])/2;
}
printf("%I64d\n",count);
}
return 0;
}

HDU 4588 Count The Carries 计算二进制进位总数的更多相关文章

  1. HDU 4588 Count The Carries 数学

    Count The CarriesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  2. HDU 4588 Count The Carries(数学统计)

    Description One day, Implus gets interested in binary addition and binary carry. He will transfer al ...

  3. HDU 4588 Count The Carries(找规律,模拟)

    题目 大意: 求二进制的a加到b的进位数. 思路: 列出前几个2进制,找规律模拟. #include <stdio.h> #include <iostream> #includ ...

  4. HDU 4588 Count The Carries (数学,计数)

    题意:给定两个十进制数,求二进制中,从x加到y的二进制进了多少位. 析:把这些数字的二进制纵向罗列出来,然后一位一位的把和加起来,最终得到总的进位数.从1到x,第i位上1的总数是x左移i+1位再右移i ...

  5. hdu 4588 Count The Carries

    思路:容易发现二进制表示的数的最低位规律是01010101……:接着是001100110011……:接着是:0000111100001111…… 这样我们发现每一位的循环节是2^(i+1),前2^i是 ...

  6. HDU 4588 Count The Carries 数位DP || 打表找规律

    2013年南京邀请赛的铜牌题...做的非常是伤心.另外有两个不太好想到的地方.. ..a 能够等于零,另外a到b的累加和比較大.大约在2^70左右. 首先说一下解题思路. 首先统计出每一位的1的个数, ...

  7. Leetcode696.Count Binary Substrings计算二进制字串

    给定一个字符串 s,计算具有相同数量0和1的非空(连续)子字符串的数量,并且这些子字符串中的所有0和所有1都是组合在一起的. 重复出现的子串要计算它们出现的次数. 示例 1 : 输入: "0 ...

  8. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  9. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

随机推荐

  1. Eclipse中如何安装和使用GrepCode插件

    GrepCode(GC)Eclipse插件允许Eclipse用户在Eclipse IDE中搜索由GrepCode提供的工厂类.本教程介绍如何安装和使用插件.使用Eclipse3.5(Galileo)的 ...

  2. i2c sub system __i2c_board_list/klist_devices/klist_drivers

    i2c_devinfo全局链表: __i2c_board_list 用来挂接 i2c_board_info,这个信息用来生成 i2c_client i2c_client 链表: i2c_bus_typ ...

  3. rpm包安装

    RPM全称是“RedHatPackageManager”是由RedHat公司发发展起来的,本质是将软件源码包经过编译并且打包成rpm的格式,rpm文件包含的有二进制文件,配置文件,库文件等,同时RPM ...

  4. NumberBox( 数值输入框) 组件

    本节课重点了解 EasyUI 中 NumberBox(数值输入框)组件的使用方法,这个组件依赖于 ValidateBox(验证框)组件.一. 加载方式//class 加载方式<input typ ...

  5. Layout( 布局)

    一. 加载方式//class 加载方式<div id="box" class="easyui-layout"style="width:600px ...

  6. Linux_x64安装Oracle11g(完整版)

    一.修改操作系统核心参数 在Root用户下执行以下步骤: 1)修改用户的SHELL的限制,修改/etc/security/limits.conf文件 输入命令:vi /etc/security/lim ...

  7. javascript中的substr和substring

    1.substr 方法 返回一个从指定位置开始的指定长度的子字符串. stringvar.substr(start [, length ]) 参数: stringvar 必选项.  要提取子字符串的字 ...

  8. 如何使用sql语句使你的数据库减肥,下面以网狐6603数据库减肥脚本举例!

    原文转自:http://www.zccode.com/forum.php?mod=viewthread&tid=637&extra=page%3D1 网狐6603 专用数据库减肥特效脚 ...

  9. Clover周报模块 -- 开发总结

    2014年7月8日 00:16:05 一.切图 这次开发,切图花了不少时间,样式是用scss写的,第一次用,不过用着用着就发现它的强大,层级.作用域.重用等都非常的方便,还有考拉神器,用着真是爽!不过 ...

  10. CSAPP--优化程序性能

    一.编写高效的程序: 1.选择合适的算法和数据结构. 2.编写出编译器能够有效优化以转换为高效可执行的源代码. 3.并行计算.当然重点还是第一个,良好的算法和数据结构大大减小了程序的时间复杂度. 二. ...