【题解】多少个(1)(exBSGS)
解方程:
[underbrace {1dots1}_{n}equiv k mod m
]
其实就是
[dfrac {10^n-1} {9}equiv k mod m
]
就是
[10^nequiv 9k+1 mod m
]
直接exBSGS【总结】皇冠上的明珠——初等数论初步
//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std; typedef long long ll;
inline ll qr(){
register ll ret=0,f=0;
register char c=getchar();
while(c<48||c>57)f|=c==45,c=getchar();
while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
return f?-ret:ret;
}
map < ll , ll > s;
inline ll exBSGS(ll a,ll b,ll m){
ll AD=1,d;
ll cnt=0;
while(d=__gcd(a,m),d!=1) {
b/=d,m/=d,AD=AD*(a/d),++cnt;
if(AD==b) return cnt;
}
ll sq=sqrt(m)+1,ret=1;
map < ll ,ll >().swap(s);
for(ll t=0;t<sq;++t,ret=(__int128)ret*a%m)
s[(__int128)ret*b%m]=t;
for(ll t=1,w=AD*ret;t<=sq;++t,w=(__int128)w*ret%m)
if(s.find(w)!=s.end())
return cnt+t*sq-s[w];
return -1;
}
int main(){
ll k=qr(),m=qr();
printf("%lld
",exBSGS(10,9ll*k+1,m));
return 0;
}