A^B % P(a^b%c问题与费马小定理)

原题链接
Description
A^B % P is a very interesting problem. Here a more bigger problem needs you to solve.

((((AB[0])B[1])…)B[n-1])%P

In which B[i]=B[i-1]^2-1( i > 0 ), P=1e9+7

Input
  The input consists of several test cases.
  The first line of the input contains a single integer T (0 < T ≤ 20), the number of test cases.
  Then Followed by T lines, each line gives a test case which contains three integers A, n and B0.
  0<A<2^31  0<n<=10000  1<B[0]<2^31
Output
For each test case, output an integer representing the result of (((AB[0])B[1])…)B[n-1]%P
Sample Input
2
3 1 2
2 2 2
Sample Output
9
64
注释请看代码

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
//const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
//const DB EPS = 1e-9;
//const DB OO = 1e20;
//const DB PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=10000 + 10;
//typedef long long ll;
//快速a^b%c计算
//特别需要注意的是如果c是一个素数,那么a^b%c==a^( b%(c-1) )%c,这就是费马小定理
long long fastpow(long long a,long long b){
        long long res=1;
        while(b){
                if(b&1) res*=a;
                res%=MOD;
                a=(a*a)%MOD;
                b=(b>>1);
        }
        return res;
}
int main(){
        int T;
        cin>>T;
        while(T--){
                long long  a,n,BI;
                cin >> a >> n >> BI;
                long long pow=BI;
                n--;
                while(n--){
                        BI=(BI*BI-1)%(MOD-1);
                        pow=(pow*BI)%(MOD-1);
                }
                cout << fastpow(a,pow) << endl;
        }
        return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门豪杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值