problem 1752 a^b mod Caccept:837 submit:3615
Time limit:1000 mSec Memory limit:32768 KB problem Description
Given A,b,c, you should quickly calculate the result of A^b mod C. (1<=a,b,c<2^63).
Input
There is multiply testcases. Each testcase, there are one line contains three integers A, B and C, separated by A single space.
Output
For each testcase, output an integer, denotes the result of A^b mod C.
Sample INPUT3 2 Output124 sourcefzu Summer Training iv--number theory
Exercises
Large data processing, the second square with multiplication, multiplication with addition, to ensure that not explode.
#include <stdio.h> #define LL unsigned __int64ll mul (ll a,ll b,ll c) {ll tmp=a%c,tre=0;while (b) {if (b&1) if (tre+ =TMP) >=c) tre-=c; if ((tmp<<=1) >=c) tmp-=c; B>>=1;} return tre;} LL MoD (ll a,ll b,ll c) {ll y=1;a=a%c;while (b) {if (b&1) Y=mul (y,a,c); A=mul (A,A,C);
Copyright NOTICE: This article for Bo Master original article, casually reproduced.
Fzu 1650 1752 a^b MoD