CodeForces - 919E

CodeForces - 919E
i的周期是p,a^i的周期是p-1,枚举i,求a^i的逆元,i=c=b*a^(-i),每当右边指数增加p-1(实际大小不变),左边都会减少1,所以差了i-c个p-1周期,所以第一个满足条件的周期就找到了。因为p是质数,求逆元用费马小定理。复杂度是p*logp

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)

using namespace std;
long long a,b,p,x,t,k,w,z,ans;
void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

long long ksm(long long a,long long b){
    long long r=1;
    while(b>0){
        if(b&1) r=r*a%p;
        a=a*a%p;
        b>>=1;
    }
    return r;
}

signed main(){
    in(a);in(b);in(p);in(x);
    t=a;
    for(register long long i=1;i<p;i++,t=t*a%p){
        k=b*ksm(t,p-2)%p;
        w=(i-k+p)%p;
        z=i+w*(p-1);
        if(z>x) continue;
        ans+=(x-z)/(p*(p-1))+1;
    }
    o(ans);
    return 0;
}

 

posted @ 2020-02-23 17:57  WeiAR  阅读(109)  评论(0编辑  收藏  举报