题解:P16213 [ECUSTPC 2025] 十六号塔

· · 题解

P16213 十六号塔 题解

:::info[题意] 给定初始数字 n,每次操作将当前数字的每一位替换为该位数字的平方,然后将所有结果按原顺序拼接成新数字。求经过 m 次操作后,得到的共 m+1 个数字之和对 9 取模的结果。

::: :::::info[题解]{open} 模 $9$ 有一个性质,一个数模 $9$ 就等于它各位数字之和模 $9$。所以我们只需要知道每个数字一共出现了多少次。 数字之间有递推关系,$n$ 较大,不难想到矩乘。根据数字平方之后的关系,可以构造出这样一个转移矩阵: :::align{center} $\begin{bmatrix} 1 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 0 \\\end{bmatrix}

:::

解释一下,第 i 行第 j 列表示数字 j 平方后会产生多少个 i。如 (8,9) 表示 9^2=81,出现了 8。初始矩阵是每个数字在 n 里出现的次数。

但是这个矩阵转移只能得出最后一个数里每个数字出现多少次。但我们在矩阵乘法中希望同时完成数字个数的转移和累加当前值。考虑在初始矩阵里再加一位表示到上一个版本为止的答案。(当前版本的出现次数刚计算,肯定没法加到和里。)

构造出这样的东西:

:::align{center}

\begin{bmatrix} M & 0 \\ c & 1 \end{bmatrix} \begin{bmatrix} x^{(t)} \\ S_{t-1} \end{bmatrix}

:::

考虑取答案。初始矩阵是 $0$ 版本,存了 $s_{-1}$。乘 $m+1$ 次转移矩阵便可得到 $s_m$。 ::::warning[如果你没看懂] 这里具体列出转移矩阵和初始矩阵的样子。 转移矩阵: :::align{center} $\begin{bmatrix} 1 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 0 & 1 & 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\ 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 0 & 1 \\\end{bmatrix}

:::

初始矩阵:

:::align{center}

cnt_1\\ cnt_2\\ \vdots\\ cnt_9\\ 0 \end{bmatrix}

:::

由于我们忽略了数码 0,基础转移矩阵 M 的大小为 9\times 9。新矩阵 A 的大小为 10\times 10,其左上角为 M,最后一行是权重,最后一列是 (0,\dots,0,1)。
时间复杂度:O(T \cdot K^3 \log m),其中 K=10 为矩阵边长。

:::info[代码]

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=9;
const int maxn=1e5+5;
int T,sum;
struct node{
    int n,m,mp[15][15];
    node(int x,int y){
        n=x,m=y;
        memset(mp,0,sizeof(mp));
    }
    node operator*(node a){
        int z=a.m;
        node res(n,z);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=z;j++){
                for(int k=1;k<=m;k++){
                    res.mp[i][j]=(res.mp[i][j]+1LL*mp[i][k]%mod*a.mp[k][j]%mod)%mod;
                }
            }
        }
        return res;
    }
};
void init(node &p){
    for(int i=1;i<=p.n;i++){
        p.mp[i][i]=1;
    }
}
node qpow(node a,int b){
    node res(10,10);
    init(res);
    while(b){
        if(b&1) res=res*a;
        a=a*a;
        b>>=1;
    }
    return res;
}
signed main(){
    cin>>T;
    while(T--){
        int n,m;
        cin>>n>>m;
        node a(10,10),ans(10,1);
        int p=n;
        while(p){
            if(p%10!=0) ans.mp[p%10][1]++;
            p/=10;
        }
        for(int i=1;i<=9;i++){
            int res=i*i;
            while(res){
                if(res%10!=0) a.mp[res%10][i]++;
                res/=10;
            }
        }
        for(int i=1;i<=9;i++) a.mp[10][i]=i%9;
        a.mp[10][10]=1;
        ans=qpow(a,m+1)*ans;
        cout<<ans.mp[10][1]<<endl;
    }
    return 0;
}
不得不说,矩阵真的很容易构造错。