uva 11645

Problem J
Bits 
Input: Standard Input

Output: Standard Output

bit is a binary digit, taking a logical value of either "1" or "0" (also referred to as "true" or "false" respectively).  And every decimal number has a binary representation which is actually a series of bits. If a bit of a number is “1” and it‘s next bit is also “1” then we can say that the number has a 1 adjacent bit. And you have to find out how many times this scenario occurs for all numbers up to N.

Examples:

Number           Binary                         Adjacent Bits

12                    1100                            1

15                    1111                            3

27                    11011                          2

Input

For each test case, you are given an integer number (0 <= N <= ((2^63)-2)), as described in the statement. The last test case is followed by a negative integer in a line by itself, denoting the end of input file.

Output

For every test case, print a line of the form “Case X: Y”, where X is the serial of output (starting from 1) and Y is the cumulative summation of all adjacent bits from 0 to N.

题意:

  给出定义 A(x) 为 x 的二进制表示中 11 的个数.

  请你求出 A(x) 的前缀和 S(x)

思路:

  如果真正理解了 uva 11038, 那么这道题应该差不多...

  分段考虑的话,就是当第 i 与 i-1 位 为1 的时候,

  左边 有.... 右边有......

  根据乘法原理, 直接乘起来就ok.

  再根据加法原理,直接加起来就是答案.

  由于答案比较大.所以采用两个数字压位的做法..(学到的好方法.)

  我的程序写得非常啰嗦.....不如再去看看别人的.

  

 1 #include<cstdlib>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<cstring>
 5 using namespace std;
 6 const long long lim = (long long)1e13;
 7 typedef long long BIG[2];
 8 BIG ans;
 9 long long n,x1,x2,x3,L;
10 int kase;
11 void add(BIG &a,long long b){
12     a[1] += b; a[0] += a[1] / lim; a[1] %= lim;
13 }
14 int digit(long long x,int pos){ return (x & (1LL << pos)) != 0; }
15 long long ext(long long n,long long st,long long ed){
16     if(st > ed) return 0;
17     n >>= st;
18     return n & ((1LL << (ed - st + 1)) - 1);
19 }
20 int main()
21 {
22     freopen("bits.in","r",stdin);
23     freopen("bits.out","w",stdout);
24     while(scanf("%lld",&n), n >= 0){
25         ans[0] = ans[1] = 0;
26         for(L = 63; L > 0 && !digit(n,L); L--);
27         int a = digit(n,L), b = digit(n,L-1);
28         if(a == b && a == 1 && n != 3) add(ans, ext(n,0,L-2) + 1);
29         a = digit(n,1), b = digit(n,0);
30         add(ans, ext(n,2,L) + (a && b));
31         for(int i = L-1; i >= 2; --i){
32             a = digit(n,i), b = digit(n,i-1);
33             x1 = ext(n,i+1,L), x2 = ext(n,0,i-2) + 1, x3 = (1LL << (i-1));
34             add(ans,(x1 && a && b ? 1 : 0) * x2 + x1 * x3);
35         }
36         add(ans,0);
37         printf("Case %d: ",++kase);
38         if(ans[0]){
39             printf("%lld",ans[0]);
40             printf("%013lld\n",ans[1]);
41         }
42         else printf("%lld\n",ans[1]);
43     }
44     return 0;
45 }

时间: 2024-07-31 21:07:30

uva 11645的相关文章

uva 11645 - Bits(计数问题+高精度)

题目链接:uva 11645 - Bits 题目大意:给出n,问从0到n这n+1个数种,数的二进制情况下,有多少11存在. 解题思路:和uva 11038一个类型的题目,只是这道题目是对于二进制下的情况.而且高精度部分可以用两个long long数解决. #include <cstdio> #include <cstring> typedef long long ll; const int N = 100; const ll M = 1e13; ll bit (int k) { r

UVA 11645 - Bits(数论+计数问题)

题目链接:11645 - Bits 题意:给定一个数字n.要求0-n的二进制形式下,连续11的个数. 思路:和?UVA 11038?这题相似,枚举中间,然后处理两边的情况. 只是本题最大的答案会超过longlong,要用高精度,只是借鉴http://www.cnblogs.com/TO-Asia/p/3214706.html这个人的方法,直接用两个数字来保存一个数字.这样能保存到2个longlong的长度,就足够存放这题的答案了. 代码: #include <stdio.h> #include

UVA - 11645 Bits

Description Problem J Bits Input: Standard Input Output: Standard Output A bit is a binary digit, taking a logical value of either "1" or "0" (also referred to as "true" or "false" respectively).  And every decimal

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica