UVALive-3722 留个坑,为什么费马小定理求逆元不对??

#include <iostream>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <fstream>
#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 x,a,n,c,t,k;
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%c;
        a=a*a%c;
        b>>=1;
    }
    return r;
}

long long gcd(long long a,long long b){
    return (a%b==0?b:gcd(b,a%b));
}

long long sum(long long a,long long b){
    if(b==1)
        return a%c;
    long long half=ksm(a,b>>1);
    long long temp=sum(a,b>>1);
    if(b%2==0)
        return (temp%c+half*temp%c)%c;
    else
        return (temp%c+half*temp%c+half*half%c*a%c)%c;
}

int main(){
    while(cin>>x>>a>>n>>c&&x){
        t=ksm(a,n);
        //if(gcd(a-1,c)==1)
        //    o(((x%c*t%c-a%c*ksm(a-1,c-2)%c*((t-1)%c+c)%c)%c+c)%c);
        //else
            o(((x%c*t%c-sum(a,n))%c+c)%c);
        p('\n');
    }
    return 0;
}

https://vjudge.net/problem/UVALive-3722

猜你喜欢

转载自www.cnblogs.com/war1111/p/11296165.html