FZU 1759 Super A^B mod C

 
 

题目链接: http://acm.fzu.edu.cn/problem.php?pid=1759

题意求A^B mod C  (1<=A,C<=1000000000,1<=B<=10^1000000).

题解

http://hi.baidu.com/aekdycoin/item/e493adc9a7c0870bad092fd9

忘了看 F.A.Q   用longlong 竟然一直返回TLE 抓狂 抓狂 抓狂 抓狂 抓狂 抓狂 抓狂
#include<cstdio>
#include<cstring>
#include<cmath>
const int maxn = 1000100;
typedef  __int64 int64;
char b[maxn];
int64 euler_phi(int64 n)
{
     int64 m = (int64)sqrt(n+0.5);
     int64 ans = n;
     for(int64 i = 2; i <= m; i++)
          if(n%i == 0)
          {
               ans = ans / i * (i-1);
               while(n % i == 0)
                    n /= i;
          }
          if(n > 1)
               ans = ans / n * (n-1);
          return ans;
}
int64 pow_mod(int64 a, int64 b, int64 n)
{
     int64 ans = 1;
     for(; b ; b>>=1, a = a * a % n)
     {
          if(b & 1)
               ans = ans * a % n;
     }
     return ans;
}

int main ()
{
    int64 A,B, C;

    while(scanf("%I64d %s %I64d", &A, b, &C) != EOF)
    {
        int64 phi = euler_phi(C);

        int len = strlen(b);

        B = 0;
        for(int i = 0; i < len; i++)
        {
            B = (B * 10 + (b[i] - '0') )% phi;
        }
        B = B + phi;

        printf("%I64d\n", pow_mod(A,B,C));
    }
    return 0;
}



你可能感兴趣的:(数论)