CF331C3 The Great Julya Calendar

· · 题解

题意:

定义对一个数 x 的一次操作为选出它十进制下中的某一位数字 y,令 x 变为 x-y

给定一个数 n,求最小的操作数使得 n 变为 0

思路:

先考虑暴力 \mathrm{dp},设 f_{i} 表示 i 所需要的操作数,有转移 f_i=1+\min{f_{i-x}},其中 x 为 i 的某一位数字

可以发现一个结论:每一次操作一定是选择最大的数位,证明可以看另一篇题解

这个暴力转移只能处理 $n\le 10^6$ 的情况,但是注意到 $1\le Dmax(i)\le 9$,范围很小,考虑使用矩阵优化转移 设 $(x,y)=[x\not =y]?0:1$,$x=Dmax(i+1)$,有转移: $$ \begin{aligned} &\begin{bmatrix} (1,x)& (2,x)& (3,x) & (4,x) & (5,x) & (6,x) & (7,x) & (8,x) & (9,x) &1\\ 1& 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0& 1& 0& 1& 0 & 1 & 0 & 1 & 0 & 0\\ 0& 0& 1& 0& 0& 0& 0& 0& 0&0 \\ 0& 0& 0& 1& 0& 0& 0& 0& 0&0 \\ 0& 0& 0& 0& 1& 0& 0& 0& 0&0 \\ 0& 0& 0& 0& 0& 1& 0& 0& 0&0 \\ 0& 0& 0& 0& 0& 0& 1& 0& 0&0 \\ 0& 0& 0& 0& 0& 0& 0& 1& 0&0 \\ 0& 0& 0& 0& 0& 0& 0& 0& 0&1 \\ \end{bmatrix} \times \begin{bmatrix} f_{i}\\ f_{i-1}\\ f_{i-2}\\ f_{i-3}\\ f_{i-4}\\ f_{i-5}\\ f_{i-6}\\ f_{i-7}\\ f_{i-8}\\ 1 \end{bmatrix} = \begin{bmatrix} f_{i+1}\\ f_{i}\\ f_{i-1}\\ f_{i-2}\\ f_{i-3}\\ f_{i-4}\\ f_{i-5}\\ f_{i-6}\\ f_{i-7}\\ 1 \end{bmatrix} \end{aligned} $$ 设 $T_{i+1}$ 为从第 $i$ 个列矩阵转移到第 $i+1$ 个列矩阵的转移矩阵,则第 $n$ 个列矩阵为 $T_n\times \cdots \times T_{10}\times F_9=F_n

注意到转移矩阵实质上只有 9 个,且只跟数位最大值有关,可以联想到数位 \mathrm{dp}

虽然矩阵乘法没有交换率,但是数位 \mathrm{dp} 其实就是从小到大进行的,两者相契合

设 g_{i,j} 表示现在数位 \mathrm{dp} 到了第 i 位,此时数位最大值为 j,可以得到转移:g_{i,j}=\prod_{i=1}^{ed}g_{i-1,\max(j,i)},ed 表示该位能填的上界

直接 \mathrm{dfs} 求出 g 最后乘上初始列矩阵就可以得到答案,注意转移矩阵不包括 T_1 到 T_9,同时 n\le 9 要特判,细节可以看代码

时间复杂度为 O(10^3\log n\times 9\times 9)

**代码**: ```cpp #include<cstdio> #include<algorithm> #include<cstring> #include<cassert> #define int long long const int Q=25; const int INF=(1<<30); typedef long long ll; #define rg register int #define cint const register int //char Buf[1<<21],*P1=Buf,*P2=Buf; //#define gc() (P1==P2&&(P2=(P1=Buf)+fread(Buf,1,1<<21,stdin),P1==P2)?EOF:*P1++) #define gc() getchar() inline bool ig(const char c){return c>=48&&c<=57;} inline void read(int &oi){char c;int f=1,res=0;while(c=gc(),(!ig(c))&&c^'-');c^'-'?res=(c^48):f=-1;while(c=gc(),ig(c))res=res*10+(c^48);oi=f*res;} inline void print(int oi){char io[23];int l=0;if(oi<0)putchar('-'),oi=~oi+1;do io[++l]=(oi%10+48);while(oi/=10);for(;l;--l)putchar(io[l]);} inline void write(cint oi,const char c){print(oi);putchar(c);} inline int max(cint x,cint y){return x>y?x:y;} const int Lim=10; struct mat{ int a[Lim][Lim];mat()=default; mat(cint X){for(rg i=0;i<Lim;++i)for(rg j=0;j<Lim;++j)a[i][j]=0;for(rg i=0;i<Lim;++i)a[i][i]=X;} inline mat operator*(const mat&rhs)const{ mat res;for(rg i=0;i<Lim;++i)for(rg j=0;j<Lim;++j)res.a[i][j]=0; for(rg i=0;i<Lim;++i)for(rg k=0;k<Lim;++k)for(rg j=0;j<Lim;++j)res.a[i][j]+=a[i][k]*rhs.a[k][j]; return res; } }; mat f[Q][Q],st; bool vst[Q][Q]; int a[Q],len; inline mat set(cint vl){ mat res;for(rg i=0;i<Lim;++i)for(rg j=0;j<Lim;++j)res.a[i][j]=0; for(rg i=1;i<Lim-1;++i)res.a[i][i-1]=1; res.a[Lim-1][Lim-1]=1;res.a[0][Lim-1]=1;res.a[0][vl-1]=1;return res; } inline mat dfs(cint len,cint mx,cint lim,cint zr){ if(len==0)return set(mx); if(!lim&&vst[len][mx])return f[len][mx]; f[len][mx]=mat(1);vst[len][mx]=1;cint ed=lim?a[len]:9; for(rg i=0;i<=ed;++i)f[len][mx]=dfs(len-1,max(mx,i),lim&&(i==ed),zr&&(!i))*f[len][mx]; return (len==1&&zr)?mat(1):f[len][mx]; } inline void solve(cint x){ if(x<=9)return write(x==0?0:1,'\n'); for(rg t=x;t;a[++len]=t%10,t/=10); mat res=dfs(len,0,1,1); for(rg i=0;i<Lim;++i)for(rg j=0;j<Lim;++j)st.a[i][j]=0; for(rg i=0;i<Lim;++i)st.a[i][0]=1; write((res*st).a[0][0],'\n'); } int n; signed main(){ read(n);solve(n); return 0; } ```