Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- unsigned GCD(unsigned a, unsigned c) {
- if (a < c) {
- a ^= c;
- c ^= a;
- a ^= c;
- }
- unsigned tmp = 0;
- while (a % c != 0) {
- tmp = a % c;
- a = c;
- c = tmp;
- }
- return c;
- }
- int findValues(unsigned a, unsigned b, unsigned c) {
- unsigned div = GCD(a, c);
- cout << endl << "\nUp to four solutions might appear." << endl << endl;
- if (b % div != 0) { cout << "Solution may not be possible. Check bellow if there is a solution!" << endl << endl; }
- if (a < b) {
- a ^= b;
- b ^= a;
- a ^= b;
- }
- if (c % a == 0) {
- cout << c / a << "*" << a << " + 0*" << b << " = 0 mod(" << c << ")" << endl;
- }
- if (c % b == 0) {
- cout << "0*" << a << " + " << c / b << "*" << b << " = 0 mod(" << c << ")" << endl;
- }
- if ((b + a) % c == 0) {
- cout << "1*" << a << " + 1*" << b << " = 0 mod(" << c << ")" << endl;
- return 0;
- }
- {
- for (int i = 1; i < c; i++) {
- unsigned x;
- unsigned y;
- if ((c - (i * b)) % a == 0) {
- y = i;
- x = (c - (i * b)) / a;
- cout << x << "*" << a << " + " << y << "*" << b << " = 0 mod(" << c << ")" << endl;
- break;
- }
- if ((c - (i * a)) % b == 0) {
- x = i;
- y = (c - (i * a)) / b;
- cout << x << "*" << a << " + " << y << "*" << b << " = 0 mod(" << c << ")" << endl;
- break;
- }
- }
- }
- }
- int main() {
- unsigned a, b, c;
- cout << "a*x + b*y = 0 (mod c)" << endl;
- cout << "a=";
- cin >> a;
- cout << "b=";
- cin >> b;
- cout << "c=";
- cin >> c;
- findValues(a, b, c);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement