HackerRank - "2's complement"

This one is marked as "Advanced".. i don‘t tink so, not that hard if you can visualize all the bits from a to b. Two key points here:

1. Say both a and b are positive, bits form an interesting pattern vertically - all bit[i] of all numbers
2. negative numbers, count(a) = 32 - count(1 - a)

The above observation leads to this code:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

long long _calc(long long a, long long b)
{
    int bb = b;
    int pc = 1, sz = 2;
    long long ttl = 0;
    while (bb)
    {
        int sa = a / sz;
        int sb = b / sz;
        long long cnt = (long long)(sb - sa + 1) * (long long)pc;

        int ra = a % sz;
        if (ra > pc)
            cnt -= std::max(ra - pc, 0);
        int rb = b % sz;
        if (rb < sz - 1)
            cnt -= std::min(pc, sz - rb - 1);

        ttl += cnt;

        //    Move on
        bb >>= 1;
        pc *= 2;
        sz *= 2;
    }

    return ttl;
}

long long calc(long long a, long long b)
{
    long long cnt = 0;

    //    negative
    int na, nb;
    if (a < 0)
    {
        na = a; nb = std::min((long long)-1, b);
        long long aa = -(nb + 1);
        long long bb = -(na + 1);
        long long ret = _calc(aa, bb);
        cnt = (long long)(bb - aa + 1) * 32 - ret;
    }

    long long pa, pb;
    if (b >= 0)
    {
        pa = std::max(a, (long long)0);
        pb = b;
        cnt += _calc(pa, pb);
    }
    return cnt;
}

int main()
{
    int t; cin >> t;
    while (t--)
    {
        long long a, b; cin >> a >> b;
        auto r = calc(a, b);
        cout << r << endl;
    }
    return 0;
}

HackerRank - "2's complement"

时间: 2024-11-01 11:45:06

HackerRank - "2's complement"的相关文章

[leetcode-476-Number Complement]

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.Note:The given integer is guaranteed to fit within the range of a 32-bit signed integer.You could assume no leading zero

LeetCode 476. Number Complement(easy难度c++)

题目: Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leadin

Two&#39;s complement

https://en.wikipedia.org/wiki/Two's_complement The two's-complement system has the advantage that the fundamental arithmetic operations of addition, subtraction, and multiplication are identical to those for unsigned binary numbers (as long as the in

Bonetrousle HackerRank 数学 + 思维题

https://www.hackerrank.com/contests/world-codesprint-6/challenges/bonetrousle 给定一个数n,和k个数,1--k这k个,要求选择b个数,使得这b个数的和等于n. 首先考虑最小值,在1--k中选择前b个数,是最小的,记为mi.最大值,后b个数相加,记为mx 注意到一个东西:如果mi <= n <= mx.那么是绝对可行的.因为mi总能增加1(同时保证满足要求),所以在这个区间里面的,都是可行解. 所以首先从mi开始枚举,

hackerrank maxsum mod

https://www.hackerrank.com/challenges/maximise-sum/submissions/code/12028158 hackerrank 子数组和模m的最大值 时间复杂度nlgn, 主要是证明一点,presum[i]-presum[j] 对于0<j<i的j,最大的是第一个大于presum[i]的presum[j] 证明如下 Quro的讨论,感觉还是证明的过程不够细致 http://www.quora.com/What-is-the-logic-used-i

房价预测(HackerRank)

从今天开始要多做一些关于机器学习方面的竞赛题目,题目来源主要是Hackerrank和Kaggle.链接如下 Hackerrank:https://www.hackerrank.com/ Kaggle:https://www.kaggle.com/ 在Hackerrank中提交源代码,这就使得很多库都需要自己写,限制比较多.而Kaggle只需要提交数据,所以随便怎么搞都行.现在来讲第一道题,房价预测,这是Andrew Ng课程里的比较经典的例子.题目描述如下 题目:https://www.hack

【HackerRank】Median

题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大堆存放前半部分较小的元素,最小堆存放后半部分较大的元素,并且最大堆的所有元素小于最小堆的所有元素:保持最大堆最多比最小堆多一个元素.每次插入元素的时候都先插入到最大堆,如果发现最大堆比最小堆多了两个个,那么就从最大堆里面拿出最大的放到最小堆里面:如果发现最大堆里面新插入的元素破坏了最大堆所有元素小于

【HackerRank】Game Of Rotation

题目连接:Game Of Rotation Mark is an undergraduate student and he is interested in rotation. A conveyor belt competition is going on in the town which Mark wants to win. In the competition, there's A conveyor belt which can be represented as a strip of 1

【HackerRank】Bus Station

有n组好朋友在公交车站前排队.第i组有ai个人.还有一辆公交车在路线上行驶.公交车的容量大小为x,即它可以同时运载x个人. 当车站来车时(车总是空载过来),一些组从会队头开始走向公交车. 当然,同一组的朋友不想分开,所以仅当公交车能容纳下整个组的时候,他们才会上车.另外,每个人不想失去自己的位置,即组的顺序不会改变. 问题时如何选择公交车的容量大小x使得它可以运走所有组的人,并且公交车每次从车站出发时没有空位?(在车里的总人数恰好达到x)? 输入格式 第一行只包含一个整数n.第二行包含n个空格分