[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;
}
posted @ 2020-08-08 21:36  Arextre  阅读(405)  评论(0编辑  收藏  举报