数位dp+判定转状态:Loj #6274. 数字

本文搬运自本人高中时期CSDN博客,若图片加载不出来,可到原文查看:https://blog.csdn.net/zhangtingxiqwq/article/details/132950029

https://loj.ac/p/6274

和位运算有关,然后值域范围又非常大,位之间关联不大,显然考虑数位dp

然后有上下界限制,直接来个4维

然后每一位考虑,先满足or的性质,然后考虑and

发现有冲突只会是(1,0)和(0,1)

首先如果发生冲突,则要么无限制,要么上界为1,下界为0

所以某位0的以后不会受上界影响,某位为1以后不会受下界影响

所以在这两种情况中,我们取个max即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
//#define M
//#define mo
#define N 70
int n, m, i, j, k;
int f[N][2][2][2][2], T[N], Lx[N], Ly[N], Rx[N], Ry[N];

void mke(int *a, int x) {
if(!x) return ;
int k=-1;
while(x) a[++k]=x%2, x/=2;
}

int dp(int k, int sx, int sy, int tx, int ty) {
if(k<0) return 1;
if(f[k][sx][sy][tx][ty]!=-1) return f[k][sx][sy][tx][ty];
int flg=0, Sx, Sy, Tx, Ty, ans=0;
for(int x=0; x<=1; ++x)
for(int y=0; y<=1; ++y) {
if(sx && x<Lx[k]) continue;
if(sy && x>Ly[k]) continue;
if(tx && y<Rx[k]) continue;
if(ty && y>Ry[k]) continue;
if((x|y)!=T[k]) continue;
int p1=sx&(x==Lx[k]), p2=sy&(x==Ly[k]), q1=tx&(y==Rx[k]), q2=ty&(y==Ry[k]);
if((x&y)==1)
ans+=dp(k-1, p1, p2, q1, q2);
else flg=max(dp(k-1, p1, p2, q1, q2), flg);
}
ans+=flg;
return f[k][sx][sy][tx][ty]=ans;
}

signed main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// srand(time(NULL));
// T=read();
// while(T--) {
//
// }
memset(f, -1, sizeof(f));
n=read(); mke(T, n);
n=read(); mke(Lx, n);
n=read(); mke(Ly, n);
n=read(); mke(Rx, n);
n=read(); mke(Ry, n);
printf("%lld", dp(61, 1, 1, 1, 1));
return 0;
}