链接: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)的更多相关文章

  1. 2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)

    链接:https://ac.nowcoder.com/acm/contest/897/J 来源:牛客网 Binary Number 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...

  2. 2019长安大学ACM校赛网络同步赛C LaTale (树上DP)

    链接:https://ac.nowcoder.com/acm/contest/897/C来源:牛客网 LaTale 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32768K,其他语 ...

  3. 2019长安大学ACM校赛网络同步赛 B Trial of Devil (递归)

    链接:https://ac.nowcoder.com/acm/contest/897/B来源:牛客网 Trial of Devil 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...

  4. 2019长安大学ACM校赛网络同步赛 M LCM (数论)

    链接:https://ac.nowcoder.com/acm/contest/897/M来源:牛客网 LCM 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65 ...

  5. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 I

    链接:https://www.nowcoder.com/acm/contest/122/I来源:牛客网 题目描述 小q最近在做一个项目,其中涉及到了一个计时器的使用,但是笨笨的小q却犯难了,他想请你帮 ...

  6. NOI 2018网络同步赛(游记?)

    刚中考完那段时间比较无聊,报名了一个同步赛,报完名才发现成绩单是要挂到网上的,而且因为报的早给了一个很靠前的考号...那布星啊,赶紧学点东西,于是在一周内学了网络流,Treap以及一些数论. Day1 ...

  7. 第十三届北航程序设计竞赛决赛网络同步赛 B题 校赛签到(建树 + 打标记)

    题目链接  校赛签到 对每个操作之间建立关系. 比较正常的是前$3$种操作,若第$i$个操作属于前$3$种,那么就从操作$i-1$向$i$连一条有向边. 比较特殊的是第$4$种操作,若第$i$个操作属 ...

  8. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  9. $NOI$ $2019$ 网络同步赛

    D2T1考试前测了自己造的“假”极限数据,看了看内存发现是118MB,感觉没问题就交上去了. 赛后用Lemon测了一下:MLE 88->0 对于别的题我已经不想再说什么了. update: 由于 ...

随机推荐

  1. MethodChannel 实现flutter 与 原生通信

    Flutter 步骤 目的:在flutter页面中主动调用原生页面中的方法. 1 在widget中,定义MethodChannel变量 static const MethodChannel metho ...

  2. IJCAI 2019 Analysis

    IJCAI 2019 Analysis 检索不到论文的关键词:retrofitting word embedding Getting in Shape: Word Embedding SubSpace ...

  3. Git检出和提交至远程仓库

    步骤一:首先需要一个Github账号,还没有的话先去注册:https://github.com/,我们使用Git需要先安装Git工具,这里给出下载地址:https://git-for-windows. ...

  4. ControlTemplate in WPF —— Menu

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  5. 阶段3 2.Spring_06.Spring的新注解_7 spring整合junit问题分析

    测试类重复代码的问题 这是之前的方式 运行findAll的方法,没有问题 测试人员不需要关心上面的方法,.应该关心的各个方法是否能够正常的运行 对于一个测试工程师,只要写完变量就可以测试了. 可以使用 ...

  6. 慕课网_Java入门第三季

    第1章 异常与异常处理 1-1 Java异常简介 (06:50) 1-2 Java中使用try..catch..finally实现异常处理 (05:08) import java.util.Input ...

  7. linux判断httpd端口是否打开

    判断端口是否打开 lsof -i:80 判断端口打开了几个 lsof -i:80 | wc -l

  8. java:struts框架3(自定义拦截器,token令牌,文件上传和下载(单/多))

    1.自定义拦截器: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  9. java:Review(J2ee)

    1.oracle: 1.1 增:insert into 删:delete from 改:update tablename set 查:select * from 1.2 聚合函数 max,min,av ...

  10. java:HTML(table表格,ul列表)和CSS(导入.css文件,三种定义颜色方式,三种样式选择器,a标签属性顺序,)

    1.重点掌握: html: 1.form表单:input,checkbox,seelct,radio,button,submit 2.table表格:thead-->tr-->th;tbo ...