[CF1245F]Daniel and Spring Cleaning

题目

传送门

题解

数位 \(DP\) 的典型题,记

\[f(x,y)=\sum_{i=0}^x\sum_{j=0}^y[i+j=i\oplus j] \]

显然有 \(f(x,y)=f(y,x)\),答案为 \(f(r,r)-f(l-1,r)-f(r,l-1)+f(l-1,l-1)\),由于前一条件,答案转换为 \(f(r,r)-2\times f(l-1,r)+f(l-1,l-1)\).

现在问题为如何计算 \(f(x,y)\).

我们考虑在什么情况下 \(i+1=i\oplus j\),对于 \(i,j\) 的二进制形式 \(m,n\),如果 \(m_t\)\(n_t\) 不同是为 \(1\),那么 \(m_t+n_t=m_t\oplus n_t\),那么我们只需要统计有多少对 \((i,j)\)\(i,j\) 的每一位不同时为 \(1\) 即可。

考虑设计函数 dfs(pos,rl1,rl2) 表示 \(i,j\) 填到第 pos 位,\(i\) 是否触及上界,\(j\) 是否触及上界,首先我们需要保证 \(i,j\) 的第 pos 位不能同时为 \(1\),而下一状态十分简单,不作过多赘述。

此题最需学习的,是 \(i,j\) 是同时进行填数这样的数位 \(DP\),而之前只以为数位 \(DP\) 只能对于单个数进行填数,这是需要注意的。

代码

#include<cstdio>
#include<cmath>
#include<cstring>

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
typedef long long LL;
// typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int maxl=40;

int l,r;

inline void Init(){l=read(1),r=read(1);}

LL f[maxl+5][2][2];

int l1[maxl+5],l2[maxl+5],len1,len2;

LL Dfs(const int pos,const int rl1,const int rl2){
    if(pos==0)return 1;
    LL& ret=f[pos][rl1][rl2];
    if(~ret)return ret;else ret=0;
    int up1=rl1?l1[pos]:1;
    int up2=rl2?l2[pos]:1;
    rep(i,0,up1)rep(j,0,up2)if(!(i&j))
        ret+=Dfs(pos-1,rl1&&(i==up1),rl2&&(j==up2));
    return ret;
}

inline LL calc(int x1,int x2){
    // printf("When x1 == %d, x2 == %d\n",x1,x2);
    if(x1<0)return 0;
    memset(f,-1,sizeof f);
    len1=len2=0;
    memset(l1,0,sizeof l1);
    memset(l2,0,sizeof l2);
    while(x1)l1[++len1]=x1&1,x1>>=1;
    while(x2)l2[++len2]=x2&1,x2>>=1;
    // printf("result == %lld\n",Dfs(len2,1,1));
    return Dfs(len2,1,1);
}

inline LL Get_ans(){
    return calc(r,r)-(calc(l-1,r)<<1)+calc(l-1,l-1);
}

signed main(){
    rep(i,1,read(1)){
        Init();
        writc(Get_ans(),'\n');
    }
    return 0;
}
posted @ 2020-08-11 22:10  Arextre  阅读(149)  评论(0编辑  收藏  举报