Find a car CodeForces - 809C (数位DP)

Posted uid001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Find a car CodeForces - 809C (数位DP)相关的知识,希望对你有一定的参考价值。

大意: 给定一个$1e9 imes 1e9$的矩阵$a$, $a_{i,j}$为它正上方和正左方未出现过的最小数, 每个询问求一个矩形内的和.

 

可以发现$a_{i,j}=(i-1)oplus (j-1)+1$, 暴力数位$dp$即可

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl ‘
‘
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<‘,‘;hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<‘0‘||p>‘9‘)p=getchar();while(p>=‘0‘&&p<=‘9‘)x=x*10+p-‘0‘,p=getchar();return x;}
//head



//求 0<=i<=x, 0<=j<=y, 0<=i^j<=k的所有i^j的和
int f[33][2][2][2], g[33][2][2][2];
void add(int &a, ll b) {a=(a+b)%P;}
//f为个数, g为和
int calc(int x, int y, int k) {
	if (x<0||y<0) return 0;
	memset(f,0,sizeof f);
	memset(g,0,sizeof g);
	f[32][1][1][1] = 1;
	PER(i,0,31)REP(a1,0,1)REP(a2,0,1)REP(a3,0,1) { 
		int &r = f[i+1][a1][a2][a3];
		if (!r) continue;
		int l1=a1&&!(x>>i&1),l2=a2&&!(y>>i&1),l3=a3&&!(k>>i&1);
		REP(b1,0,1)REP(b2,0,1) {
			if (l1&&b1) continue;
			if (l2&&b2) continue;
			if (l3&&(b1^b2)) continue;
			int c1=a1&&b1>=(x>>i&1),c2=a2&&b2>=(y>>i&1),c3=a3&&(b1^b2)>=(k>>i&1);
			add(f[i][c1][c2][c3],r);
			add(g[i][c1][c2][c3],g[i+1][a1][a2][a3]*2ll+(b1^b2)*r);
		}
	}
	int ans = 0;
	REP(a1,0,1)REP(a2,0,1)REP(a3,0,1) add(ans,g[0][a1][a2][a3]+f[0][a1][a2][a3]);
	return ans;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int x1,y1,x2,y2,k;
		scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
		--x1,--y1,--x2,--y2,--x1,--y1,--k;
		int ans = (calc(x2,y2,k)-calc(x1,y2,k)-calc(x2,y1,k)+calc(x1,y1,k))%P;
		if (ans<0) ans += P;
		printf("%d
", ans);
	}
}

 

以上是关于Find a car CodeForces - 809C (数位DP)的主要内容,如果未能解决你的问题,请参考以下文章

CF809C Find a car

停车场找不到自己的车停在哪儿?教你开发一款“Find My Car”App

Music in Car CodeForces - 746F (贪心,模拟)

CodeForces - 1608A Find Array水题

[Codeforces 606C]Sorting Railway Cars

Codeforces 746F Music in Car