1. 程式人生 > 實用技巧 >[CF1245F]Daniel and Spring Cleaning

[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;
}