HDU 4588 Count The Carries (数学,计数)
题意:给定两个十进制数,求二进制中,从x加到y的二进制进了多少位。
析:把这些数字的二进制纵向罗列出来,然后一位一位的把和加起来,最终得到总的进位数。从1到x,第i位上1的总数是x左移i+1位再右移i位后得到的,
(在第 0位上,1和0以1010101010的周期出现,并且每个周期一个1,在第1位上,1和0以11001100的周期出现,并且每个周期2个1,以此类推,
则第n位上的1的个数是x/2^n*2^(n-1),即先左移n+1位,再右移n位),如果x在i位上面上是1,特殊判断一下,求一下周期以外的1的个数,这个时候的1的个数是
a%(1<<k)+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 <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const int maxn = 10000 + 5; LL solve(LL num, int i){
if(-1 == num) return 0; LL ans = (num>>i+1)<<i;
if(num & (1LL<<i)) ans += (num % (1LL<<i)) + 1LL; return ans;
} int main(){
int a, b;
while(scanf("%d %d", &a, &b) == 2){ LL ans = 0;
LL sum = 0;
for(int i = 0; i < 63; ++i){
sum += solve(b, i) - solve(a-1, i);
sum /= 2;
ans += sum;
} cout << ans << endl;
}
return 0;
}
HDU 4588 Count The Carries (数学,计数)的更多相关文章
- HDU 4588 Count The Carries 数学
Count The CarriesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- HDU 4588 Count The Carries 计算二进制进位总数
点击打开链接 Count The Carries Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- HDU 4588 Count The Carries(数学统计)
Description One day, Implus gets interested in binary addition and binary carry. He will transfer al ...
- HDU 4588 Count The Carries(找规律,模拟)
题目 大意: 求二进制的a加到b的进位数. 思路: 列出前几个2进制,找规律模拟. #include <stdio.h> #include <iostream> #includ ...
- hdu 4588 Count The Carries
思路:容易发现二进制表示的数的最低位规律是01010101……:接着是001100110011……:接着是:0000111100001111…… 这样我们发现每一位的循环节是2^(i+1),前2^i是 ...
- HDU 4588 Count The Carries 数位DP || 打表找规律
2013年南京邀请赛的铜牌题...做的非常是伤心.另外有两个不太好想到的地方.. ..a 能够等于零,另外a到b的累加和比較大.大约在2^70左右. 首先说一下解题思路. 首先统计出每一位的1的个数, ...
- HDU 5901 Count primes 大素数计数
题意:计算1~N间素数的个数(N<=1e11) 题解:题目要求很简单,作为论文题,模板有两种 \(O(n^\frac{3}{4} )\),另一种lehmer\(O(n^\frac{2}{3})\ ...
- [hdu 6184 Counting Stars(三元环计数)
hdu 6184 Counting Stars(三元环计数) 题意: 给一张n个点m条边的无向图,问有多少个\(A-structure\) 其中\(A-structure\)满足\(V=(A,B,C, ...
- HDU 5441 Travel (并查集+数学+计数)
题意:给你一个带权的无向图,然后q(q≤5000)次询问,问有多少对城市(城市对(u,v)与(v,u)算不同的城市对,而且u≠v)之间的边的长度不超过d(如果城市u到城市v途经城市w, 那么需要城市u ...
随机推荐
- BZOJ 2429 聪明的猴子
kruskal. #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...
- Service完全解析(转)
今天我们来讲一下Android中Service的相关内容. Service在Android中和Activity是属于同一级别上的组件,我们可以将他们认为是两个好哥们,Activity仪表不凡,迷倒万千 ...
- adb shell 查找并删除文件
# -*- coding: cp936 -*- ## function: remove file ## remark: python version -- import os,sys import l ...
- unity, sprite atlas
一, Sprite Packer 可以直接在unity里放碎图,只要将Texture Type选为Sprite(2D and UI),Sprite Mode选为Single,再把想打在一张大图里的碎图 ...
- 转:移动开发中一些bug及解决方案
网页开发要面对各种各样的浏览器,让人很头疼,而移动开发中,你不但要面对浏览器,还要面对各种版本的手机,iOS好一点,而安卓就五花八门了,你可能在开发中也被它们折磨过,或者正在被它们折磨,我在这里说几个 ...
- 部署K2 Blackpearl流程时出错(与基础事务管理器的通信失败或Communication with the underlying transaction manager has failed.
转:http://www.cnblogs.com/dannyli/archive/2011/12/01/2270222.html 亲,在部署K2流程是,是否遇到这个错误(以下是中.英文错误信息) 中文 ...
- apple-touch-icon,shortcut icon和icon的区别
apple-touch-icon 可以了解到这是一个类似网站favicon的图标文件,用来在iphone和ipod上创建快捷键时使用. 这个文件应当是png格式,57x57像素大小,放在网站根目录之下 ...
- linux下ubuntu系统安装及开发环境配置
1.安装系统:别的没什么说的,就是安的时候把网线拔了,不然到 configure apt的时候会卡起很久不走的2.配置网络 编辑/etc/network/interface打开/etc/networt ...
- mysql 1130 ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server
mysql -u root -p mysql;use mysql; mysql;select 'host' from user where user='root'; mysql;update u ...
- 《A Tour of PostgreSQL Internals》学习笔记——进程间通信
中秋节假期这么快就没了,这几天还一直下雨,索性在家看看书.这次看的是Tom Lane的<A Tour of PostgreSQL Internals>.这篇小随笔就算做学习笔记了.园子里面 ...