您的位置:首页 > 编程语言

poj 2447 代码改正

2013-11-18 16:02 232 查看
//之前2447贴错了代码,表示很sorry
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: