LUOGU 9月 月赛

T1 签到题

传送门

解题思路

将原式化简一下,让n个1变成 (10^n-1)/9 ,然后再移项,变成了高次同余形式,用bsgs求解。交了好几次都是80,后来才被告知要快速乘。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<cmath> 

using namespace std;
typedef long long LL;

LL m,k;
map<LL,LL> mp;

LL fast_mul(LL x,LL y){
    LL ret=0;
    for(;y;y>>=1){
        if(y&1) ret=(ret+x)%m;
        x=(x+x)%m;
    }
    return ret;
}

inline LL fast_pow(LL x,LL y){
    LL ret=1;
    for(;y;y>>=1){
        if(y&1) ret=fast_mul(ret,x);
        x=fast_mul(x,x);
    }
    return ret;
}


int main(){
    scanf("%lld%lld",&k,&m);
    k=k*9+1;k%=m;
    LL siz=ceil(sqrt(m));
    LL now=k;mp[now]=0;
    for(LL i=1;i<=siz;i++){
        now=now*10%m;
        mp[now]=i;
    }
    now=1ll;LL base=fast_pow(1ll*10,siz);
    for(LL i=1;i<=siz;i++){
        now=fast_mul(now,base);
        if(mp.count(now)) {printf("%lld
",((i*siz-mp[now])%m+m)%m);return 0;}
    }
    return 0;
}
原文地址:https://www.cnblogs.com/sdfzsyq/p/9676829.html