AT_arc050_c
Gaochenxi103_QAQ · · 题解
哇,也是写上 ARC 的题目了,献祭某个隔壁机位 hjy 的阳寿也是一遍过好吧。
题目也是十分简洁,看到
算了,还是好好的推一下吧,免得后来的 OIer 看到这篇题解大骂作者。
(以上废话)
对于
发现
那么我们就可以得到这题的核心推导式。
所以。
因此答案就是,
对于
有聪明的人就要问了,
不清楚的同学可以了解一下矩阵快速幂,这是模板题链接,体面上有对其详细的介绍。
然后我们设计出一个矩阵。
因此对于求
对于
说人话就是,
再对于
维护整个式子同样可以用矩阵快速幂。
合理,先对
整体代码。
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a,b,mod;
struct M{
int a[105][105];
int x,y;
M()
{
memset(a,0,sizeof a);
x=y=0;
}
};
M cf(M A,M B)
{
M res;
res.x=A.x;
res.y=B.y;
for(int i=1;i<=A.x;i++)
{
for(int j=1;j<=B.y;j++)
{
for(int k=1;k<=A.y;k++)
{
res.a[i][j]+=A.a[i][k]*B.a[k][j];
res.a[i][j]%=mod;
}
}
}
return res;
}
M LVM_J(M x,int k)
{
M cnt=x;
M res=x;
k--;
while(k)
{
if(k&1)res=cf(res,cnt);
k>>=1;
cnt=cf(cnt,cnt);
}
return res;
}
int LVM(int x,int k)
{
int cnt=x;
int res=x;
k--;
while(k)
{
if(k&1)res=(res*cnt)%mod;
k>>=1;
cnt=(cnt*cnt)%mod;
}
return res;
}
M f(int k)//鐢熸垚{{10^k,1},{0,1}}鐨勭煩闃?
{
M res;
res.x=res.y=2;
res.a[1][1]=LVM(10,k);
res.a[1][2]=1;
res.a[2][1]=0;
res.a[2][2]=1;
return res;
}
M g()
{
M res;
res.x=1;
res.y=2;
res.a[1][1]=0;
res.a[2][1]=1;
return res;
}
signed main()
{
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
cin>>a>>b>>mod;
cout<<(cf(LVM_J(f(1LL),a),g()).a[1][1]*cf(LVM_J(f(gcd(a,b)),b/gcd(a,b)),g()).a[1][1])%mod;
return 0;
}