当前位置:网站首页>Atcoder regular contest 133 d.range XOR (digital dp+ classification discussion)
Atcoder regular contest 133 d.range XOR (digital dp+ classification discussion)
2022-07-28 17:07:00 【Code92007】
subject
Given L,R,V(1<=L<=R<=1e18,0<=V<=1e18)
Please L<=l<=r<=R, And (l,r) The exclusive or sum of numbers between is exactly equal to v Of (l,r) Number of two tuples ,
The answer is right 998244353 modulus
Source of ideas
YLWang Code
Answer key
heltion Of dp Unify all situations , But I can't understand orz, Just fill this
Official explanation , It's also digital dp+ Classification of discussion
be aware [0,x] There are only four prefix XOR values ,
①x%4==0 when , by x
②x%4==1 when , by 1
③x%4==2 when , by x+1
④x%4==3 when , by 0
[l,r] Exclusive or and , That is, prefix exclusive or and sum[r]^sum[l-1]
Then points l-1 model 4 Value ,r model 4 Value ,16 There are two situations to discuss
What is more difficult to deal with are the following four situations :
①(l-1)%4=0 And r%4=0
②(l-1)%4=0 And r%4=2
③(l-1)%4=2 And r%4=0
④(l-1)%4=2 And r%4=2
rest 12 In this case ,
Or you can determine the location of an endpoint ,
Or it's like an arithmetic sequence , Can be obtained quickly
this 4 In this case , It can be reduced to one problem ,
That is, given interval [x,y], Extract two unequal numbers from the interval lb,rb(lb<rb), seek lb^rb=w Binary logarithm of (lb,rb)
there x the truth is that l-1, and y Still r
therefore , Introduce digits dp,
dp[i][j][k] It means that there is still i position , The first number is / No card upper bound , Whether the second number is the number of schemes of the upper bound of the card
One dimensional digits dp, It's usually used dfs(r)-dfs(l-1), Prefix subtraction to find the answer
The same goes for two dimensions , Because it can only control the upper bound of card or not , So the problem of the lower bound of the card is left to Rong Xu ,
Similar to two-dimensional matrix solution (l,l) To (r,r) The area of , Subtract two rectangular areas from the total area , Add back the area of the rectangle in the upper left corner

Analogy shows that , The final two-dimensional answer is solve(r,r)-2*solve(l-1,r)+solve(l-1,l-1)
Due to the fact that l-1<r, So when v=0 when , You need to extract any equal number (l-1==r) Subtract the number of ,
And because of the number dp in ,i and j It's symmetrical , It doesn't matter , So the final answer is divided by 2
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10,mod=998244353,inv2=(mod+1)/2;
ll l,r,v;
int ans,a[66],b[66],dp[66][2][2];
void add(int &x,int y){
x+=y;
if(x>=mod)x-=mod;
if(x<0)x+=mod;
}
int dfs(int pos,bool lima,bool limb,ll my_v){
if(pos==-1){
return 1;
}
if(~dp[pos][lima][limb])return dp[pos][lima][limb];
int ret=0;
int upa=lima?a[pos]:1,upb=limb?b[pos]:1;
for(int i=0;i<=upa;++i){
for(int j=0;j<=upb;++j){
if(pos==0 && (i || j))continue;//l:(0,2) r:(0,2)
if(pos==1){
if((i^j) && (v%4!=3))continue;//l:0 r:2; l:2 r:0
if((i==j) && (v%4!=0))continue;//l:0 r:0; l:2 r:2
}
if((i^j) != (my_v>>pos&1))continue;
add(ret,dfs(pos-1,lima && i==upa,limb && j==upb,my_v));
}
}
dp[pos][lima][limb]=ret;
return ret;
}
int solve(ll x,ll y,ll v){
memset(a,0,sizeof a);
memset(b,0,sizeof b);
int sza=0,szb=0;
for(;x;x/=2)a[sza++]=x%2;
for(;y;y/=2)b[szb++]=y%2;
memset(dp,-1,sizeof dp);
return dfs(63,1,1,v);
}
int cal(ll lb,ll rb,int re){
if(rb<re || lb>rb)return 0;
ll nowl=0,nowr=0;
nowr=(rb-re)/4+1;
lb--;
if(lb<re)nowl=0;
else nowl=(lb-re)/4+1;
ll ans=nowr-nowl;
ans%=mod;
return (int)ans;
}
//[lb,rb] Internal extraction l<=r, bring (l^r)==v Logarithm of number , Special judgement lb=0
int cal2(ll lb,ll rb,ll v){
int ans=0;
ans=((1ll*solve(rb,rb,v)-2ll*solve(lb-1,rb,v)+solve(lb-1,lb-1,v))%mod+mod)%mod;
if(!v){//l=r illegal ,l It's practical l-1
ans=(ans-cal(lb,rb,0)+mod)%mod;
ans=(ans-cal(lb,rb,2)+mod)%mod;
}
ans=1ll*ans*inv2%mod;
return ans;
}
int main(){
scanf("%lld%lld%lld",&l,&r,&v);
if(v==0){
int three=cal(l-1,r,3);//l-1:3 r:3 C(x,2)
add(ans,1ll*(three-1)*three%mod*inv2%mod);
int one=cal(l-1,r,1);//l-1:1 r:1 C(x,2)
add(ans,1ll*(one-1)*one%mod*inv2%mod);
}
if(v==1){
int two=cal(l,r,2);
if(two){//l-1:1 r:3
ll ltwo=l;while(ltwo%4!=2)ltwo++;
int lthree=cal(ltwo,r,3);
ll rtwo=r;while(rtwo%4!=2)rtwo--;
int rthree=cal(rtwo,r,3);
add(ans,1ll*(lthree+rthree)*two%mod*inv2%mod);
}
int zero=cal(l,r,0);
if(zero){//l-1:3 r:1
ll lzero=l;while(lzero%4)lzero++;
int lone=cal(lzero,r,1);
ll rzero=r;while(rzero%4)rzero--;
int rone=cal(rzero,r,1);
add(ans,1ll*(lone+rone)*zero%mod*inv2%mod);
}
}
if(v%4==0){
if(l<=v+1 && v+1<=r){
add(ans,cal(v+1,r,3));//l-1:0 r:3
}
if(l<=v && v<=r){
add(ans,cal(l,v,0));//l-1:3 r:0
}
if(l>1)add(ans,cal2(l-1,r,v));//l-1:0 r:0 l-1:2 r:2
else add(ans,cal2(1,r,v)+(l<=v && v<=r));// Special judgement l-1=0, Split into two parts for statistics
}
if(v%4==1){
if(l<=v && v<=r){
add(ans,cal(v,r,1));//l-1:0 r:1
}
if(l<=(v^1) && (v^1)<=r){
add(ans,cal(l,v^1,2));//l-1:1 r:0
}
}
if(v%4==2){
if(l<=v && v<=r){
add(ans,cal(l,v,2));//l-1:1 r:2
}
if(l<=(v^1) && (v^1)<=r){
add(ans,cal(v^1,r,1));//l-1:2 r:1
}
}
if(v%4==3){
if(l<=v && v<=r){
add(ans,cal(v,r,3));//l-1:2 r:3
}
if(l<=(v^1) && (v^1)<=r){
add(ans,cal(l,v^1,0));//l-1:3 r:2
}
if(l>1)add(ans,cal2(l-1,r,v^1));//l-1:0 r:2;l-1:2 r:0
else add(ans,cal2(1,r,v^1)+(l<=(v^1) && (v^1)<=r));// Special judgement l-1=0, Split into two parts for statistics
}
printf("%d\n",ans);
return 0;
}边栏推荐
- Re13:读论文 Gender and Racial Stereotype Detection in Legal Opinion Word Embeddings
- Summary of kubenertes 1.16 cluster deployment problems
- Go language slow entry - process control statement
- Applet: scroll view slides to the bottom by default
- Comprehensively design an oppe homepage -- page service part
- Realize the reset function of steering wheel UI with touch rotation and finger departure in unity
- Realization of reflection and refraction effect in unity shader cube texture
- Easypoi --- excel file export
- 海康威视回应'美国禁令'影响:目前所使用的元器件都有备选
- 累计出货130亿颗Flash,4亿颗MCU!深度解析兆易创新的三大产品线
猜你喜欢

Ruoyi集成flyway后启动报错的解决方法
![[deep learning]: introduction to pytorch to project practice: simple code to realize linear neural network (with code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: introduction to pytorch to project practice: simple code to realize linear neural network (with code)

Re11: read EPM legal judgment prediction via event extraction with constraints

Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making

【深度学习】:《PyTorch入门到项目实战》第五天:从0到1实现Softmax回归(含源码)

Re12: read these3 semantic self segmentation for abstract summary of long legal documents in low

【深度学习】:《PyTorch入门到项目实战》第一天:数据操作和自动求导

关于 CMS 垃圾回收器,你真的懂了吗?

【深度学习】:《PyTorch入门到项目实战》:简洁代码实现线性神经网络(附代码)

Jsonarray traversal
随机推荐
TCP handshake, waving, time wait connection reset and other records
Ugui learning notes (VI) get the information of the clicked UI
In 2020q2, shipments in the global tablet market soared by 26.1%: Huawei ranked third and Lenovo increased the most!
Question note 4 (the first wrong version, search the insertion position)
Question making note 2 (add two numbers)
Re10: are we really making much progress? Revisiting, benchmarking, and refining heterogeneous gr
Re12:读论文 Se3 Semantic Self-segmentation for Abstractive Summarization of Long Legal Documents in Low
Epoll horizontal departure, which edge triggers
Hikvision responded to the impact of the 'US ban': there are options for the components currently used
Re11: read EPM legal judgment prediction via event extraction with constraints
充分利用----英文
结构化设计的概要与原理--模块化
Jsonarray traversal
[deep learning]: model evaluation and selection on the seventh day of pytorch introduction to project practice (Part 1): under fitting and over fitting (including source code)
epoll水平出发何边沿触发
Best cow fences solution
Outline and principle of structured design -- modularization
华为Mate 40系列曝光:大曲率双曲面屏,5nm麒麟1020处理器!还将有天玑1000+的版本
向高通支付18亿美元专利费之后,传华为向联发科订购了1.2亿颗芯片!官方回应
传英伟达已与软银展开会谈,将出价超过320亿美元收购Arm