Luogu 9月月赛A(BSGS)

  11……1(n个)=99……9(n个)/9=(10n-1)/9。

  那么显然就是求离散对数了,BSGS即可。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define ll long long
ll read()
{
    ll x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
ll k,m,block,a[1000000];
map<ll,ll> f;
ll ksc(ll a,ll b)
{
    ll t=a*b-(ll)((long double)a*b/m+0.5)*m;
    return t<0?t+m:t;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    k=read(),m=read();
    k=(k*9+1)%m;
    block=sqrt(m);f[a[0]=k]=1;ll s=1;
    for (int i=1;i<=block;i++) f[a[i]=a[i-1]*10%m]=i+1,s=s*10%m;
    ll t=s;
    for (ll i=block;i<=m+block;i+=block,s=ksc(s,t))
    if (f[s]) {cout<<i-f[s]+1;return 0;}
}

猜你喜欢

转载自www.cnblogs.com/Gloid/p/9656632.html