Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5.  
  6. unsigned GCD(unsigned a, unsigned c) {
  7. if (a < c) {
  8. a ^= c;
  9. c ^= a;
  10. a ^= c;
  11. }
  12. unsigned tmp = 0;
  13. while (a % c != 0) {
  14. tmp = a % c;
  15. a = c;
  16. c = tmp;
  17. }
  18. return c;
  19.  
  20. }
  21.  
  22. int findValues(unsigned a, unsigned b, unsigned c) {
  23. unsigned div = GCD(a, c);
  24. cout << endl << "\nUp to four solutions might appear." << endl << endl;
  25. if (b % div != 0) { cout << "Solution may not be possible. Check bellow if there is a solution!" << endl << endl; }
  26.  
  27. if (a < b) {
  28. a ^= b;
  29. b ^= a;
  30. a ^= b;
  31. }
  32.  
  33. if (c % a == 0) {
  34.  
  35. cout << c / a << "*" << a << " + 0*" << b << " = 0 mod(" << c << ")" << endl;
  36. }
  37.  
  38.  
  39. if (c % b == 0) {
  40. cout << "0*" << a << " + " << c / b << "*" << b << " = 0 mod(" << c << ")" << endl;
  41. }
  42.  
  43. if ((b + a) % c == 0) {
  44. cout << "1*" << a << " + 1*" << b << " = 0 mod(" << c << ")" << endl;
  45. return 0;
  46. }
  47.  
  48. {
  49. for (int i = 1; i < c; i++) {
  50. unsigned x;
  51. unsigned y;
  52. if ((c - (i * b)) % a == 0) {
  53. y = i;
  54. x = (c - (i * b)) / a;
  55. cout << x << "*" << a << " + " << y << "*" << b << " = 0 mod(" << c << ")" << endl;
  56. break;
  57. }
  58. if ((c - (i * a)) % b == 0) {
  59. x = i;
  60. y = (c - (i * a)) / b;
  61. cout << x << "*" << a << " + " << y << "*" << b << " = 0 mod(" << c << ")" << endl;
  62. break;
  63. }
  64. }
  65. }
  66. }
  67.  
  68. int main() {
  69. unsigned a, b, c;
  70. cout << "a*x + b*y = 0 (mod c)" << endl;
  71. cout << "a=";
  72. cin >> a;
  73. cout << "b=";
  74. cin >> b;
  75. cout << "c=";
  76. cin >> c;
  77. findValues(a, b, c);
  78.  
  79.  
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement