bitmasks brute force combinatorics dp *2300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>

#pragma GCC optimize(1)

#pragma GCC optimize(2)

#pragma GCC optimize(3,"Ofast","inline")

using namespace std;

typedef long long ll;

ll f[500][2][2];

ll bit1[505],bit2[505];

ll dp(ll def,ll eq1,ll eq2){

    if(!def)return 1;

    if(f[def][eq1][eq2] != -1)return f[def][eq1][eq2];

    ll up1 = eq1?bit1[def]:1;

    ll up2 = eq2?bit2[def]:1;

    ll ans = 0;

    for(int i = 0;i <= up1;i++){

        for(int j = 0;j <= up2;j++){

            if(i+j <= 1)ans += dp(def-1,eq1&&(i==up1),eq2&&(j==up2));

        }

    }

    f[def][eq1][eq2] = ans;

    return ans;

}

ll work(ll x,ll y){

    if(x == -1||y == -1)	return 0;

    memset(f,-1,sizeof(f));

    ll pos = 0;

    for(;x||y;x>>=1,y>>=1){

        bit1[++pos] = x&1;

        bit2[pos] = y&1;

    }

    ll ans = 0;

    ans = dp(pos,1,1);

    return ans;

}

void solve(){

    ll a,b;cin >> a >> b;

    ll ret = work(b,b)-2*work(b,a-1)+work(a-1,a-1);//二维前缀和思想,画图就明白了

    cout << ret << "\n";

}

int main(){

    ios_base::sync_with_stdio(0);

    ll t;cin >> t;

    while(t--)solve();

}




Comments

Submit
0 Comments
More Questions

1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson