fzu 1759  A^B % C

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

题目大意 A^b % c的值,其中a,c<1000000000 b<10^1000000

考察点 A^B % C = A ^(B % PHI(C) + PHI(C) ) % C   ( B >= PHI(C));

思路 执行上面公式的过程

提交情况: Accepted 1

收获/经验 : 要找到严谨的公式

AC code

#include <stdio.h>

#include <math.h>

#define MAXB 10000010

#define I64 __int64

charb[MAXB];

I64 prime[1110];

 

I64 Getphi(I64 c){

    I64 ans = c, i, count = 0, m;

    if(c % 2 == 0){

       prime[count ++] = 2;

       while(c % 2 == 0) c /= 2;

    }

    m = (I64)sqrt((long double)c) + 1;

    for(i = 3; i <= m && c >= i; i += 2)

       if(c % i == 0){

           prime[count ++] = i;

           while(c % i == 0) c /= i;

       }

    if(c != 1) prime[count ++] = c;

    for(i = 0; i < count; i ++)

       ans = (ans / prime[i]) * (prime[i] - 1);

    return ans;

}

 

I64 Multi(I64 a, I64 x, I64 c){

    I64 temp = 0;

    while(x){

       if(x & 1) temp = (temp + a) % c;

       a = (a + a) % c;

       x >>= 1;

    }

    return temp;

}

 

I64 Get_mod(I64 a, I64 x, I64 c){

    I64 temp = 1;

    a %= c;

    while(x){

       if(x & 1) temp = Multi(temp, a, c);

       a = Multi(a, a, c);

       x >>= 1;

    }

    return temp;

}

 

 

int main(){

    I64 a, c, PHI, t, ans, i;

    while(~scanf("%I64d %s %I64d", &a, b, &c)){

       PHI = Getphi(c);

       t = 0;

       for(i = 0; b[i] != '\0'; i ++){

           t = t * 10 + b[i] - 48;

           if(t > PHI) break;

       }

       if(t > PHI){

           t = 0;

           for(i = 0; b[i] != '\0'; i ++)

              t = ((t * 10) + (b[i] - 48)) % PHI;

           t += PHI;

       }

       ans = Get_mod(a, t, c); //a^t % c 用快速幂

       printf("%I64d\n", ans);

    }

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值