链接: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';}}
}

转载于:https://www.cnblogs.com/qieqiemin/p/11228817.html

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校赛网络同步赛 Trial of Devil

    链接:https://ac.nowcoder.com/acm/contest/view-submission?submissionId=40669755 来源:牛客网 题目描述 As an acmer ...

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

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

  4. 2019长安大学ACM校赛网络同步赛 L XOR

    题意 求区间内有多少数x满足x^4x^5x=0 1≤l≤r≤1018. 题解 根据异或的性质可以推到 x^4x=5x -->x^4x=x+4x 即x和4x每一位都不同,即x和(x<< ...

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

    A,C,I签到题,只搞了8题,还一题是神仙做的,我不会 链接:https://www.nowcoder.com/acm/contest/122/B 来源:牛客网 取石子 时间限制:C/C++ 1秒,其 ...

  6. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛B 取石子(博弈SG函数模板)

    题目链接:取石子 链接:https://www.nowcoder.com/acm/contest/122/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65 ...

  7. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 部分题解

    A-ID and password 签到题. #include <iostream> #include <cstdio> #include <bits/stdc++.h& ...

  8. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 - 题解

    A - ID and password 题目描述 Users prefer simple passwords that are easy to remember, but such passwords ...

  9. 2020中国大学生程序设计竞赛(CCPC) - 网络选拔赛----HDU--6899、Xor(数位dp)

    题目链接 题面: 题意: 求解符合 x∈[0,a],y∈[0,b]x\in[0,a],y\in[0,b]x∈[0,a],y∈[0,b] 且 ∣x−y∣≤k|x-y|\le k∣x−y∣≤k 且 xxo ...

最新文章

  1. robots协议是什么?对网站SEO有什么好处?
  2. Ubuntu最简单的方式安装NVIDIA显卡驱动和cuda工具
  3. 【C 语言】二级指针案例 ( 字符串切割 | 返回 自定义二级指针 作为结果 )
  4. Python教程:Python内置数据结构之双向队列!
  5. java虚拟机10.内存模型与线程
  6. 专栏导读:数据驱动的优化
  7. set python用法_Python set()用法及代码示例
  8. [Flink]Flink实时框架介绍
  9. Codeforces 1169A Circle Metro
  10. linux下加载ISO镜像的方法
  11. 美国政府继续紧盯中兴,并可能剑指华为
  12. Java使用POI导出Excel文件
  13. dtft性质及证明_FFT , DTFT, DFT 的区别和联系?
  14. 浙大PAT 1051
  15. LFS(linux for stratch)关于急救盘的制作问题
  16. 什么是JPA?SpringBoot 中使用JPA
  17. GTA5美化MOD了,这套MOD真的绝了……?
  18. 医疗APP功能解析一微医
  19. 开发新设备设计时使用低温探针台的 8425 型直流霍尔系统
  20. java判断是否安装了pdf_java判断上传文件是否为pdf java图像上传中如何判断是否是jpg格式...

热门文章

  1. Report not added to business roles
  2. 我回答的一个粉丝关于用编程语言模拟SAP事务的问题
  3. 如何查看S/4HANA指定时间段批量生产订单的状态
  4. 运行虚拟机报错:vmware workstations与devicecredential不兼容(终极解决方案)
  5. 三维家可以导入别人的方案吗_三维激光扫描仪
  6. 要关闭python解释器可使用函数或者快捷键_【判断题】螺旋机构具有结构简单,传动平稳,噪声低等优点,被广泛应用。...
  7. c语言的函数中局部变量可以return吗,C语言--返回局部变量的地址
  8. mysql 在时间上加120s_sqlserver日期推算(年,季度,月,星期推算)
  9. UE4学习-场景介绍、基本操作、快捷键
  10. hbase集群之间数据迁移_hbase数据迁移到另一集群上