题解:P16213 [ECUSTPC 2025] 十六号塔
审核题解,管理员辛苦了!
点击跳转题意
考虑小学数学,一个数模
那么我们不关注最后的数是多少,只关注所有数字的个数,已经会了
然后我们不难发现,
第
对于我这样的蒟蒻来说,过于困难,于是想到
然后才想到可以增广矩阵。
介绍一下,就是将每个点的贡献与数量也打到矩阵里,然后剩下的一个点放上之前变换的总和。
废话不多说,直接上矩阵。
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
:::info[详细解释]
左上角是正常求第
num[1]
num[2]
num[3]
num[4]
num[5]
num[6]
num[7]
num[8]
num[9]
0//做0次当然是0
然后就做完了
#include <bits/stdc++.h>
#define int long long
using namespace std;
int mod=9;
struct node
{
int n,m,mp[15][15];
node(int x,int y)
{
n=x,m=y;
memset(mp,0,sizeof(mp));
}
void init()
{
for(int i=1;i<=n;i++)
mp[i][i]=1;
}
node operator * (node&a)
{
node res(n,a.m);
for(int i=1;i<=n;i++)
for(int k=1;k<=m;k++)
for(int j=1;j<=a.m;j++)
res.mp[i][j]=(res.mp[i][j]+mp[i][k]*a.mp[k][j])%mod;
return res;
}
node sqqrt(node&a,int x)
{
node res(n,n);
res.init();
while(x>0)
{
if(x&1)
res=res*a;
a=a*a;
x>>=1;
}
return res;
}
};
signed main()
{
int t;
cin>>t;
while(t--)
{
node a(10,10);
node ans(10,1);
int n,m;
cin>>n>>m;
for(int i=1;i<=9;i++)
{
int x=i*i;
while(x)
{
int res=x%10;
if(res)
a.mp[res][i]++;
x/=10;
}
}
for(int i=1;i<=9;i++)
{
a.mp[10][i]=i%9;
}
a.mp[10][10]=1;
while(n)
{
int res=n%10;
if(res)
ans.mp[res][1]++;
n/=10;
}
ans.mp[10][1]=0;
a=a.sqqrt(a,m);
ans=a*ans;
int cnt=ans.mp[10][1];
for(int i=1;i<=9;i++)
{
cnt+=i*ans.mp[i][1]%mod,cnt%=mod;
}
cout<<cnt<<"\n";
}
}
看到这里,就点个赞吧!