1. 程式人生 > 實用技巧 >[CF1073E]Segment Sum

[CF1073E]Segment Sum

題目

傳送門

題解

一道 記憶化搜尋 用狀壓配合數位 \(DP\) 的題。

首先將”求區間 \([l,r]\) 的和“轉化為”求區間 \([1,i]\) 的和“,記 \([1,i]\) 的合法數字和為 \(f(i)\),那麼答案就是 \(f(r)-f(l-1)\),這是數位 \(DP\) 的經典操作,不作過多解釋。

考慮 dfs(pos,s,pz,rl) 為填數到第 \(pos\) 位(最低位保證 \(pos=1\)),數字出現情況為 \(s\)(二進位制串),是否前導零,是否觸及上界的狀態,考慮 \(f[pos][s][pz][rl]\) 為這個狀態的合法後續數字的和,考慮怎麼從 \(f[pos-1]\)

處算出 \(f[pos]\) 的值。

假設第 \(pos\) 位填的是 \(x\),那麼其對答案的貢獻應該為 \(x\times 10^{pos-1}\times cnt\),其中 \(cnt\) 是之後狀態的合法數字的個數,但是如果我們只有 \(f\) 似乎是無法同時記錄這個 \(cnt\) 的。

考慮多一個數組記錄 \(cnt\),設 \(g[pos][s][pz][rl]\) 為狀態 dfs(pos,s,pz,rl) 的合法後續的數字個數,那麼 \(g[pos]\) 很好從 \(g[pos-1]\) 處轉移,有

\[g[pos][s][pz][rl]=\sum g[pos-1][s'][pz'][rl'] \]

似乎什麼都沒寫 其中,\(s'\) 指數字出現的狀態 \(s\)\(x\) 填入後變成的新狀態,\(pz'\)\(x\) 在填入後是否仍然存在前導零,\(rl'\)\(x\) 填入後是否仍然觸及上界

\(f[pos]\) 的轉移和這個差不多,有

\[f[pos][s][pz][rl]=\sum f[pos-1][s'][pz'][rl']+x\times 10^{pos-1}\times g[pos-1][s'][pz'][rl'] \]

對於臨界狀態 \(f[0]\)\(g[0]\),顯然有 \(\forall f[0]=0\),對於 \(g[0]\),記 \(\text{bitcnt}(x)\)

\(x\) 二進位制下有多少個 \(1\),如果 \(\text{bitcnt}(s)\le k\),有 \(g[0][s][pz][rl]=1\),否則有 \(g[0][s][pz][rl]=0\).

程式碼

#include<cstdio>
#include<utility>
#include<cstring>
using namespace std;

#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;
#define ft first
#define sd second
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=18;
const int ALL=(1<<10)-1;
//0~9, 故要開 2^10-1
const int MOD=998244353;

LL l,r;int k;

int bitcnt[ALL+5];

int a[MAXL+5],pow10[MAXL+5];

inline void Init(){
    l=read(1ll),r=read(1ll),k=read(1);
    rep(i,1,ALL)bitcnt[i]=bitcnt[i>>1]+(i&1);
    pow10[0]=1;
    rep(i,1,MAXL)pow10[i]=1ll*pow10[i-1]*10%MOD;
}

int f[MAXL+5][ALL+5][2][2];
int g[MAXL+5][ALL+5][2][2];
/*
f.ft : 和
f.sd : 個數
*/

inline int Plus(int& a,const int delta){
    a+=delta;
    if(a>=MOD)a-=MOD;
    return a;
}

void dfs(const int pos,const int s,const int pz,const int rl){
//pz : prezero
//rl : reachlim
    // printf("dfs:>pos == %d, s == %d, pz == %d, rl == %d\n",pos,s,pz,rl);
    if(pos==0){
        g[pos][s][pz][rl]=bitcnt[s]<=k;
        f[pos][s][pz][rl]=0;
        return;
    }if(~g[pos][s][pz][rl])return;
    int up=(rl)?a[pos]:9,ns;
    f[pos][s][pz][rl]=g[pos][s][pz][rl]=0;
    rep(i,0,up){
        ns=(pz&&i==0)?s:(s|(1<<i));
        dfs(pos-1,ns,pz&&i==0,rl&&i==up);
        int _f=f[pos-1][ns][pz&&i==0][rl&&i==up];
        int _g=g[pos-1][ns][pz&&i==0][rl&&i==up];
        Plus(g[pos][s][pz][rl],_g);
        Plus(f[pos][s][pz][rl],(1ll*i*pow10[pos-1]%MOD*_g%MOD+_f)%MOD);
        //注意 pow10 的下標
    }return;
}

inline int solve(LL lim){
    memset(f,-1,sizeof f);
    memset(g,-1,sizeof g);
    memset(a,0,sizeof a);
    int len=0;
    while(lim){
        a[++len]=lim%10;
        lim/=10;
        // printf("a[%d] == %d\n",len,a[len]);
    }
    dfs(len,0,1,1);
    return f[len][0][1][1];
}

signed main(){
    Init();
    // printf("solve(%lld) == %d\n",r,solve(r));
    // printf("solve(%lld) == %d\n",l-1,solve(l-1));
    // printf("solve(%lld) - solve(%lld) == %d\n",r,l-1,solve(r)-solve(l-1));
    writc((solve(r)-solve(l-1)+MOD)%MOD,'\n');
    return 0;
}