可爱袜子刚学 bsgs, 0 pt 求助
查看原帖
可爱袜子刚学 bsgs, 0 pt 求助
749665
huta0楼主2024/8/14 16:48
#include <iostream>
#include <vector>
#include <cmath>
#include <unordered_map>
#define ai_jiang signed main()
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define drep(a,b,c) for(int a=b;a>=c;a--)
#define all(a) a.begin(),a.end()
#define ll long long
using namespace std;
using poly=vector<ll>;
namespace ai {
    long long aa,bb,cc;
    unordered_map<ll,ll> mp;
    ll k,m;
    inline ll qpow(ll a,ll b,ll p) {
        ll ans=1;
        while(b) { if(b&1) ans=ans*a%p; a=a*a%p; b>>=1; }
        return ans;
    }
    inline ll llsqrt(ll &x) {
        ll l=0,r=1e6,mid;
        while(l<=r) {
            mid=l+((r-l)>>1);
            if(mid*mid==x) return mid;
            else if(mid*mid>x) r=mid-1;
            else l=mid+1;
        } return 1;
    }
}
using namespace ai;
ai_jiang {
    cin.tie(0); cout.tie(0);
    cin>>aa>>bb; k=aa,m=bb; ll x=llsqrt(m),tar=(9*k+1)%m;
    for(ll i=0;i*i<=m;i++) mp[tar*qpow(10,i,m)%m]=i+1;
    for(ll i=0;i*i<=m;i++) {
        if(mp[qpow(10,i*x,m)] && i*x-mp[qpow(10,i*x,m)]+1 > 0) {
            cc=i*x-mp[qpow(10,i*x,m)]+1;
            cout<<cc<<endl;
            return 0;
        }
    }
    return 0;
}
2024/8/14 16:48
加载中...