跳到正文

题解

题解 ABC138F 【Coincidence】

ycyaw约 4 分钟
原文
文章信息与版本

文章信息

发布
2019年11月4日
收录
2026年7月18日
更新
2026年11月12日
篇幅
约 4 分钟
获赞
17

版本

  1. v1当前

传送门

题意:求L≤x≤y≤RL\leq x\leq y\leq R且满足y%x=y⊕xy\% x=y\oplus x的(x,y)(x,y)的对数。(1≤L≤R≤1018)(1\leq L\leq R\leq 10^{18})

y%xy\% x显然很不好做,我们需要转化一下。

我们发现:

1.1. 当2x≤y2x\leq y,有y−x>y%xy-x>y\% x;

2.2. 当2x>y2x>y,有y−x=y%xy-x=y\%x。

3.3. y⊕x≥y−xy\oplus x\geq y-x。

于是:

2x≤y2x\leq y时,不存在y⊕x=y%xy\oplus x=y\% x。

所以2x>y2x>y,即xx和yy的位数相同,最高位同时为11。

那么问题就转化成,求y−x=y⊕xy-x=y\oplus x的(x,y)(x,y)的对数。

满足y−x=y⊕xy-x=y\oplus x,那么yy二进制下为11,xx为00或11,yy二进制下为00,xx必为00。

考虑数位dpdp,这种类型的数位dpdp不像常规的数位dpdp,用0∼r0\sim r的答案减去0∼l−10\sim l-1的答案。

我们考虑枚举哪一位为最高位,然后dp[i][x1][x2]dp[i][x1][x2]表示前ii位,数的大小有没有达到下界LL,有没有达到上界RR的方案数,转移时先枚举yy,再枚举xx。

还有一道类似的题,做法是相同的。

Code Below:Code\ Below:

C++
#include<bits/stdc++.h>
#define ts cout<<"ok"<<endl
#define int long long
#define hh puts("")
#define pc putchar
#define mo 1000000007
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N=65;
int l,r,lenl,lenr,pl[N],pr[N],dp[N][2][2],ans;
inline int read(){
    int ret=0,ff=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') ff=-1;ch=getchar();}
    while(isdigit(ch)){ret=ret*10+(ch^48);ch=getchar();}
    return ret*ff;
}
void write(int x){if(x<0){x=-x,pc('-');}if(x>9) write(x/10);pc(x%10+48);}
void writeln(int x){write(x),hh;}
void writesp(int x){write(x),pc(' ');}
int dfs(int pos,int x1,int x2){
    if(!pos) return 1;
    if(dp[pos][x1][x2]!=-1) return dp[pos][x1][x2];
    int &res=dp[pos][x1][x2];res=0;
    int t1=x1?pl[pos]:0,t2=x2?pr[pos]:1;
    for(int y=t1;y<=t2;y++)
        for(int x=t1;x<=y;x++)
            res=(res+dfs(pos-1,x1&(x==t1),x2&(y==t2)))%mo;
    return res;
}
signed main(){
    memset(dp,-1,sizeof(dp));
    l=read(),r=read();
    while(l){
        pl[++lenl]=l&1;
        l>>=1;
    }
    while(r){
        pr[++lenr]=r&1;
        r>>=1;
    }
    for(int i=lenl;i<=lenr;i++) ans=(ans+dfs(i-1,i==lenl,i==lenr))%mo;//枚举哪个最高位为1 
    write(ans);
    return 0;
}