题目传送门:https://www.luogu.org/problemnew/show/P4884
题目大意:
求1111...(n个1) mod m=k的最小的n
假定有n个1,那么1111...可以写成(sumlimits_{i=0}^n10^i=dfrac{10^{n+1}-1}{9}),然后就可以推一波式子
[dfrac{10^{n+1}-1}{9}equiv k(\%m)\10^{n+1}equiv9k+1(\%m)
]
然后直接BSGS求解即可
/*program from Wolfycz*/
#include<map>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
#define Fi first
#define Se second
typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;
template<typename T>inline T min(T x,T y){return x<y?x:y;}
template<typename T>inline T max(T x,T y){return x>y?x:y;}
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline T frd(T x){
int f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
template<typename T>inline T read(T x){
int f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
namespace Math{
using std::map;
map<ll,int>Mp;
ll mlt(ll _a,ll _b,ll _p){
ll _c=(ld)_a*_b/_p;
ll _Ans=_a*_b-_c*_p;
if (_Ans<0) _Ans+=_p;
return _Ans;
}
ll power(ll a,ll b,ll p){
ll res=1;
for (;b;b>>=1,a=mlt(a,a,p)) if (b&1) res=mlt(res,a,p);
return res;
}
ll BSGS(ll y,ll z,ll p){
int lmt=(ll)sqrt(p)+1; ll tmp=z;
Mp.insert(map<ll,int>::value_type(tmp,0));
for (int i=1;i<=lmt;i++){
tmp=mlt(tmp,y,p);
map<ll,int>::iterator it=Mp.find(tmp);
if (it==Mp.end()) it=Mp.insert(map<ll,int>::value_type(tmp,i)).Fi;
it->Se=i;
}
tmp=1; ll K=power(y,lmt,p);
for (ll i=1;i*lmt<p;i++){
tmp=mlt(K,tmp,p);
map<ll,int>::iterator it=Mp.find(tmp);
if (it!=Mp.end()) return i*lmt-it->Se;
}
return -1;
}
}
int main(){
ll K=read(0ll),m=read(0ll);
K=(9*K+1)%m;
printf("%lld
",Math::BSGS(10,K,m));
return 0;
}