1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <iostream>
//#include<fstream>
using namespace std;
int gcd(int a, int n)
{
if(n == 0)
{
return a;
}
else
{
return gcd(n, a % n);
}
}
void extendedeuclid(int value1,int value2)
{
int x = 0, y = 1;
int prevx = 1, prevy = 0;
while (value2 != 0)
{
int holdValue = value2;
int quotient = value1 / value2;
value2 = value1 % value2;
value1 = holdValue;
holdValue = x;
x = prevx - quotient * x;
prevx = holdValue;
holdValue = y;
y = prevy - quotient * y;
prevy = holdValue;
}
cout << "X: " << prevx << " Y:" << prevy << endl;
cout << "D:" << value1 << endl;
}
int modularlinear(int a,int b,int n)
{
int i;
int x0;
int result;
(d,x,y)= extendedeuclid(a,n);
if (d/b)
{
x0=x*(b/d)%n;
for(i=0;i<d;i++)
{
result=(x0+i*(n/d))%n;
cout<<result;
}
cout<<"No solution"<<endl;
}
}
unsigned mod_pow(unsigned num, unsigned pow, unsigned mod)
{
unsigned test;
for(test = 1; pow; pow >>= 1)
{
if (pow & 1)
test = (test * num) % mod;
num = (num * num) % mod;
}
return test;
}
int main()
{
int a,b,n;
int m,e,n2;
//ofstream outf("f://output.txt",ios::out);
cout << "Input first number: ";
//outf<< "Input first number: ";
cin >> a;
//outf << a<<endl;
cout << "Input second number: ";
//outf<<"Input second number: ";
cin >> n;
//outf<< n<<endl;
cout<<"b:"<<endl;
cin>>b;
cout<<"m: "<<endl;
cin>>m;
cout<<"e: "<<endl;
cin>>e;
cout<<"n : "<<endl;
cin>>n2;
cout<<"Number of solutions:"<<modularlinear(a,b,n);
cout<<"M^e (mod n) = "<<mod_pow(m,e,n2)<<endl;
extendedeuclid(a,n);
cout << "GCD ("<<a<<","<<n<<")="<< gcd(a,n) << endl;
//outf<<"GCD ("<<a<<","<<n<<")="<< gcd(a,n) << endl;
system("pause");
return 0;
}
|