2019长安大学ACM校赛网络同步赛 L XOR (规律,数位DP)
链接:https://ac.nowcoder.com/acm/contest/897/L
来源:牛客网
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 of each next T lines contains two integers l, r(
1
≤
l
≤
r
≤
10
18
1≤l≤r≤1018).
输出描述:
For each test case print the answer.
示例1
输入
复制
2
2 6
1 109
输出
复制
4
39
题意:
思路:
根据异或的规律,我们知道x^x=0, ^是异或运算,即两个相等的数异或起来为0,
又因为异或运算满足交换律和分配律。
所以 x⊕4x⊕5x=0. 可以得到,(x⊕4x)⊕5x=0.
那么当x⊕4x=5x 使满足条件,我们还知道x⊕4x=x+4x 当且仅当 x与4x 在二进制状态下,任一位不同时为1.
而4*x 就是x在二进制状态下 尾部补两个0,也就是左移2位,那么为了满足上面的条件也就要满足 二进制数中没有两个1中间只有一个数。
例如二进制中不能有 101 ,111 ,这种子串。
这显然就是数位dp了,直接爆搜肯定过不去,加一个记忆化搜索即可。
细节见代码:
#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';
}
}
}
本博客为本人原创,如需转载,请必须声明博客的源地址。
本人博客地址为:www.cnblogs.com/qieqiemin/
希望所写的文章对您有帮助。