1046 a^b mod C reference time limit: 1 seconds space limit: 131072 KB score: 0 Difficulty: Basic collection attention gives 3 positive integers a B C, for A^b Mod c. For example, 3 5 8,3^5 Mod 8 = 3. Input
3 positive integers a B C, separated by a space in the middle. (1 <= a,b,c <= 10^9)
Output
Output calculation results
Input example
3 5 8
Output example
3
Related issues x^a mod p x^2 mod p x^a mod B 640 x^3 mod p 640 li edify(topic Provider)
Click to open link
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include < Stdlib.h> #include <math.h>using namespace Std;long long quickmod (Long long A,long long B,long long m) { long L Ong ans = 1; while (b)//With a loop right-to-left convenience B all Bits { if (b&1)//Determines whether bits at this time b[i] is 1 { ans = (ans*a)%m;//multiply to the result, where A is a^ ( 2^i)%m b--;//The change to 0 } b/=2; A = a*a%m; } return ans;} int main () { int m,a,b; while (scanf ("%d%d%d", &a,&b,&m)!=eof) { long long int ans = quickmod (a,b,m); printf ("%lld\n", ans); } return 0;}
Copyright NOTICE: This article is the original blogger article, if you have special needs, please contact Bo Master qq:793977586.
Wuyi nod 1046 a^b Mod C (Fast power-over redundancy)