陕西省第一届ACM程序设计竞赛A题(快速幂)

http://acm.xidian.edu.cn/land/problem/detail?problem_id=1265

Problem 1265 - A^B % P
Time Limit: 1000MS      Memory Limit: 65536KB      Difficulty:    
Total Submit: 6     Accepted: 4     Special Judge: No 
Description

A^B % P is a very interesting problem. Here a more bigger problem needs you to solve.

((((A^B[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
Output
For each test case, output an integer representing the result of (((A^B[0])^B[1])^…)^B[n-1]%P
Sample Input
2
3 1 2
2 2 2
Sample Output
9
64


分析:

我们知道求解A^B%P,我们可以使用快速幂

指数的运算公式A^B^C=A^(B*C)

费马小定理 如果P为素数,A^(P-1)%P=1

于是运用费马小定理我们就能发现指数B0*B1*B2*…*Bn可以变小为(B0*B1*B2*…*Bn)%(P-1)

最后我们得出解题步骤

1、用循环求出B1B2Bn

2、对他们的乘积%(P-1)

3、快速幂求答案

最后程序复杂度为O(N+log(P))


#include
#include
#include
 

#define mod 1000000007
using namespace std;

long long mul(long long a, long long b)  //快速幂求解a^b%mod
{
     long long sum =1;
     while(b)
     {
        if(b&1)
        sum=sum*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return sum;
}

int main()
{
    int c,n,a;
    long long m;
    scanf("%d",&c);
    while (c--)
    {

          scanf("%d%d%lld",&a,&n,&m);
          long long ret = 1;
          for (int i=0;i

你可能感兴趣的:(ACM_比赛)