[ARC050C] LCM 111

发布时间 2023-10-04 21:14:29作者: SError

[ARC050C] LCM 111

给定三个数 \(a,b,P\),令 \(x\)\(a\)\(1\) 拼接而成,\(y\)\(b\)\(1\) 拼接而成,求 \(\operatorname{lcm}(x,y)\)\(P\) 的值。

\(1\le a,b\le 10^{18}\)\(2\le P\le 10^9\).


\(\displaystyle x=\frac{10^a-1}{9},y=\frac{10^b-1}{9}\),不妨先求 \(\gcd(10^a-1,10^b-1)\).

  • \(\gcd(x^n-1,x^m-1)=x^{\gcd(n,m)}-1\).

考虑辗转相除:

\[\gcd(x^n-1,x^m-1)=\gcd(x^n-x^m,x^m-1) \]

\[=\gcd(x^m(x^{n-m}-1),x^m-1) \]

由于 \(x^m\perp x^m-1\)

\[\gcd(x^n-1,x^m-1)=x^{\gcd(n,m)}-1 \]

答案即

\[\frac{(10^a-1)(10^b-1)}{9\cdot(10^{\gcd(a,b)}-1)} \]

方便地,令 \(c\leftarrow\gcd(a,b)\)\(a\leftarrow \frac{a}{c}\)\(b\leftarrow \frac{b}{c}\).

把答案变为

\[\frac{10^c-1}{9}\cdot\frac{10^{ac}-1}{10^c-1}\cdot\frac{10^{bc}-1}{10^c-1} \]

  • \(\displaystyle 10^{ac}-1=(10^c-1)(\sum_{i=0}^{a-1}10^{ic})\).

考虑把这个 \(ac\) 位数分成 \(a\) 个大小为 \(c\) 的部分,那么它们最终都是全为 \(9\) 的块,可得上式。

即求

\[(\sum_{i=0}^{c-1}10^i)\cdot(\sum_{i=0}^{a-1}10^{ic})\cdot(\sum_{i=0}^{b-1}10^{ic}) \]

三个式子均可分治得出。这样就不需要处理麻烦的逆元了。

//by Shui_Dream
#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL a,b,c,P;
LL Pow(LL x,LL kx){
	LL y=1;
	while(kx){
		if(kx & 1) y=y*x%P;
		kx>>=1; x=x*x%P;
	}
	return y;
}
LL s1(LL n,LL c){
	if(!n) return 1;
	LL bf=s1(n>>1,c); bf=(bf+Pow(Pow(10,(n>>1)),c)*(bf-1+P))%P;
	if(n & 1) (bf+=Pow(Pow(10,n),c))%=P;
	return bf;
}
LL s2(LL n){
	if(!n) return 1;
	LL bf=s2(n>>1); bf=(bf+Pow(10,n>>1)*(bf-1+P)%P)%P;
	if(n & 1) (bf+=Pow(10,n))%=P;
	return bf;
}
int main(){
	scanf("%lld%lld%lld",&a,&b,&P); c=__gcd(a,b); a/=c; b/=c;
	LL res=s1(a-1,c)*s1(b-1,c)%P*s2(c-1)%P;
	printf("%lld",res);
	return 0;
}