Modular exponentiation

//
Number // n-th power of 712, the result after three to 696, n satisfy this condition is how much? (0 <n-<24767)
// This is a typical problem of modular exponentiation algorithm, demonstrated the following: (A * B) n-% = [(A n-%) * (n-% B)] n-%   (* into the also holds +)
// set K1 * = n-+ A R1, K2 * = B + n-R2,
@ then there are:
// (A * B) n-% = (K1 * K2 + K1 * n-n-* R2 * n-R1 + K2 * * * * n-R1 + R2) n-% = (R1 * R2) n-% = [(A n-%) * (n-% B)] n-%.
// The above has demonstrated (a ^ b ) n-% = ((% n-a) * (a ^ (B -. 1))% n-)% n-          == 696;
// the following presents modular exponentiation recursive and non-recursive codes:
#include <the iostream>
the using namespace std;

int getMod1(int a, int b, int n) //712*712*712*712*712*712*712*712。。。。。 a^b%n
{
if (0 == b)
return 1;
return (a % n * getMod1(a, b - 1, n)) % n;
}

int getMod2 (int a, int b , int n) // a: 712 b: the number of power 712 n: 1000, after the remaining three digits modulo n
{
int I, R & lt =. 1;
for (I =. 1; I <= B; I ++)
{
R & lt = ((n-R & lt%) * (n-% A)) n-%;
}
return R & lt;
}

int main()
{
int a = 3;
int b = 4;
int n = 5;
cout << getMod1(a, b, n) << endl; //81%5=1
cout << getMod2(a, b, n) << endl;

int i, r, sum = 0;
a = 712;
n = 1000;
r = 696;
for (i = 1; i < 24767; i++)
if (r == getMod2(a, i, n))
sum++;
cout << sum << endl;

return 0;
}

 

Guess you like

Origin www.cnblogs.com/xcb-1024day/p/11334804.html