码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive 6527 Counting ones dfs(水

时间:2016-01-21 15:46:22      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:点击打开链接




#include <cstdio>
#include <vector>
using namespace std;
typedef long long ll;
ll re;
vector<int> p;
void dfs(int dep, int g) {
	if (dep == 0)
		return ;
	if (p[dep-1] == 1) {
		re += (dep-1) * (1ll<< (dep-2));
		re += g * (1ll << (dep-1));
	}
	dfs(dep-1, g+p[dep-1]);
}
ll C(ll x) {
	if (x == 0)
		return 0;
	p.clear();
	re = 0;
	while (x>0) {
		p.push_back(x%2);
		x /= 2;
	}
	for (int i = 0; i < (int)p.size(); ++i)
		re += p[i];
	dfs(p.size(), 0);
	return re;
}
int main() {
	ll A, B;
	while (~scanf("%lld%lld", &A, &B))
		printf("%lld\n", C(B) - C(A-1));
	return 0;
}


UVALive 6527 Counting ones dfs(水

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/5148436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!