题目链接:https://ac.nowcoder.com/acm/contest/887/H

 题意:

给定A,B,C问在[1,A]和[1,B]中有多少对x,y满足x&y>C或者x^y<C.

数据范围:1<=A,B,C<=1e9,测试用例 T<=100 .

思路:

根据题意本题可以用数位dp来解。

/* and   0&0=0;  0&1=0;   1&0=0;    1&1=1;
xor 0 ^ 0=0; 0 ^ 1=1; 1^ 0=1; 1^1=0; 3
3 4 2
4 5 2
7 8 5 5
7
31 1<=x<=a
1<=y<=b x&y>c x^y<c */ #include <iostream>
#include <bitset>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int a,b,c,ans=;
cin>>a>>b>>c;
for(int i=;i<=a;i++)
for(int j=;j<=b;j++)
{
if((i^j)<c)
{
ans++;
cout<<(i^j)<<" ";
bitset<> aa(i);
bitset<> bb(j);
bitset<> cc(i^j);
cout<<aa<<" "<<i<<endl;
cout<<bb<<" "<<j<<endl;
cout<<cc<<" "<<(i^j)<<endl<<endl;
}
}
cout<<ans<<endl;
}
return ;
}
/*
7 8 5
0 3 2 4 3 0 1 4 2 1 0 4 0 1 2 3 4 1 0 3 2 4 2 3 0 1 4 3 2 1 0 31 0 3 2 4 3 0 1 4 2 1 0 4 0 1 2 3 4 1 0 3 2 4 2 3 0 1 4 3 2 1 0 31 0 3 2 4 3 0 1 4 2 1 0 4 0 1 2 3 4 1 0 3 2 4 2 3 0 1 4 3 2 1 0 0 32 0 3 2 4 3 0 1 4 2 1 0 0 1 2 4 1 0 3 4 2 3 0 22 0 3 2 3 0 1 2 1 0 0 10 0 0 0 0 0 0 0 7 0 0 1 1 0 0 1 1 0 0 1 1 0 13 0 2 0 1 2 1 0 0 1 2 1 0 2 2 0 1 2 1 0 19 0 3 2 3 0 1 2 1 0 0 1 2 3 1 0 3 2 2 3 0 1 3 2 1 0 25 0 3 2 4 3 0 1 4 2 1 0 4 0 1 2 3 4 1 0 3 2 4 2 3 0 1 4 3 2 1 0 31 */
/*
00000001
00000001
0
00000010
00000010
0
00000010
00000011
1
00000011
00000010
1
00000011
00000011
0
00000100
00000100
0
00000100
00000101
1 7
*/ /*
00000001 00000010 0 3
00000001 00000100 0 5
00000001 00000011 1 2
00000010 00000001 0 3
00000010 00000100 0 6
00000011 00000100 0 7
00000011 00000001 1 2
*/ /*
x&y>c x^y<c 2
00000001 00000001 1 0
00000010 00000010 2 0
00000010 00000011 2 1
00000011 00000010 2 1
00000011 00000011 3 0 */
/*
/*
00000001 00000100 0 5
00000001 00000110 0 7
00000001 00000111 1 6
00000001 00001000 0 9
00000010 00000100 0 6
00000010 00000101 0 7
00000010 00000111 2 5
00000010 00001000 0 10
00000011 00000100 0 7
00000011 00000101 1 6
00000011 00000110 2 5
00000011 00001000 0 11
00000100 00000001 0 5
00000100 00000010 0 6
00000100 00000011 0 7
00000100 00001000 0 12
00000101 00000010 0 7
00000101 00000011 1 6
00000101 00001000 0 13
00000110 00000001 0 7
00000110 00000011 2 5
00000110 00001000 0 14
00000111 00000001 1 6
00000111 00000010 2 5
00000111 00001000 0 15
*/ /*
x&y>c x^y<c
00000001 00000001 1 0
00000001 00000010 0 3
00000001 00000011 1 2
00000001 00000101 1 4 00000010 00000001 0 3
00000010 00000010 2 0
00000010 00000011 2 1 00000010 00000110 2 4 00000011 00000001 1 2
00000011 00000010 2 1
00000011 00000011 3 0
00000011 00000111 3 4
00000100 00000100 4 0
00000100 00000101 4 1
00000100 00000110 4 2
00000100 00000111 4 3 00000101 00000001 1 4 00000101 00000100 4 1
00000101 00000101 5 0
00000101 00000110 4 3
00000101 00000111 5 2 00000110 00000010 2 4 00000110 00000100 4 2
00000110 00000101 4 3
00000110 00000110 6 0
00000110 00000111 6 1 00000111 00000011 3 4
00000111 00000100 4 3 00000111 00000101 5 2
00000111 00000110 6 1 00000111 00000111 7 0 */
*/

分析题意的代码

 大佬的解析:

题意:给你 三个数A,B,C让你从  1~A内选择一个X,1~B内选择一个Y

找出<X,Y>的对数,使得 X&Y > C  ||  X^Y < C

平常做的数位dp都是从一个区间内找数的个数,而现在是从两个区间找数对的对数,那就在之前的三维dp上多开两维,之前是枚举一个区间上限,现在改成两个区间上限,其他的和普通的数位dp一样

注意数位dp找到的是从0开始的  0 & Y 和 X&0  一定都小于C 不需要管

但是  0 ^ Y 和 X^0  当Y  和  X  小于C时  是满足的  所以结果还需要减去 ^运算  X 或  Y  为零时的情

原文链接:https://blog.csdn.net/Daxian911/article/details/99462695

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
using namespace std;
#define ll long long
#define int long long
const int maxn=1e5+;
ll dp[][][][][];
int a[], b[],c[];
ll dfs1(int pos, int And, int Xor, int limit1, int limit2){
if(pos == -) {
if(And == || Xor == ) return ;
return ;
}
if(dp[pos][And][Xor][limit1][limit2] != -) return dp[pos][And][Xor][limit1][limit2];
int up1 = limit1 ? a[pos] : ;
int up2 = limit2 ? b[pos] : ;
ll ans = ;
for(int i = ; i <= up1; i++) {
for(int j = ; j <= up2; j++) {
int x = i & j, y = i ^ j;
int _and = And;
int _xor = Xor;
if(And == ) {
if(x > c[pos]) _and = ;
else if(x < c[pos]) _and = ;
}
if(Xor == ) {
if(y > c[pos]) _xor = ;
else if(y < c[pos]) _xor = ;
}
ans += dfs1(pos - , _and, _xor, (i == up1) && limit1, limit2 &&(j == up2));
}
}
dp[pos][And][Xor][limit1][limit2] = ans;
return ans;
}
signed main()
{
int T, A, B, C;
scanf("%lld", &T); while(T--) {
memset(dp, -, sizeof(dp));
scanf("%lld%lld%lld", &A, &B, &C);
int i = , j = , k = ;
int ans = min(C - , A) + min (C - , B) + ;
while(i < ) {
a[i++] = (A & ); A>>=;
}
while(j < ) {
b[j++] = (B & ); B>>=;
}
while(k < ) {
c[k++] = (C & ); C>>=;
}
printf("%lld\n", dfs1(,,,,) - ans);
}
return ;
}

大佬的代码,我直接复制了

另外一个大佬的代码解析:

思路:数位dp,定义一个dp[len][lim1][lim2][ok1][ok2][za][zb]。

len代表当前枚举的二进制位,lim1,lim2分别代表x和y的上限,

ok1代表对于x&y>C是否成立,成立是1,有可能成立是0,不可能成立是-1,

ok2代表对于x^y<C是否成立,1代表不成立,0代表可能成立,-1代表成立,

za和zb分别代表x和y的数中是否出现了1,因为x和y的二进制位不能是全零。转移的话代码中非常容易看懂。
原文链接:https://blog.csdn.net/qq_43316231/article/details/98968980

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll A,B,C,f[],g[],h[];
ll dp[][][][][][][];
ll dfs(ll len,ll lim1,ll lim2,ll ok1,ll ok2,ll za,ll zb){
ll &ret=dp[len][lim1][lim2][ok1+][ok2+][za][zb];
if(ret!=-)return ret;
if(len==){
ret=za&&zb&&(ok1>||ok2<);
return ret;
}
ret=; --len;
int up1=lim1?:f[len];
int up2=lim2?:g[len];
for(int x=;x<=up1;x++)for(int y=;y<=up2;y++)
ret+=dfs(len,lim1|(x<f[len]),lim2|(y<g[len]),ok1==?(x&y)-h[len]:ok1,ok2==?(x^y)-h[len]:ok2,za|x,zb|y);
return ret;
}
int main(){
scanf("%d",&t);
while(t--){
memset(dp,-,sizeof(dp));
scanf("%lld%lld%lld",&A,&B,&C);
for(int i=;i<;i++){
f[i]=A%,A/=;
g[i]=B%,B/=;
h[i]=C%,C/=;
}
printf("%lld\n",dfs(,,,,,,));
}
return ;
}

另一个大佬的,我又复制了

结合大佬的

我自己水了一遍,嘻嘻

/* and   0&0=0;  0&1=0;   1&0=0;    1&1=1;
xor 0 ^ 0=0; 0 ^ 1=1; 1^ 0=1; 1^1=0;
3 4 2
5 2
8 5
7
1<=x<=a
1<=y<=b
条件 x&y>c x^y<c 分析得 只满足x^y<c 即可。
a,b,c<1e9 转换为二进制数位是30个。
*/ #include<bits/stdc++.h>
using namespace std;
typedef long long LL; LL dp[][][][][], a[],b[],c[]; LL dfs(LL len, LL And, LL Xor, LL limit1, LL limit2){
if(len==-)
{
if(And == || Xor == ) return 1ll;
return ;
}
//满足条件,直接返回最底层答案。
if(dp[len][And][Xor][limit1][limit2] != -)
return dp[len][And][Xor][limit1][limit2];
//没到达上界并且数位已经统计过,直接返回数量。 LL up1 = limit1 ? a[len] : ;
LL up2 = limit2 ? b[len] : ;
LL cnt =;
for(LL i = ; i <= up1; i++) {
for(LL j = ; j <= up2; j++) {
LL x = i & j, y = i ^ j;
LL _and = And;
LL _xor = Xor;
if(And == ) {
if(x > c[len]) _and = ;
else if(x < c[len]) _and = ;
}
if(Xor == ) {
if(y > c[len]) _xor = ;
else if(y < c[len]) _xor = ;
}
cnt += dfs(len-,_and,_xor,(i == up1) && limit1,limit2 &&(j==up2));
}
}
dp[len][And][Xor][limit1][limit2]=cnt;
return cnt;
} LL solve(LL aa,LL bb,LL cc)
{
LL ans=min(cc - , aa) + min (cc - , bb) + ;
for(LL i=;i<;i++){
a[i]=(aa & ); aa>>=;
b[i]=(bb & ); bb>>=;
c[i]=(cc & ); cc>>=;
//b[i]=bb%2,bb/=2;
//c[i]=cc%2,cc/=2;
}
return (dfs(,,,,)-ans);
} int main()
{
int t;
cin>>t;
while(t--)
{
memset(dp, -, sizeof(dp));
LL a,b,c;
cin>>a>>b>>c;
cout<<solve(a,b,c)<<endl;
}
return ;
}

2019 牛客网 第七场 H pair的更多相关文章

  1. 2019牛客多校第⑨场H Cutting Bamboos(主席树+二分)

    原题:https://ac.nowcoder.com/acm/contest/889/H 题意: 给你一些竹子,q个询问,问你从第l到第r个竹子,如果你要用y次砍完它,并且每次砍下来的长度是相同的,问 ...

  2. 2019 牛客多校第二场 H Second Large Rectangle

    题目链接:https://ac.nowcoder.com/acm/contest/882/H 题目大意 给定一个 n * m 的 01 矩阵,求其中第二大的子矩阵,子矩阵元素必须全部为 1.输出其大小 ...

  3. 2019牛客多校第二场H题(悬线法)

    把以前的题补补,用悬线求面积第二大的子矩形.我们先求出最大子矩阵的面积,并记录其行三个方向上的悬线长度.然后排除这个矩形,记得还得特判少一行或者少一列的情况 #include <bits/std ...

  4. 2019牛客多校第一场H XOR 线性基模板

    H XOR 题意 给出一组数,求所有满足异或和为0的子集的长度和 分析 n为1e5,所以枚举子集肯定是不可行的,这种时候我们通常要转化成求每一个数的贡献,对于一组数异或和为0.我们考虑使用线性基,对这 ...

  5. 2019牛客多校第二场 A Eddy Walker(概率推公式)

    2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...

  6. 2019牛客多校第一场 I Points Division(动态规划+线段树)

    2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...

  7. 2019牛客网暑假多校训练第四场 K —number

    链接:https://ac.nowcoder.com/acm/contest/884/K来源:牛客网 题目描述 300iq loves numbers who are multiple of 300. ...

  8. 牛客网第9场多校E(思维求期望)

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...

  9. 牛客网第4场A

    链接:https://www.nowcoder.com/acm/contest/142/A 来源:牛客网 题目描述 A ternary , , or . Chiaki has a ternary in ...

随机推荐

  1. 明解C语言 入门篇 第二章答案

    练习2-1 #include <stdio.h> int main() { int x; int y; int percent; puts("请输入两个整数"); pr ...

  2. [2018-07-19] 安装python

    1.Python官网 https://www.python.org/downloads/ 2.应该使用Python2.x还是Python3.x? Python有2.x和3.x两个版本,这两个版本是不兼 ...

  3. MAC配置JAVA环境变量

    一.下载安装文件 地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html jdk-8u144-macosx-x64. ...

  4. 网络安全-主动信息收集篇第二章-三层网络发现之ping

    第三层网络扫描基于TCP/IP.ICMP协议. 优点:可路由.速度比较快 缺点:相对于二层网络扫描较慢,容易被边界防火墙过滤 所有扫描发现技术,都会有相应的对抗办法,所以无论是来自二层的网络扫描还是来 ...

  5. 常用git命令教程

    一.新建代码库 # 在当前目录新建一个Git代码库 $ git init # 新建一个目录,将其初始化为Git代码库 $ git init [project-name] # 下载一个项目和它的整个代码 ...

  6. Spring Cloud Gateway使用简介

    Spring Cloud Gateway是类似Nginx的网关路由代理,有替代原来Spring cloud zuul之意: Spring 5 推出了自己的Spring Cloud Gateway,支持 ...

  7. 『题解』洛谷P3384 【模板】树链剖分

    Problem Portal Portal1: Luogu Description 如题,已知一棵包含\(N\)个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作\(1\): ...

  8. canvas线条实践之运动的正方形

    原理说明: 1.通过rect实现正方形的绘制: 2.save保存canvas面板的保存,restore回复保存的canvas面板到初始状态: 3.translate用于改变canvas坐标的起始位置: ...

  9. springcloud微服务基于redis集群的单点登录

    springcloud微服务基于redis集群的单点登录 yls 2019-9-23 简介 本文介绍微服务架构中如何实现单点登录功能 创建三个服务: 操作redis集群的服务,用于多个服务之间共享数据 ...

  10. fpm打包神奇rpm包升级python2.7.16

    fpm打包神器参考文档:https://www.cnblogs.com/flintlovesam/p/6594635.html FPM的安装:安装ruby环境和gem命令: yum -y instal ...