当前位置:网站首页>【luogu P3220】与非(构造)(数位DP)(推论)
【luogu P3220】与非(构造)(数位DP)(推论)
2022-07-17 20:48:00 【SSL_TJH】
与非
题目链接:luogu P3220
题目大意
给你一个操作叫做与非,就是把两个数与起来然后取反。
然后给你一些 K 位二进制数,问你可以通过操作得到多少在 L~R 中的数。
思路
很好玩的一道题呢!
你考虑这个操作: n o t ( A & B ) not(A\& B) not(A&B)
自己跟自己: n o t ( A & A ) = n o t ( A ) not(A\&A)=not(A) not(A&A)=not(A),非操作
操作之后非一下: n o t ( n o t ( A & A ) ) = A & A not(not(A\&A))=A\&A not(not(A&A))=A&A,与操作。
那或操作也有了: n o t ( n o t ( A ) & n o t ( B ) ) not(not(A)\¬(B)) not(not(A)¬(B))
那异或也有了:(就一个真一个假) ( A & n o t ( B ) ) o r ( n o t ( A ) & B ) (A\& not(B))or(not(A)\&B) (A¬(B))or(not(A)&B)
那这四个操作都有了看起来啥都能搞了,那考虑找找例外。
会发现如果对于给出的所有数,存在两位之间要么都是 1 1 1 要么都是 0 0 0,你是不可能凑出 10 , 01 10,01 10,01 这些的,所以这个就相当于形成了若干了连通块。
那我们就可以数位 DP 做了。
那我们考虑证一下为什么只有这个是不行的,也就是剩下的情况为啥可以。
(那我们就把这些绑定的看做一个)
我们考虑有异或,我们就用类似线性基的思想,考虑求出只有某一位是 1 1 1 的情况。
这样就可以或得到所有了。
那怎么得到一个位置呢?我们先考虑不管别的地方 ,我们就要一个地方是 1 1 1。
那我们可以把所有的数,这一位是 1 1 1 的就正常用,不是的就取反了再用,这样所有得到的与起来,这一位就是 1 1 1,而且看起来别的都会是 0 0 0。
仔细想想会发现确实是这样的,因为你反证一下就是如果别的一位是 1 1 1,那这一位是 1 1 1 的地方它要是 1 1 1,是 0 0 0 的它也要是 0 0 0,那不就一模一样,可以绑定看做一个了吗?
所以就证了。
代码
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const ll N = 1005;
ll n, K, lim[60];
bool sam[60][60], sp[60];
ll L, R, a[N];
ll clac(ll R, ll wei, bool ding) {
if (wei < 0) return 1;
if (!ding) {
ll num = 0; for (ll i = 0; i <= wei; i++) if (sp[i]) num++;
return (1ll << num);
}
if (lim[wei] == -1) {
ll re = 0;
for (ll i = 0; i <= ((R >> wei) & 1); i++) {
ll tmp = 0;
for (ll j = 0; j < wei; j++) if (sam[wei][j]) lim[j] = i;
re += clac(R, wei - 1, i == ((R >> wei) & 1));
for (ll j = 0; j < wei; j++) if (sam[wei][j]) lim[j] = -1;
}
return re;
}
else {
if (lim[wei] == 1 && !((R >> wei) & 1)) return 0;
return clac(R, wei - 1, lim[wei] == ((R >> wei) & 1));
}
}
ll work(ll R) {
memset(lim, -1, sizeof(lim));
if (R < 0) return 0;
return clac(R, K - 1, (R >> K) ? 0 : 1);
}
int main() {
scanf("%lld %lld %lld %lld", &n, &K, &L, &R);
for (ll i = 0; i < K; i++) for (ll j = 0; j < K; j++) sam[i][j] = 1;
for (ll i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
for (ll k = 0; k < K; k++)
for (ll kk = 0; kk < k; kk++)
if (((a[i] >> k) & 1) ^ ((a[i] >> kk) & 1)) sam[k][kk] = sam[kk][k] = 0;
}
for (ll i = 0; i < K; i++) {
sp[i] = 1;
for (ll j = i + 1; j < K; j++) if (sam[i][j]) {
sp[i] = 0; break;}
}
printf("%lld", work(R) - work(L - 1));
return 0;
}
边栏推荐
- 洛谷P2422 良好的感觉 题解
- Robotics at Google:Laura Graesser | i-Sim2Real:在紧密的人机交互循环中强化学习机器人策略
- 手册不全,如何手工刨出TongWeb的监控信息?
- 陶博士月线反转6.0
- [acwing] solution of the 60th weekly match
- 面额10exp(303)的钞票
- [dynamic programming] - longest ascending subsequence model
- 面试记录
- 欧奈尔的RPS曲线的编制方法(陶博士原创)
- A review of classical must see for Nonconvex Optimization Problems "from symmetry to geometry", University of Rochester, et al
猜你喜欢

非凸优化问题经典必看综述“从对称性到几何性”,罗切斯特大学等

Installation of Topy Library (topology optimization software)

Rotation formula of coordinate simulation matrix

华为无线设备配置频谱导航

Importerror: DLL load failed while importing win32api: the specified program cannot be found.

Huawei wireless device configuration intelligent roaming

Ranking and evaluation of the second "green tree Cup" Mathematics Competition

Use of Google browser developer tools (Master!)

Take a look at try{}catch{}

Some puzzles about data dictionary
随机推荐
Redis源码与设计剖析 -- 2.链表
非凸优化问题经典必看综述“从对称性到几何性”,罗切斯特大学等
Go-Excelize API源码阅读(三)——OpenReader()
Initial understanding of functions - next
A review of classical must see for Nonconvex Optimization Problems "from symmetry to geometry", University of Rochester, et al
负载均衡有哪几种实现方式?
Huawei wireless devices are configured with static load balancing
Brief introduction of Bezier curve
Huawei technologies:jonathan Krolikowski | from design to deployment, zero contact deep reinforcement learning WLANs
009 execution sequence of SQL statement of interview questions
Introduction:Multiple DataFrames
Ranking of top ten ERP software systems at home and abroad!
函数初认识-下
No.4 bits, bytes, information storage
暑期rhcsa培训第一天作业
ModuleNotFoundError: No module named ‘_ distutils_ hack‘
Okaleido or get out of the NFT siege, are you optimistic about it?
STL string find substring
[acwing] solution of the 60th weekly match
AcWing 136. Adjacent value lookup