UVA 11645

题目链接:11645 - Bits

题意:给定一个数字n。要求0-n的二进制形式下,连续11的个数。

思路:和 UVA 11038 这题相似,枚举中间,然后处理两边的情况。
只是本题最大的答案会超过longlong,要用高精度,只是借鉴http://www.cnblogs.com/TO-Asia/p/3214706.html这个人的方法,直接用两个数字来保存一个数字。这样能保存到2个longlong的长度,就足够存放这题的答案了。
代码:
#include <stdio.h>
#include <string.h>

long long n, a, b;
const long long DIG = 1e13;
void add(long long num) {
	b += num;
	a += b / DIG;
	b %= DIG;
}

int main() {
	int cas = 0;
	while (~scanf("%lld", &n) && n >= 0) {
		a = b = 0;
		long long tmp = n, d = 1;
		while (n > 1) {
			add((n>>2) * d);
			if ((n&3) == 3)
				add((tmp&(d - 1)) + 1);
			d <<= 1;
			n >>= 1;
		}
		printf("Case %d: ", ++cas);
		if (a) {
  			printf("%lld", a);
  			printf("%013lld
", b);
		}
		else printf("%lld
", b);
 	}
	return 0;
}


原文地址:https://www.cnblogs.com/mqxnongmin/p/10773003.html