Problem R: "Divide and conquer" fast mode power time limit: 1 Sec memory limit: up to MB
Submissions: 8 resolution: 7
Submitted State [Discussion Version] The title describes the value of the ab%n, where a, B, and N are the numbers in the integer range. Enter values for three integers a, b, and n output ab%n sample input
1 1 1
Sample output
0
Problem-solving ideas: The rapid modulus of power than the fast power to take more than the remainder, and another fast to take the remainder is the same. There are formulas.
Code:
#include <iostream>#include<cstdio>using namespacestd;//a*b% n = (a% n) * (b% n)% nLong Long intMoLong Long intXLong Long intNLong Long inta) { if(x==a) { return 0; } if(n==1){ returnX%A; } if(n==2){ returnx*x%A; } if(n==3){ returnx*x*x%A; } if(n==4){ returnx*x*x*x%A; } if(n%2==0){ returnMo (x,n/2, a) *mo (x,n/2, a)%A; } if(n%2!=0){ returnMo (x, (n1)/2, a) *mo (x, (n1)/2, a) *x%A; }}intMain () {intx; intN; intA; Long Long intJieguo; while(SCANF (" %d%d%d", &x,&n,&a)! =EOF) {Jieguo=Mo (x,n,a); printf ("%lld\n", Jieguo); } return 0;}
"Divide and conquer" fast modulus power