Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. /*
  2. g++ main.cpp -o main -O2 -std=c++11 -lgmp
  3. */
  4. #include <iostream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <math.h>
  8. #include <numeric>
  9. #include <fstream>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13.  
  14. using namespace std;
  15.  
  16. /*
  17. Question:
  18. What is the lowest x that fits the modulo rules?
  19.  
  20. Example:
  21. x = 1 (mod 3)
  22. x = 4 (mod 5)
  23. x = 6 (mod 7)
  24. x = 7 (mod 11)
  25. x = 5 (mod 13)
  26. x = 5 (mod 19)
  27.  
  28. Step 1:
  29. x1 = 3k + 1
  30.  
  31. Step 2:
  32. 3k + 1 = 4 (mod 5)
  33. 3k = 3 (mod 5)
  34. 3k = 5L + 3
  35. k = 5L + 1
  36.  
  37. x = 3(5L + 1) + 1
  38. x = 15L + 4
  39.  
  40. Step 3:
  41. 15L + 4 = 6 (mod 7)
  42. 15L = 2 (mod 7)
  43. 1L = 2 (mod 7)
  44. 1L = 7m + 2
  45. L = 7m + 2
  46.  
  47. x = 15(7m + 2) + 4
  48. x = 105m + 34
  49.  
  50. Step 4
  51. 105m + 34 = 7 (mod 11)
  52. 105m = 6 (mod 11)
  53. 6m = 6 (mod 11)
  54. 6m = 11n + 6
  55. m = 11n + 1
  56.  
  57. x = 105(11n + 1) + 34
  58. x = 1155n + 139
  59.  
  60. Step 5:
  61. 1155n + 139 = 5 (mod 13)
  62. 1155n = 9 (mod 13)
  63. 11n = 9 (mod 13)
  64. 11n = 13p + 9
  65. n = 13p + 2
  66.  
  67. x = 1155(13p + 2) + 139
  68. x = 15015p + 2449
  69.  
  70. Step 6:
  71. 15015p + 2449 = 5 (mod 19)
  72. 15015p = 7 (mod 19)
  73. 5n = 7 (mod 19)
  74. 5n = 19q + 7
  75. p = 19q + 9
  76.  
  77. x = 15015(19q + 9) + 2449
  78. x = 285285q + 137584
  79. */
  80.  
  81. struct Formula {
  82. long long remainder;
  83. long long modulo;
  84. };
  85.  
  86. typedef vector<Formula> Rules;
  87.  
  88. long long gcd(long long a, long long b) {
  89. long long c;
  90.  
  91. while ( a != 0 ) {
  92. c = a;
  93. a = b % a;
  94. b = c;
  95. }
  96.  
  97. return b;
  98. }
  99.  
  100. /*
  101. Get lowest value that fits the rules (Bruteforce)
  102. */
  103. long long f2(const Rules &r) {
  104. int i, id;
  105. long long v;
  106.  
  107. //Init
  108. id = r.size() - 1;
  109. v = r[id].remainder;
  110.  
  111. for(i = 0; i < r.size(); i++) {
  112. // Debug
  113. //cout << v << " % " << r[i].modulo << " = " << v % r[i].modulo << endl;
  114.  
  115. if(v > 10000000)
  116. return -1;
  117.  
  118. if(v % r[i].modulo != r[i].remainder) {
  119. v += r[id].modulo;
  120. i = -1;
  121. }
  122. }
  123.  
  124. return v;
  125. }
  126.  
  127. /*
  128. Find smallest x as a integer
  129.  
  130. Formula:
  131. xa = b + cy
  132. */
  133. long long getRemainder(long long a, long long b, long long c) {
  134. long long x, y;
  135.  
  136. if(a == 0)
  137. return -1; //No answer found
  138.  
  139. for(y = 0; y <= c; y++) {
  140. x = b + (c * y);
  141.  
  142. if(x % a == 0) {
  143. x = x / a;
  144.  
  145. // Debug
  146. //cout << "gcd(" << a << ", " << b << ") = " << gcd(a, b) << endl;
  147. //cout << a << " * " << x << " = " << b << " + (" << y << " * " << c << ")" << endl;
  148. //cout << "------------------------" << endl;
  149.  
  150. return x;
  151. }
  152. }
  153.  
  154. return -1; //No answer found
  155. }
  156.  
  157. /*
  158. Get lowest value that fits the rules
  159. */
  160. long long f(const Rules &r) {
  161. int i;
  162. long long x, y, x2, y2;
  163. Formula f;
  164.  
  165. f = r[0];
  166. x = f.remainder;
  167. y = f.modulo;
  168.  
  169. for(i = 1; i < r.size(); i++) {
  170. f = r[i];
  171. y2 = f.modulo;
  172.  
  173. x2 = (((((x / y2) + 1) * y2) + f.remainder) - x) % y2;
  174. x2 = getRemainder(y % y2, x2, y2);
  175.  
  176. if(x2 == -1)
  177. return -1; // No answer possible
  178.  
  179. // Update
  180. x += y * x2;
  181. y = y * y2;
  182. }
  183.  
  184. return x;
  185. }
  186.  
  187. int main() {
  188. //Rules r = {{1, 3}, {4, 5}, {6, 7}};
  189. //Rules r = {{1, 3}, {4, 5}, {6, 7}, {7, 11}};
  190. //Rules r = {{1, 3}, {4, 5}, {6, 7}, {7, 11}, {5, 13}};
  191. Rules r = {{1, 3}, {4, 5}, {6, 7}, {7, 11}, {5, 13}, {1, 19}};
  192.  
  193. cout << "Result = " << f(r) << " " << f2(r) << endl;
  194.  
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement