# [洛谷4884]多少个1？

$\dfrac{10^{n+1}-1}{9}\equiv k(\%m)\\10^{n+1}\equiv9k+1(\%m)$

/*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;
}
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;
}
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(){
K=(9*K+1)%m;
printf("%lld\n",Math::BSGS(10,K,m));
return 0;
}
posted @ 2019-03-31 08:47 Wolfycz 阅读(...) 评论(...) 编辑 收藏