• Codeforces Round #597 (Div. 2) F


    F. Daniel and Spring Cleaning

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it seems like it is broken. When he tries to compute 1+31+3 using the calculator, he gets 22 instead of 44. But when he tries computing 1+41+4, he gets the correct answer, 55. Puzzled by this mystery, he opened up his calculator and found the answer to the riddle: the full adders became half adders!

    So, when he tries to compute the sum a+ba+b using the calculator, he instead gets the xorsum a⊕ba⊕b (read the definition by the link: https://en.wikipedia.org/wiki/Exclusive_or).

    As he saw earlier, the calculator sometimes gives the correct answer. And so, he wonders, given integers ll and rr, how many pairs of integers (a,b)(a,b) satisfy the following conditions:

    a+b=a⊕ba+b=a⊕b

    l≤a≤rl≤a≤r

    l≤b≤rl≤b≤r

    However, Daniel the Barman is going to the bar and will return in two hours. He tells you to solve the problem before he returns, or else you will have to enjoy being blocked.

    Input

    The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of testcases.

    Then, tt lines follow, each containing two space-separated integers ll and rr (0≤l≤r≤1090≤l≤r≤109).

    Output

    Print tt integers, the ii-th integer should be the answer to the ii-th testcase.

    Example

    input

    Copy

    3
    1 4
    323 323
    1 1000000
    

    output

    Copy

    8
    0
    3439863766
    

    Note

    a⊕ba⊕b denotes the bitwise XOR of aa and bb.

    For the first testcase, the pairs are: (1,2)(1,2), (1,4)(1,4), (2,1)(2,1), (2,4)(2,4), (3,4)(3,4), (4,1)(4,1), (4,2)(4,2), and (4,3)(4,3).

    数位dp还是写过一些题的,不过看到这道题的时候还是没有办法

    立刻想出做法 这里有一个结论就是满足该条件的两个数的二进制同一位不能同时为1

    然后我们根据数位dp的方法搜索出所有可能的对数

    注意的是我们不能用传统的数位dp的做法

    在这里即使该位被限制了,我们也要将这一位记录下来 以后使用

    如果使用传统的数位dp的方法是会超时

    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
    int ar[50];
    int br[50];
    int dp[50][2][2];
    int dfs(int pos,int lim1,int lim2)
    {
        if(pos==0) return 1;
        int ans=0;
        if(dp[pos][lim1][lim2]!=-1)
        {
            return dp[pos][lim1][lim2];
        }
        int sa=lim1?ar[pos]:1;
        int sb=lim2?br[pos]:1;
        for(int i=0;i<=sa;i++)
        {
            for(int j=0;j<=sb;j++)
            {
                if((i&j)==0)
                {
                    ans+=dfs(pos-1,lim1&(sa==i),lim2&(sb==j));
                }
            }
        }
        dp[pos][lim1][lim2]=ans;
        return ans;
    }
    int solve(int a,int b)
    {
        if(a<0||b<0) return 0;
        memset(dp,-1,sizeof(dp));
        for(int i=1;i<=31;i++)
        {
            ar[i]=a&1;
            br[i]=b&1;
            a/=2;
            b/=2;
    //        cout<<ar[i]<<endl;
        }
        return dfs(31,1,1);
    }
    signed main()
    {
        int t;
        scanf("%lld",&t);
        while(t--)
        {
            int a,b;
            scanf("%lld%lld",&a,&b);
            printf("%lld
    ",solve(b,b)-2*solve(a-1,b)+solve(a-1,a-1));
        }
    }
    
    
  • 相关阅读:
    C#编程(七十三)----------浅析C#中内存管理
    C#高级编程小结
    C#编程(七十二)----------DynamicObject和ExpandoObject
    C#编程(七十一)----------DLR ScriptRuntime
    C#编程(七十)----------dynamic类型
    C#编程(六十九)----------DLR简介
    C#编程(六十八)----------LINQ小结
    C#编程(六十七)----------LINQ提供程序
    C#编程(六十六)----------表达式树总结
    python 显示上午下午
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852189.html
Copyright © 2020-2023  润新知