Euclid.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // This is a collection of useful code for solving problems that
  2. // involve modular linear equations. Note that all of the
  3. // algorithms described here work on nonnegative integers.
  4. // return a % b (positive value)
  5. int mod(int a, int b) {
  6. return ((a%b)+b)%b;
  7. }
  8. // computes gcd(a,b)
  9. int gcd(int a, int b) {
  10. int tmp;
  11. while(b){a%=b; tmp=a; a=b; b=tmp;}
  12. return a;
  13. }
  14. // computes lcm(a,b)
  15. int lcm(int a, int b) {
  16. return a/gcd(a,b)*b;
  17. }
  18. // returns d = gcd(a,b); finds x,y such that d = ax + by,
  19. // with |x|+|y| minimal.
  20. int extended_euclid(int a, int b, int &x, int &y) {
  21. if (!b) { x = 1; y = 0; return a; }
  22. else { int d = extended_euclid(b, a%b, y, x); y -= x*(a/b); return d; }
  23. }
  24. // finds all solutions to ax = b (mod n)
  25. VI modular_linear_equation_solver(int a, int b, int n) {
  26. int x, y;
  27. VI solutions;
  28. int d = extended_euclid(a, n, x, y);
  29. if (!(b%d)) {
  30. x = mod (x*(b/d), n);
  31. for (int i = 0; i < d; i++)
  32. solutions.push_back(mod(x + i*(n/d), n));
  33. }
  34. return solutions;
  35. }
  36. // computes b such that ab = 1 (mod n), returns -1 on failure
  37. int mod_inverse(int a, int n) {
  38. int x, y;
  39. int d = extended_euclid(a, n, x, y);
  40. if (d > 1) return -1;
  41. return mod(x,n);
  42. }
  43. // Chinese remainder theorem (special case): find z such that
  44. // z % x = a, z % y = b. Here, z is unique modulo M = lcm(x,y).
  45. // Return (z,M). On failure, M = -1.
  46. PII chinese_remainder_theorem(int x, int a, int y, int b) {
  47. int s, t;
  48. int d = extended_euclid(x, y, s, t);
  49. if (a%d != b%d) return make_pair(0, -1);
  50. return make_pair(mod(s*b*x+t*a*y,x*y)/d, x*y/d);
  51. }
  52. // Chinese remainder theorem: find z such that
  53. // z % x[i] = a[i] for all i. Note that the solution is
  54. // unique modulo M = lcm_i (x[i]). Return (z,M). On
  55. // failure, M = -1. Note that we do not require the a[i]'s
  56. // to be relatively prime.
  57. PII chinese_remainder_theorem(const VI &x, const VI &a) {
  58. PII ret = make_pair(a[0], x[0]);
  59. for (int i = 1; i < x.size(); i++) {
  60. ret = chinese_remainder_theorem(ret.second, ret.first, x[i], a[i]);
  61. if (ret.second == -1) break;
  62. }
  63. return ret;
  64. }
  65. // computes x and y such that ax + by = c; on failure, x = y =-1
  66. void linear_diophantine(int a, int b, int c, int &x, int &y) {
  67. int d = gcd(a,b);
  68. if (c%d) {
  69. x = y = -1;
  70. } else {
  71. x = c/d * mod_inverse(a/d, b/d);
  72. y = (c-a*x)/b;
  73. }
  74. }
  75. int test() {
  76. // expected: 2
  77. cout << gcd(14, 30) << endl;
  78. // expected: 2 -2 1
  79. int x, y;
  80. int d = extended_euclid(14, 30, x, y);
  81. cout << d << " " << x << " " << y << endl;
  82. // expected: 95 45
  83. VI sols = modular_linear_equation_solver(14, 30, 100);
  84. for (int i = 0; i < (int) sols.size(); i++) cout << sols[i] << " ";
  85. cout << endl;
  86. // expected: 8
  87. cout << mod_inverse(8, 9) << endl;
  88. // expected: 23 56
  89. // 11 12
  90. int xs[] = {3, 5, 7, 4, 6};
  91. int as[] = {2, 3, 2, 3, 5};
  92. PII ret = chinese_remainder_theorem(VI (xs, xs+3), VI(as, as+3));
  93. cout << ret.first << " " << ret.second << endl;
  94. ret = chinese_remainder_theorem (VI(xs+3, xs+5), VI(as+3, as+5));
  95. cout << ret.first << " " << ret.second << endl;
  96. // expected: 5 -15
  97. linear_diophantine(7, 2, 5, x, y);
  98. cout << x << " " << y << endl;
  99. }