九度OJ 1113题解

<!-- lang: cpp -->
#include<cstdio>
#include<cstring>
#include<cstdlib>

int num = 0;

int calculate(int m, int n)
{
    int left, right, add = 1;
    num++;
    left = 2*m;
    right = 2*m+1;
    while(right <= n)
    {
        add *= 2;
        num += add;
        left *= 2;
        right = 2*right+1;
     }
    if(left <= n)
        num += n-left+1;
    return num;
}

int main()
{
    int m, n;
    while(scanf("%d %d", &m, &n) != EOF)
    {
        num = 0;
        if(m == 0 && n == 0) return 0;
        printf("%d\n", calculate(m, n));
    }
    return 0;
}

你可能感兴趣的:(九度OJ,1113)