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';
}
}
}
2019长安大学ACM校赛网络同步赛 L XOR (规律,数位DP)的更多相关文章
- 2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)
链接:https://ac.nowcoder.com/acm/contest/897/J 来源:牛客网 Binary Number 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...
- 2019长安大学ACM校赛网络同步赛C LaTale (树上DP)
链接:https://ac.nowcoder.com/acm/contest/897/C来源:牛客网 LaTale 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32768K,其他语 ...
- 2019长安大学ACM校赛网络同步赛 B Trial of Devil (递归)
链接:https://ac.nowcoder.com/acm/contest/897/B来源:牛客网 Trial of Devil 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...
- 2019长安大学ACM校赛网络同步赛 M LCM (数论)
链接:https://ac.nowcoder.com/acm/contest/897/M来源:牛客网 LCM 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65 ...
- 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 I
链接:https://www.nowcoder.com/acm/contest/122/I来源:牛客网 题目描述 小q最近在做一个项目,其中涉及到了一个计时器的使用,但是笨笨的小q却犯难了,他想请你帮 ...
- NOI 2018网络同步赛(游记?)
刚中考完那段时间比较无聊,报名了一个同步赛,报完名才发现成绩单是要挂到网上的,而且因为报的早给了一个很靠前的考号...那布星啊,赶紧学点东西,于是在一周内学了网络流,Treap以及一些数论. Day1 ...
- 第十三届北航程序设计竞赛决赛网络同步赛 B题 校赛签到(建树 + 打标记)
题目链接 校赛签到 对每个操作之间建立关系. 比较正常的是前$3$种操作,若第$i$个操作属于前$3$种,那么就从操作$i-1$向$i$连一条有向边. 比较特殊的是第$4$种操作,若第$i$个操作属 ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- $NOI$ $2019$ 网络同步赛
D2T1考试前测了自己造的“假”极限数据,看了看内存发现是118MB,感觉没问题就交上去了. 赛后用Lemon测了一下:MLE 88->0 对于别的题我已经不想再说什么了. update: 由于 ...
随机推荐
- Dark 数据类型
dark基础数据类型 1数值型 num int a =1; double b=1.0; 2 字符型 string a ='hello'; 插值表达式${expression} int a = 1; ...
- linux添加新用户
使用root用户adduser yj 后面操作有提示 用这种方法新建的用户会在home下生成用户文件夹
- mingw 编译 libopus 1.3.1 时 注意事项
OPUS_STACK_PROTECTOR 默认是使用的, 在 windows 上编译时一定要去掉选项不然 -lopus 链接时出现错误undefined reference to `__stack_c ...
- Elastic Stack学习
原文链接 Elastic Stack简称ELK,在本教程你将学习如何快速搭建并运行Elastic Stack. 首先你要安装核心开源产品: Elasticsearch: Kibana: Beats: ...
- GO——beego简单开发实例(二)
在新建项目成功之后我们可以做一个简单的动态增删查改. 1.在models文件夹下新建models.go,根据模型新建表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...
- [不错]A step-by-step guide to enabling security, TLS/SSL, and PKI authentication in Elasticsearch
Now posted on the Elastic blog December 12, 2018 update: This article has been published on Elastic’ ...
- C# 中的字符串内插
$ 特殊字符将字符串文本标识为内插字符串. 内插字符串是可能包含内插表达式的字符串文本. 将内插字符串解析为结果字符串时,带有内插表达式的项会替换为表达式结果的字符串表示形式. 此功能在 C# 6 及 ...
- Opencv之LBP特征(算法)
LBP(Local Binary Pattern),即局部二进制模式,对一个像素点以半径r画一个圈,在圈上取K个点(一般为8),这K个点的值(像素值大于中心点为1,否则为0)组成K位二进制数.此即局部 ...
- python基础--导入模块
一,import的使用1, 模块就是一组功能的集合体,我们的程序可以导入模块来复用模块中的功能一个模块就是包含了一组功能的python文件,例如demo.py 可以通过import来使用这个文件定义d ...
- 【神经网络与深度学习】学习笔记:AlexNet&Imagenet学习笔记
学习笔记:AlexNet&Imagenet学习笔记 ImageNet(http://www.image-net.org)是李菲菲组的图像库,和WordNet 可以结合使用 (毕业于Caltec ...