Links: https://ac.nowcoder.com/acm/contest/897/L
Source: Cattle-off network
XOR
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
Exclusive or is a logical operation that outputs true only when inputs differ(one is true, the other is false). It is symbolized by the infix operators such as XOR,
⊕
⊕.
This time, brave QQQ raises a problem to you. Given an interval [l, r], you need to calculate how many numbers x between l and r, where x satisfies
x
⊕
4
x
⊕
5
x
=
0
x⊕4x⊕5x=0.
输入描述:
The first line contains an integer number T, the number of test cases.
I
T
H
Ith T Lines Next the contains of each TWO integers L, R & lt (
1
≤
L
≤
R & lt
≤
10
18 is
1≤l≤r≤1018).
Output Description:
the For each Test Print Case The answer.
Example 1
Input
Copy
2
2 . 6
. 1 109
output
copy
. 4
39
Meaning of the questions: idea: according to the laws or different, and we know that x ^ x = 0, ^ is the XOR operation, that is equal to the number two exclusive or up to 0,
Because the exclusive-OR operation and commutative and distributive law.
Therefore x⊕4x⊕5x = 0. Can be obtained, (x⊕4x) ⊕5x = 0.
Then when x⊕4x = 5x the conditions are satisfied, we also know x⊕4x = x + 4x if and only if x and 4x in a binary state, any one not simultaneously 1.
And 4 * x x is in a binary 0 state tail complement two, i.e. left by 2 bits, in order to satisfy the above conditions is not necessary to satisfy the two binary numbers is only a few intermediate.
For example, can not have 101, 111 binary, this string.
This obviously is a digital dp direct explosion certainly make life difficult for search, plus a memory of the search.
See details Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int a[700];
int cnt;
ll dp[70][2][2];
ll dfs(int dep, int last1, int last2, bool limit)
{
ll res = 0ll;
if (dep == 0) {
return 1ll;
} else {
if (limit) {
int up = a[dep];
for (int i = 0; i <= up; ++i) {
if (i) {
if (last2!=1) {
res += dfs(dep - 1, i, last1, limit && (i == a[dep]));
}
} else {
res += dfs(dep - 1, i, last1, limit && (i == a[dep]));
}
}
return res;
} else {
if(dp[dep][last1][last2]!=-1)
{
return dp[dep][last1][last2];
}
int up = 1;
for (int i = 0; i <= up; ++i) {
if (i) {
if (last2!=1){
res += dfs(dep - 1, i, last1, limit && (i == a[dep]));
}
} else {
res += dfs(dep - 1, i, last1, limit && (i == a[dep]));
}
}
dp[dep][last1][last2]=res;
return res;
}
}
}
ll solve(ll x)
{
cnt = 0;
while (x) {
if (x & 1) {
a[++cnt] = 1;
} else {
a[++cnt] = 0;
}
x >>= 1;
}
return dfs(cnt, 0, 0, 1);
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
int t;
gbtb;
cin >> t;
ll l, r;
memset(dp,-1,sizeof(dp));
while (t--) {
cin >> l >> r;
cout << solve(r) - solve(l - 1) << endl;
}
return 0;
}
inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}