Module-based exponential Computation
Long live family, long live family.
Introduction
Oh, after the holiday, I went home, and the village was quite well-built. In the evening, Aunt Auntie came out to dance. I am still in coding. It's really nice to have a coding day. I like rural areas, I'm a farmer, not yet a village cadre!
Modulo exponential operation
The functionality of the original model is so powerful that I didn't care too much about it before. It's really embarrassing. The module gives the Organization great meaning, so that big data can be processed by us. This is my understanding of the module. Definition of the problem: given x and y, calculate the value of x's y Power modulo N, where N has hundreds of digits. The algorithm is as follows (recursive algorithm)
The principle is as follows:
Lab
As long as the algorithm is recursive, I will convert it into a non-recursive algorithm and give it to you. Test1.cpp
#include
#include
using namespace std;// function modexp in recursionint modexp_re(int x,int y,int N);// function modexp without recursionint modexp_nonre(int x,int y,int N);int const N = 10000 ;// function mainint main(){int a,b,i = 0;while(i < 100){a = rand()%10000;b = rand()%10000;cout<<"a="< * s = new stack
;while(y >= 1){s->push(y);y = y/2;}result = x % N;while(s->size()>1){z = s->top();s->pop();result = ( (result%N) * (result%N) )%N;if(s->empty())break;else if(z*2 != s->top()){result = (result * (x%N) )%N;}}return result;}}