zoukankan      html  css  js  c++  java
  • HDU 4588 Count The Carries 数学

    Count The Carries
    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87326#problem/C

    Description

    One day, Implus gets interested in binary addition and binary carry. He will transfer all decimal digits to binary digits to make the addition. Not as clever as Gauss, to make the addition from a to b, he will add them one by one from a to b in order. For example, from 1 to 3 (decimal digit), he will firstly calculate 01 (1)+10 (2), get 11,then calculate 11+11 (3),lastly 110 (binary digit), we can find that in the total process, only 2 binary carries happen. He wants to find out that quickly. Given a and b in decimal, we transfer into binary digits and use Implus's addition algorithm, how many carries are there?.

    Input

    Two integers a, b(0<=a<=b<1000000000), about 100000 cases, end with EOF.

    Output

    One answer per line.

    Sample Input

    1 2
    1 3
    1 4
    1 6

    Sample Output

    0
    2
    3
    6

    HINT

    题意

    二进制累加,从A累加到B,问你在二进制中,一共进位了多少次

    题解

    我并不知道怎么做的……

    队友啪啪啪就拍完了= =

    代码:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    
    using namespace std;
    
    long long x[70],y[70];
    
    int main()
    {
        long long a,b;
        while(scanf("%lld%lld",&a,&b)!=EOF)
        {
            long long now=0;
            long long cnt=0;
            if(a>0) a--;
            for(long long i=1LL;i<=60*1LL;i++)
            {
    
                x[i]=(1LL<<(i-1LL))*(a>>i)+max(0LL,(a%(1LL<<i)-(1LL<<(i-1LL))+1LL));
                y[i]=(1LL<<(i-1LL))*(b>>i)+max(0LL,(b%(1LL<<i)-(1LL<<(i-1LL))+1LL));
                now+=y[i]-x[i];
                cnt+=1LL*(now>>1LL);
                now=now>>1LL;
            }
            while(now)
            {
                cnt+=1LL*now;
                now>>=1LL;
            }
            printf("%lld
    ",cnt);
        }
        return 0;
    }
  • 相关阅读:
    基于flash的web视频对讲直播测试
    终于放弃Windows下丑陋的cmd
    直播推流兼容性问题追踪
    32位和64位dll判断
    H264编码参数的一些小细节
    cmake编译win下64位obs
    The certificate used to sign "XXX" has either expired or has been revoked
    记录一次Android交叉编译ffmpeg排查错误
    SDK "iphoneos" cannot be located
    av_interleaved_write_frame 网络不好的情况下返回较慢
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4722379.html
Copyright © 2011-2022 走看看