Codeforces 1245F Daniel and Spring Cleaning(数位DP)

题目链接:Daniel and Spring Cleaning

题意:求区间[l,r]内(0<=l<=r<=10^9)a xor b== a+b的(a,b)对数

题解:我们化简下题意可以发现就是求区间内"&"(and)和为0的(x,y)数量,用[L,R]~[L,R]表示x属于第一个区间,y属于第二个区间的合法方案数,那么可以得出以下结论[L,R]~[L,R]=([0,R]~[0,R])+([0,L-1]~[0,L-1])-2*([0,L-1]~[0,R]),然后我们就化成了按位运算的数位DP了,我们在数位DP枚举x,y各二进制位上取值情况,当二者这一位"&"(and)和为0时为合法状态,dp[len][fl][fr]表示当前枚举第len位时,x,y取值是否处于边界

细节和不懂的可以看代码注释,写的很详细:

#include<iostream>
#include<stack>
#include<list>
#include<set>
#include<vector>
#include<algorithm>
#include<math.h>
#include<numeric>
#include<map>
#include<cstring>
#include<queue>
#include<iomanip>
#include<cmath>
#include<queue>
#include <bitset>
#include<unordered_map>
	#ifndef local
	#define endl '\n'
#endif */
using namespace std;
using std::bitset;
typedef long long ll;
typedef long double ld;
const int inf=0x3f3f3f3f;
const ll MAXN=1e6+10;
const ll N=1e5+100;
const ll mod=998244353;
const ll hash_p1=1610612741;
const ll hash_p2=805306457;
const ll hash_p3=402653189;
//-----------------------------------------------------------------------------------------------------------------*/
// ll head[MAXN],net[MAXN],to[MAXN],edge[MAXN]/*流量*/,cost[MAXN]//费用;
/* 
void add(ll u,ll v,ll w,ll s){
	to[++cnt]=v;net[cnt]=head[u];edge[cnt]=w;cost[cnt]=s;head[u]=cnt;
	to[++cnt]=u;net[cnt]=head[v];edge[cnt]=0;cost[cnt]=-s;head[v]=cnt;
}
struct elemt{
	int p,v;
};
struct comp{
	public:
		bool operator()(elemt v1,elemt v2){
			return v1.v<v2.v;
		}
};
priority_queue<elemt,vector<elemt>,comp>q;  
*/
//-----------------------------------------------------------------------------------------------------------------*/
 //	unordered_map<int,int>mp;
ll dp[100][2][2];//枚举第i位,左右边界是否处在边界对应答案数(记忆化)
int a[100];
int c[100];
int len1,len2;//左右边界对应进制下长度
ll dfs(ll b/*进制*/,ll len/*当前枚举第几位*/,int fl/*左边界是否在边界*/,int fr/*右边界是否在边界*/){
	if(!len){
		return 1;//成功枚举到最后,成功一种方案 
	}
	if(dp[len][fl][fr]!=-1){
		return dp[len][fl][fr];
	}
	//-------------确定上下界
	ll res=0;
	int top1=1,top2=1;
	if(fl){
		top1=a[len];
	}
	if(fr){
		top2=c[len];
	}
	//-------------确定上下界
	for(int i=0;i<=top1;i++){
		for(int j=0;j<=top2;j++){
			if(!(i&j)){
				res+=dfs(b,len-1,fl&&(i==a[len]),fr&&(j==c[len]));
			}
		}
	}
	dp[len][fl][fr]=res;
	return res;
}
ll cal(ll b,ll l,ll r){//进制,左右上界
	if(l>r){
		swap(l,r);
	}
	if(l<0){
		return 0;
	}
	memset(dp,-1,sizeof(dp));//这里因为上下界不一样,不能重复利用
	memset(a,0,sizeof(a));
	memset(c,0,sizeof(c));
	int res=0;
	while(l){//取出L各位上的数
		c[++res]=l%b;
		l/=b;
	}
	len1=res;
	res=0;
	while(r){//取出R各位上的数
		a[++res]=r%b;
		r/=b;
	}
	len2=res;
	return dfs(b,res,1,1);
}
int main(){
/*cout<<setiosflags(ios::fixed)<<setprecision(8)<<ans<<endl;//输出ans(float)格式控制为8位小数(不含整数部分)*/
/*cout<<setprecision(8)<<ans<<endl;//输出ans(float)格式控制为8位小数(含整数部分)*/
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//同步流
memset(dp,-1,sizeof(dp));
int t;
cin>>t;
while(t--){
	ll b=2,l,r;
	cin>>l>>r;
	cout<<cal(b,r,r)+cal(b,l-1,l-1)-2*cal(b,l-1,r)<<endl;
	//区间[L,R]<->[L,R]内合法点对数=([0,R]<->[0,R])+([0,L-1]<->[0,L-1])-2*([0,L-1]<->[0,R])
}
	return 0;
}
//求区间[l,r]内异或和为0的(x,y)数量

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值