牛客多校第七场H Pair 数位dp理解
Pair
题意
给出A B C,问x取值[1,A]和y取值[1,B]存在多少组pair<x,y>满足以下最小一种条件,\(x \& y >c\),\(x\) xor \(y<c\)
分析
有关二进制位运算的操作肯定是和要联想到和位的关系的,我们可以考虑枚举每一位计数,但这样会复杂度爆炸,枚举每一位有没有想到什么?数位dp,我们可以考虑把题目条件装化,全集好求,那么求他的补集,求所有\(x \& y <=c\)并且\(x\) xor \(y>=c\),然后用全集A×B减去就是答案了。
这里的数位dp状态为dp[位数][A枚举上界][B枚举上界][是否满足x and y< c ][是否满足x xor y>c][A是否取了不为0的数][B是否取了不为0的数]
这里有一个关键点,状态中[是否满足x and y< c ][是否满足x xor y>c] 没有取等号,为什么不取等号呢,因为如果条件相反大于的时候我们会直接continue,那么状态就只剩下了到目前位置等于c,和不等于c的相应条件了,这是两种不同的状态,但是都是合法的,所以要记录下来。
刚开始写的时候状态为dp[位数][是否满足x and y< c ][是否满足x xor y>c][A是否取了不为0的数][B是否取了不为0的数] ,即把两个limit没有放到状态里面,就T了。因为常规的写法数位dp是求两个区间之间的值,所以dp的状态要对两个数通用,如果加了limit的话,对于我们的状态定义来说,同一个状态定义对两个不同的数实际上是有不同的,而limit没限制的条件居多,所以之前写的数位dp题状态都是没有limit1的情况,这些情况都暴力解决了,当然也可以在这些数位dp的题目中加上limit的状态,但是这样就要每次求之前memset dp数组了。而在本题中,因为只进行一次操作,并且都是01串,两个limit1的情况也是非常多的,如果不记录这两个状态的话,就会导致超时了T_T
第一份AC 第二份状态少了T
#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define mkp make_pair
typedef long long ll;
using namespace std;
const int maxn=5e5+5;
int abit[35],bbit[35],cbit[35];
const int mod=1e9+7;
int n,q;
ll dp[35][3][3][3][3][3][3];
ll dfs(int dep,bool limit1,bool limit2,bool ok1,bool ok2,bool ling1,bool ling2){
if(dep==0){
return ling1&&ling2;
}
if(dp[dep][ok1][ok2][ling1][ling2][limit1][limit2]!=-1)return dp[dep][ok1][ok2][ling1][ling2][limit1][limit2];
ll ans=0;
int up1=limit1?abit[dep]:1;
int up2=limit2?bbit[dep]:1;
for(int i=0;i<=up1;i++){
for(int j=0;j<=up2;j++){
if(!ok1&&(i&j)>cbit[dep])continue;
if(!ok2&&(i^j)<cbit[dep])continue;
ans+=dfs(dep-1,limit1&&i==up1,limit2&&j==up2,ok1||((i&j)<cbit[dep]),ok2||((i^j)>cbit[dep]),ling1||i!=0,ling2||j!=0);
}
}
return dp[dep][ok1][ok2][ling1][ling2][limit1][limit2]=ans;
}
ll solve(int a,int b,int c){
memset(dp,-1,sizeof(dp));
for(int i=1;i<=30;i++){
abit[i]=a&1;
bbit[i]=b&1;
cbit[i]=c&1;
a>>=1;
b>>=1;
c>>=1;
}
return dfs(30,1,1,0,0,0,0);
}
int main(){
int t;
scanf("%d",&t);
int a,b,c;
while(t--){
scanf("%d%d%d",&a,&b,&c);
printf("%lld\n",1ll*a*b-solve(a,b,c));
}
return 0;
}
#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define mkp make_pair
typedef long long ll;
using namespace std;
const int maxn=5e5+5;
int abit[35],bbit[35],cbit[35];
const int mod=1e9+7;
int n,q;
ll dp[35][3][3][3][3];
ll dfs(int dep,bool limit1,bool limit2,bool ok1,bool ok2,bool ling1,bool ling2){
if(dep==0){
return ling1&&ling2;
}
if(!limit1&&!limit2&&dp[dep][ok1][ok2][ling1][ling2]!=-1)return dp[dep][ok1][ok2][ling1][ling2];
ll ans=0;
int up1=limit1?abit[dep]:1;
int up2=limit2?bbit[dep]:1;
for(int i=0;i<=up1;i++){
for(int j=0;j<=up2;j++){
if(!ok1&&(i&j)>cbit[dep])continue;
if(!ok2&&(i^j)<cbit[dep])continue;
ans+=dfs(dep-1,limit1&&i==up1,limit2&&j==up2,ok1||((i&j)<cbit[dep]),ok2||((i^j)>cbit[dep]),ling1||i!=0,ling2||j!=0);
}
}
return dp[dep][ok1][ok2][ling1][ling2]=ans;
}
ll solve(int a,int b,int c){
memset(dp,-1,sizeof(dp));
for(int i=1;i<=30;i++){
abit[i]=a&1;
bbit[i]=b&1;
cbit[i]=c&1;
a>>=1;
b>>=1;
c>>=1;
}
return dfs(30,1,1,0,0,0,0);
}
int main(){
int t;
scanf("%d",&t);
int a,b,c;
while(t--){
scanf("%d%d%d",&a,&b,&c);
printf("%lld\n",1ll*a*b-solve(a,b,c));
}
return 0;
}
牛客多校第七场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 < ...
- 2019牛客多校第六场H Pair(数位DP 多个数相关)题解
题意: 传送门 给你\(A,B,C\),要求你给出有多少对\((x, y)\)满足\(x\in [1,A],y\in [1,B]\),且满足以下任意一个条件:\(x \& y > C\) ...
- 牛客多校第七场 C Bit Compression 思维
链接:https://www.nowcoder.com/acm/contest/145/C来源:牛客网 A binary string s of length N = 2n is given. You ...
- 2018牛客多校第五场 H.subseq
题意: 给出a数组的排列.求出字典序第k小的b数组的排列,满足1<=bi<=n,bi<bi+1,a[b[i]]<a[b[i+1]],m>0. 题解: 用树状数组倒着求出以 ...
- 2019牛客多校第七场E Find the median 权值线段树+离散化
Find the median 题目链接: https://ac.nowcoder.com/acm/contest/887/E 题目描述 Let median of some array be the ...
- 两两内积为0(牛客多校第七场)-- CDMA
题意: 构造一个n*n的矩阵,元素只能是-1或1,任意两行内积为0(两两相乘加起来和为0). 思路: #define IOS ios_base::sync_with_stdio(0); cin.tie ...
- [题解]Magic Line-计算几何(2019牛客多校第三场H题)
题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意: 给你偶数个点的坐标,找出一条直线将这n个点分成数量相等的两部分 并在这条直线上取不同的两个点,表示 ...
- 2019牛客多校第五场H - subsequence 2 拓扑
H - subsequence 2 题意 要你使用前\(m\)个小写字母构造一个长度为\(n\)的字符串 有\(m*(m-1)/2\)个限制条件: \(c_{1} .c_{2}. len\):表示除去 ...
- 2019牛客多校第七场C-Governing sand(线段树+枚举)
Governing sand 题目传送门 解题思路 枚举每一种高度作为最大高度,则需要的最小花费的钱是:砍掉所有比这个高度高的树的所有花费+砍掉比这个高度低的树里最便宜的m棵树的花费,m为高度低的里面 ...
随机推荐
- PHP0016:PHP http协议
post提交请求头
- NAS之NFS/CIFS
NAS之NFS 为集群中的 Web Server 配置后端存储 NFS:Network File System 网络文件系统,Unix系统之间共享文件的一种协议NFS 的客户端主要为Linux支持多节 ...
- HTTP Status 404 – 未找到 spring mvc
HTTP Status 404 – 未找到 Type Status Report 消息 /houseSale//houseSaleController/houseSaleList 描述 源服务器未能找 ...
- 【笔记】机器学习 - 李宏毅 - 6 - Logistic Regression
Logistic Regression 逻辑回归 逻辑回归与线性回归有很多相似的地方.后面会做对比,先将逻辑回归函数可视化一下. 与其所对应的损失函数如下,并将求max转换为min,并转换为求指数形式 ...
- Uva12169 扩展欧几里得模板
Uva12169(扩展欧几里得) 题意: 已知 $x_i=(a*x_{i-1}+b) mod 10001$,且告诉你 $x_1,x_3.........x_{2t-1}$, 让你求出其偶数列 解法: ...
- Redis初级安装及使用
env:ubuntu 19.04 redis官网: redis.io 安装步骤: 1.wget http://download.redis.io/releases/redis-5.0.7.tar.gz ...
- jQuery笔记(四)jQuery中的动画
jQuery最吸引人的地方莫过于能做出绚丽的动画了,也是能极大提高用户体验的地方,这次我们就来一探jQuery中的动画! 一. show()方法和hide()方法 show()方法与hide()方法是 ...
- tp5使用PHPexcel扩展导出excel表
1,使用composer安装phpexcel包: composer require phpoffice/phpexcel 2,在控制器中创建方法: (1)使用PHPexcel扩展.代码如下 /** * ...
- PAT (Advanced Level) Practice 1054 The Dominant Color (20 分)
Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of i ...
- ASP.NET常用内置对象(三)Server
Server对象是HttpServerUtility的一个实例,也是上下文对象HttpContext的一个属性,提供用于处理Web请求的Helper方法. Server.MapPath("& ...