Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. // LabaGovn.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <string>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include <regex>
  12.  
  13. #define lint unsigned long long
  14. using namespace std;
  15.  
  16. bool isFirstWrite = true;
  17. void pushOut(const char* text) {
  18. ofstream out;
  19. out.open("output.txt", std::ios::app);
  20. out << (isFirstWrite ? "" : " ") << text;
  21. isFirstWrite = false;
  22. out.close();
  23. }
  24. void printOut(const char* text) {
  25. remove("output.txt");
  26.  
  27. ofstream out;
  28. out.open("output.txt");
  29. out << text;
  30. out.close();
  31. }
  32. void printError() {
  33. printOut("incorrect command");
  34. }
  35.  
  36. #define SHOW_ERROR() {printError();return -1;}
  37. #define SAVE_READ_CHAR(file,fileSize,out) {if (fileSize-file.tellg() < 1) {SHOW_ERROR();} else {file.get(out);} }
  38. #define SAVE_READ(file,fileSize,out,len) {if (fileSize-file.tellg() < len) {SHOW_ERROR();} else {file.get(out,len);} }
  39.  
  40.  
  41. bool scanArgument(string data, const char* name, lint& out) {
  42. string regexData = string(name) + "=([0-9]{1,})";
  43. regex scaner(regexData);
  44. smatch res;
  45. if (regex_search(data, res, scaner)) {
  46. char* pEnd;
  47. out = strtoull(res[1].str().c_str(), &pEnd, 10);
  48. return true;
  49. }
  50. return false;
  51. }
  52.  
  53.  
  54. lint nod(lint n1, lint n2) {
  55. while (n1 != 0 && n2 != 0) {
  56. if (n1 >= n2) n1 = n1 % n2;
  57. else n2 = n2 % n1;
  58. }
  59. return n1 + n2;
  60. }
  61.  
  62. void cmd_get_c(lint cmin, lint cmax, lint m) {
  63. lint c = cmax;
  64. bool finded = true;
  65. while (c >= cmin) {
  66. if (nod(c, m) == 1) {
  67. finded = true;
  68. pushOut(to_string(c).c_str());
  69. }
  70. c--;
  71. }
  72. if (!finded) printOut("no solution");
  73. }
  74.  
  75. void cmd_get_a(lint m) {
  76. lint f;
  77. lint k = 0;
  78. lint a = 1;
  79. f = m;
  80. lint dividers[10000] = { 0 };
  81. for (lint i = 2; i <= sqrt((double)f); i++)
  82. if (m % i == 0) {
  83. do {
  84. m = m / i;
  85. } while (m%i == 0);
  86. dividers[k] = i;
  87. k++;
  88. }
  89. dividers[k] = m;
  90. for (lint i = 0; i <= k; i++) {
  91. a = a * dividers[i];
  92. }
  93. if (f % 4 == 0 && (a * 2) < f) {
  94. a = a * 2;
  95. }
  96. a++;
  97. if (f % 4 == 0) {
  98. if ((a - 1) % 4 == 0) {
  99. pushOut(to_string(a).c_str());
  100. }
  101. else {
  102. printOut("no solution");
  103. }
  104. }
  105. else {
  106. if (a <= f) {
  107. printOut(to_string(a).c_str());
  108. }
  109. else {
  110. printOut("no solution");
  111. }
  112. }
  113.  
  114. }
  115.  
  116. void cmd_lcg(lint a, lint x0, lint c, lint m, lint n) {
  117. lint i = 0;
  118. lint xt, x_n;
  119. xt = x0;
  120. if (n == 0) {
  121. printOut("no solution");
  122. }
  123. else {
  124. while (i < n) {
  125. x_n = (xt * a + c) % m;
  126. pushOut(to_string(x_n).c_str());
  127. xt = x_n;
  128. i++;
  129. }
  130. }
  131. }
  132.  
  133. void cmd_bits(lint a, lint x0, lint c, lint m, lint n) {
  134. lint i = 0;
  135. lint xt, x_n;
  136. int cache[64] = { 0 };
  137. xt = x0;
  138. if (n == 0) {
  139. printOut("no solution");
  140. }
  141. else
  142. {
  143. while (i < n) {
  144. x_n = (xt*a + c) % m;
  145. xt = x_n;
  146. for (int j = 0; j < 64 || x_n>0; j++) {
  147. cache[j] = cache[j] + (x_n % 2);
  148. x_n = x_n / 2;
  149. };
  150. i++;
  151. }
  152. for (int j = 63; j >= 0; j--) {
  153. pushOut(to_string(cache[j]).c_str());
  154. }
  155. }
  156. }
  157.  
  158. int main()
  159. {
  160. //raise old output
  161. isFirstWrite = true;
  162. remove("output.txt");
  163.  
  164.  
  165. ifstream input;
  166. input.open("input.txt", ios::binary);
  167.  
  168. input.seekg(0, SEEK_END);
  169. streamoff fSize = input.tellg();
  170. input.seekg(0, SEEK_SET);
  171.  
  172. char temp1[4];
  173.  
  174. SAVE_READ(input, fSize, temp1, 4);
  175.  
  176. char inputBuff[500];
  177.  
  178. if (strcmp(temp1,"get") == 0) {
  179. char temp2[3];
  180. SAVE_READ(input, fSize, temp2, 3);
  181.  
  182. if (strcmp(temp2, "_a") == 0) {
  183. input.get(inputBuff, fSize - input.tellg() + (streamoff)1);
  184. string cppInput(inputBuff);
  185.  
  186. lint m = 0;
  187. bool correctScan = scanArgument(cppInput, "m", m);
  188. if (!correctScan) {
  189. printError();
  190. return -1;
  191. }
  192. cmd_get_a(m);
  193. }
  194. else if (strcmp(temp2, "_c") == 0) {
  195. input.get(inputBuff, fSize - input.tellg() + (streamoff)1);
  196. string cppInput(inputBuff);
  197.  
  198. lint cmax = 0, cmin = 0, m = 0;
  199. bool correctScan =
  200. scanArgument(cppInput, "cmax", cmax) &&
  201. scanArgument(cppInput, "cmin", cmin) &&
  202. scanArgument(cppInput, "m", m);
  203.  
  204. if (!correctScan) {
  205. printError();
  206. return -1;
  207. }
  208. cmd_get_c(cmin, cmax, m);
  209. }
  210. else {
  211. SHOW_ERROR();
  212. }
  213. }
  214. else if (strcmp("lcg",temp1) == 0) {
  215. input.get(inputBuff, fSize - input.tellg() + (streamoff)1);
  216. string cppInput(inputBuff);
  217. lint a = 0, x0 = 0, c = 0, m = 0, n = 0;
  218.  
  219. bool correctScan =
  220. scanArgument(cppInput, "a", a) &&
  221. scanArgument(cppInput, "x0", x0) &&
  222. scanArgument(cppInput, "c", c) &&
  223. scanArgument(cppInput, "m", m) &&
  224. scanArgument(cppInput, "n", n);
  225.  
  226. if (!correctScan) {
  227. printError();
  228. return -1;
  229. }
  230. cmd_lcg(a, x0, c, m, n);
  231.  
  232. }
  233. else if (strcmp(temp1,"bit")==0) {
  234. char temp2;
  235. SAVE_READ_CHAR(input, fSize, temp2);
  236. if (temp2 == 's') {
  237. input.get(inputBuff, fSize - input.tellg() + (streamoff)1);
  238. string cppInput(inputBuff);
  239. lint a = 0, x0 = 0, c = 0, m = 0, n = 0;
  240.  
  241. bool correctScan =
  242. scanArgument(cppInput, "a", a) &&
  243. scanArgument(cppInput, "x0", x0) &&
  244. scanArgument(cppInput, "c", c) &&
  245. scanArgument(cppInput, "m", m) &&
  246. scanArgument(cppInput, "n", n);
  247.  
  248. if (!correctScan) {
  249. printError();
  250. return -1;
  251. }
  252. cmd_bits(a, x0, c, m, n);
  253. }
  254. else {
  255. SHOW_ERROR();
  256. }
  257. }
  258. else {
  259. SHOW_ERROR();
  260. }
  261. return 0;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement