poj 2447 代碼改正


 

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define MAXN 10
#define Ct 16381
#define I64 long long

I64 multi(I64 a, I64 b, I64 n){
     I64 tmp = a % n, s = 0;
   while(b){
         if(b & 1) s = (s + tmp) % n;
              tmp = (tmp + tmp) % n;
            b >>= 1;
    }
 return s;
}

I64 Pow(I64 a, I64 b, I64 n){
 I64 tmp = a % n, s = 1;
   while(b){
         if(b & 1) s = multi(s, tmp, n);
           tmp = multi(tmp, tmp, n);
         b >>= 1;
    }
 return s;
}

I64 gcd(I64 a, I64 b){
        return b ? gcd(b, a % b) : a;
}

I64 Ext_Gcd(I64 a, I64 b, I64 &x, I64 &y){
        if(!b){
           x = 1, y = 0;
             return a;
 }
 I64 r = Ext_Gcd(b, a % b, x, y);
  I64 t = x; x = y; y = t - a / b * y;
      return r;
}

I64 pollard_rho(I64 n, I64 c){
        I64 x, y, d, i = 1, k = 2;
        srand((I64)time(0));
      x = ((I64) rand()) % (n - 1) + 1;
 y = x;
    while(1){
         i ++;
             x = (multi(x, x, n) + c) % n;
             d = gcd(y - x + n, n);
            if(d != 1 && d != n) return d;
            if(y == x) return n;
              if(i == k) y = x, k <<= 1;
  }
}

int main(){
   I64 C, E, N, D, M, P, Q, T, X, Y;
 while(~scanf("%I64d %I64d %I64d", &C, &E, &N)){
           P = pollard_rho(N, Ct);
           Q = N / P;
                T = (P - 1) * (Q - 1);
            Ext_Gcd(E, T, X, Y);
              D = (X + T) % T;
          M = Pow(C, D, N);
         printf("%lld\n", M);
      }
 return 0;
}

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com