ZOJ Problem Set

来源:互联网 发布:大数据挖掘与应用 编辑:程序博客网 时间:2024/05/17 23:26

写一个从0x000000到x的结果的函数,依此来推出结果

#include<iostream>#include<string>using namespace std;int s[] = { 6,8,13,18,22,27,33,36,43,49,55,60,64,69,74,78 };long long int f(long long int x){    long long int t = 1;    long long int r = 1;    long long int re=0;    for (int i = 0; i < 8; ++i)    {        int A = x & 0xf;        x >>= 4;        re += (A==0)?s[0]*r:(s[A]-s[A-1])*r;        if (A)            re += s[A - 1] * t;        re += x*s[15]*t;        r = r+ A*t;        t<<=4;    }    return re;}int main(){    long long int  N;    long long int  a, n;    scanf("%u", &N);    while (N--)    {        long long int re = 0;        scanf("%llu", &n);        scanf("%llx", &a);        --n;        long long int b = a + n;        if (a == 0)        {            printf("%lld\n", f(b));            continue;        }        if (b > 0xffffffff)        {            re += f(0xffffffff) - f(a - 1);            b &= 0xffffffff;            re += f(b);            printf("%lld\n", re);        }        else        {            re += f(b) - f(a - 1);            printf("%lld\n", re);        }    }}
0 0