Euclid.cc 3.1 KB

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