The n-th power modulo of ac number theory

power modulo

Time Limit: 1000  ms | Memory Limit: 65535  KB
Difficulty: 3
describe

Find the value of the remainder of c when a raised to the b power

 

enter
Enter an integer n in the first line to indicate the number of test data groups (n<100).
Each group of tests has only one line, and there are three positive integers a,b,c(1=<a,b,c<=1000000000)
output
Output the result of taking the remainder of c to the b power of a
sample input
3
2 3 5
3 100 10
11 12345 12345
Sample output
3
1

10481

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int pow(long long int a,long long int n,long long int b)
{//Fast exponentiation modulo
    a=a%b;//Initialize the base modulo
    int result=1;
    while(n>0)
    {
        if(n%2==1)//If it is n
            result=result*a%b;//Multiply one more modulo
        a=a*a%b;//Multiply modulo
        n=n/2;//Shift left by one
    }
    return result;
}
intmain()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        long long int a,n,b;
        scanf("%lld%lld%lld",&a,&n,&b);//Read in three numbers
        printf("%d\n",pow(a,n,b));
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326483844&siteId=291194637