2019牛客多校第六场H Pair(数位DP 多个数相关)题解
题意:
传送门
给你\(A,B,C\),要求你给出有多少对\((x, y)\)满足\(x\in [1,A],y\in [1,B]\),且满足以下任意一个条件:\(x \& y > C\)或者\(x \oplus y < C\)。
思路:
数位\(DP\),以前做的数位\(DP\)只是和一个数相关,今天是和两个数相关,有点神奇。这里我开了九维,第\(i\)位\(x\)是\(j\),\(y\)是\(k\),对\(第一种\)情况,对\(第二种\)情况,\(x\)到达上界,\(y\)到达上界,\(x\)前导零,\(y\)前导零。一开始只开了前五维,但是\(T\)了。因为在二进制中,其中一个数达到上界的情况其实非常多,那么如果我每次都要求\(!limita\ \&\&\ !limitb\)时才返回\(dp\)那么势必造成很多情况都要\(dfs\)很多次求解。前导零同理。
代码:
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100000 + 5;
const int INF = 0x3f3f3f3f;
const ull seed = 131;
const ll MOD = 1e9 + 7;
using namespace std;
ll dp[40][3][3][3][3][3][3][3][3]; //第i位x是j,y是k,对第一种情况,对第二种情况,x到达上界,y到达上界,x前导零,y前导零
//0不知 1不满足 2满足
int bit1[40], bit2[40], C;
//x and y > C
//x xor y < C
ll dfs(int pos, int x, int y, int oxor, int oand, int stx, int sta, bool limita, bool limitb, bool leadx, bool leady){
if(pos == -1){
if((stx == 2 || sta == 2) && !leadx && !leady) return 1;
return 0;
}
if(dp[pos][x][y][stx][sta][limita][limitb][leadx][leady] != -1) return dp[pos][x][y][stx][sta][limita][limitb][leadx][leady];
int top1 = limita? bit1[pos] : 1;
int top2 = limitb? bit2[pos] : 1;
ll ret = 0;
for(int i = 0; i <= top1; i++){
for(int j = 0; j <= top2; j++){
int nxor = (oxor << 1) + (i ^ j), nand = (oand << 1) + (i & j);
int nstx = stx, nsta = sta;
if(stx == 0 && nxor > (C >> pos)){
nstx = 1;
}
else if(stx == 0 && nxor < (C >> pos)){
nstx = 2;
}
if(sta == 0 && nand > (C >> pos)){
nsta = 2;
}
else if(sta == 0 && nand < (C >> pos)){
nsta = 1;
};
ret += dfs(pos - 1, i, j, nxor, nand, nstx, nsta, limita && i == top1, limitb && j == top2, leadx && !i, leady && !j);
}
}
dp[pos][x][y][stx][sta][limita][limitb][leadx][leady] = ret;
return ret;
}
ll solve(int A, int B){
int pos = 0;
if(A < B) swap(A, B);
while(A){
bit1[pos] = A & 1;
A >>= 1;
bit2[pos++] = B & 1;
B >>= 1;
}
ll ans = dfs(pos - 1, 0, 0, 0, 0, 0, 0, true, true, true, true);
return ans;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
memset(dp, -1, sizeof(dp));
int a, b;
scanf("%d%d%d", &a, &b, &C);
ll ans = solve(a, b);
printf("%lld\n", ans);
}
return 0;
}
2019牛客多校第六场H Pair(数位DP 多个数相关)题解的更多相关文章
- 2019牛客多校第七场H Pair 数位DP
题意:给你一个3个数A, B, C问有多少对pair(i, j),1 <= i <= A, 1 <= j <= B, i AND j > C或 i XOR j < ...
- 牛客多校第七场H Pair 数位dp理解
Pair 题意 给出A B C,问x取值[1,A]和y取值[1,B]存在多少组pair<x,y>满足以下最小一种条件,\(x \& y >c\),\(x\) xor \(y& ...
- 2019牛客多校第六场 B - Shorten IPv6 Address 模拟
B - Shorten IPv6 Address 题意 给你\(128\)位的二进制,转换为十六进制. 每\(4\)位十六进制分为\(1\)组,每两组用一个\(":"\)分开. 每 ...
- [题解]Shorten IPv6 Address-模拟(2019牛客多校第六场B题)
题目链接:https://ac.nowcoder.com/acm/contest/886/B 题意: 您将获得一个IPv6地址,该地址是128位二进制字符串.请根据以下规则确定其最短的表示: 以十六进 ...
- 2019牛客多校第六场J-Upgrading Technology(枚举+单调队列)
Upgrading Technology 题目传送门 解题思路 对于这题,我们可以枚举一个k从0~m,表示当前我们把所有技能最少升到了k级,且至少有一个为k级. 此时我们刚好获得了前k个d[]的收益, ...
- 2019 牛客多校第六场 D Move
题目链接:https://ac.nowcoder.com/acm/contest/886/D 题解摘自官方题解 题目大意 有 K 个体积相同的箱子,有 N 个体积相同或相异的物品,现要按照如下策略装箱 ...
- 2019 牛客多校第六场 J Upgrading Technology
题目链接:https://ac.nowcoder.com/acm/contest/886/J 题目大意 略. 分析 见代码. 代码如下 #include <bits/stdc++.h> u ...
- 2019 牛客多校第六场 B Shorten IPv6 Address
题目链接:https://ac.nowcoder.com/acm/contest/886/B 题目大意 给定一个 128 位的二进制 ip 地址,让你以 16 位一组,每组转成 16 进制,用冒号连接 ...
- [题解]Magic Line-计算几何(2019牛客多校第三场H题)
题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意: 给你偶数个点的坐标,找出一条直线将这n个点分成数量相等的两部分 并在这条直线上取不同的两个点,表示 ...
随机推荐
- 1.5V升3V芯片和电路图,DC-DC升压IC
1.5V升3V的升压芯片,3V给LED供电,或者单片机模块供电等. PW5200A工作频率为1.4MHZ.轻载时自动PWM/PFM模式切换,提高效率. PW5200A能够提供2.5V和5V之间的可调输 ...
- 树莓派安装 Ubuntu 20.04 LTS 碰壁指南
树莓派安装 Ubuntu 20.04 LTS 碰壁指南 设备 Raspberry 4B 4+32G 系统 Ubuntu 20.04 LTS 1.镜像下载与烧录 镜像下载地址:https://cdima ...
- 初审blucms(入坑)
作为一名初来乍到审计小白,从blueCMS入手再好不过了.通过对入门级的cms进行审计以及一个整体的框架和常见的漏洞学习,对个人而言是一次不错的学习经历.话不多说直接进入主题. 代码审计环境 Blue ...
- Python+Selenium+Unittest实现PO模式web自动化框架(8)
1.main.py模块的功能 最后就是要有一个项目入口,并且是需要加载测试用例集. # --^_^-- coding:utf-8 --^_^-- # @Remark:运行入口 "" ...
- jQuery 当前展开其他收缩 三级下拉菜单(转载)
jQuery可展开收缩三级下拉菜单 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- https://www.cs.cmu.edu/~dga/papers/cuckoo-conext2014.pdf 检验hash冲突
https://github.com/google/cityhash We like to test hash functions with SMHasher, among other things. ...
- 将字符串进行md5加密
import java.security.MessageDigest; public class MD5Tools { /** * 将字符串进行md5加密 */ public static Strin ...
- Java——定时任务调度工具
一.什么是定时任务调度? 1.常用的定时调度工具:Timer和Quartz 二.Timer简介 1.Timer的定义以及架构 2.Timer示例 三.Timer的定时调度函数 1.schedule的四 ...
- 将Spring Boot项目运行在Docker上
将Spring Boot项目运行在Docker上 一.使用Dockerfile构建Docker镜像 1.1Dockerfile常用指令 1.1.1ADD复制文件 1.1.2ARG设置构建参数 1.1. ...
- 小米和MAC触摸板手势汇总
小米的触摸手势: 左键:单指单击 右键:双指单击 选取并打开:单指双击 滚动页面:双指 移动 拖拽项目:双击并拖拽 放大/缩小:双指张开,双指捏合 MAC触摸板手势: http://www.cr173 ...