I can see the stamp face questions oh
It took an afternoon thorough understanding of the implementation BSGS, the feeling can be, in fact, did not think so hard before. . .
BSGS What is it? - can solve high-order congruence equation. concise.
If want to learn, then recommend the chiefs of the blog , speak of is still very clear.
This question is for us. . . . .
We can see by observing the nature of modulo, we can multiply the original formula with nine sides plus a
This is BSGS found the standard formula, as m guarantee is a prime number, so there is no need to expand the use BSGS.
We find that question head is going to blow the LL
Bigwigs will write _int128, had to learn a quick ride, la la la
Quick Take the code:
LL mul(LL a,LL b,LL p) { LL L=a*(b>>25LL)%p*(1LL<<25)%p; LL R=a*(b&((1LL<<25)-1))%p; return (L+R)%p; }
In this case, this question can be A, and happy.
#include<bits/stdc++.h> using namespace std; #define LL long long LL k,P; LL mul(LL a,LL b,LL p) { LL L=a*(b>>25LL)%p*(1LL<<25)%p; LL R=a*(b&((1LL<<25)-1))%p; return (L+R)%p; } LL ksm(LL x,LL y) { LL res=1; for (;y;y>>=1,x=1LL*mul(x,x,P)) if (y&1)res=1LL*mul(res,x,P); return res; } map<LL,LL> mp; int main() { scanf("%lld%lld",&k,&P); k=9*k+1;k%=P; LL m=ceil(sqrt(P));LL now=k; for (LL i=0;i<=m;i++) { mp[now]=i+1; now=mul(now,10,P); } now=1;LL ans=-1;LL kk=ksm(10,m); for (LL i=1;i<=m;i++) { now=1LL*mul(now,kk,P); if (mp[now]) {ans=i*m-mp[now]+1;break;} } printf("%lld",ans); return 0; }