Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LabaGovn.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <string>
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- #include <math.h>
- #include <regex>
- #define lint unsigned long long
- using namespace std;
- bool isFirstWrite = true;
- void pushOut(const char* text) {
- ofstream out;
- out.open("output.txt", std::ios::app);
- out << (isFirstWrite ? "" : " ") << text;
- isFirstWrite = false;
- out.close();
- }
- void printOut(const char* text) {
- remove("output.txt");
- ofstream out;
- out.open("output.txt");
- out << text;
- out.close();
- }
- void printError() {
- printOut("incorrect command");
- }
- #define SHOW_ERROR() {printError();return -1;}
- #define SAVE_READ_CHAR(file,fileSize,out) {if (fileSize-file.tellg() < 1) {SHOW_ERROR();} else {file.get(out);} }
- #define SAVE_READ(file,fileSize,out,len) {if (fileSize-file.tellg() < len) {SHOW_ERROR();} else {file.get(out,len);} }
- bool scanArgument(string data, const char* name, lint& out) {
- string regexData = string(name) + "=([0-9]{1,})";
- regex scaner(regexData);
- smatch res;
- if (regex_search(data, res, scaner)) {
- char* pEnd;
- out = strtoull(res[1].str().c_str(), &pEnd, 10);
- return true;
- }
- return false;
- }
- lint nod(lint n1, lint n2) {
- while (n1 != 0 && n2 != 0) {
- if (n1 >= n2) n1 = n1 % n2;
- else n2 = n2 % n1;
- }
- return n1 + n2;
- }
- void cmd_get_c(lint cmin, lint cmax, lint m) {
- lint c = cmax;
- bool finded = true;
- while (c >= cmin) {
- if (nod(c, m) == 1) {
- finded = true;
- pushOut(to_string(c).c_str());
- }
- c--;
- }
- if (!finded) printOut("no solution");
- }
- void cmd_get_a(lint m) {
- lint f;
- lint k = 0;
- lint a = 1;
- f = m;
- lint dividers[10000] = { 0 };
- for (lint i = 2; i <= sqrt((double)f); i++)
- if (m % i == 0) {
- do {
- m = m / i;
- } while (m%i == 0);
- dividers[k] = i;
- k++;
- }
- dividers[k] = m;
- for (lint i = 0; i <= k; i++) {
- a = a * dividers[i];
- }
- if (f % 4 == 0 && (a * 2) < f) {
- a = a * 2;
- }
- a++;
- if (f % 4 == 0) {
- if ((a - 1) % 4 == 0) {
- pushOut(to_string(a).c_str());
- }
- else {
- printOut("no solution");
- }
- }
- else {
- if (a <= f) {
- printOut(to_string(a).c_str());
- }
- else {
- printOut("no solution");
- }
- }
- }
- void cmd_lcg(lint a, lint x0, lint c, lint m, lint n) {
- lint i = 0;
- lint xt, x_n;
- xt = x0;
- if (n == 0) {
- printOut("no solution");
- }
- else {
- while (i < n) {
- x_n = (xt * a + c) % m;
- pushOut(to_string(x_n).c_str());
- xt = x_n;
- i++;
- }
- }
- }
- void cmd_bits(lint a, lint x0, lint c, lint m, lint n) {
- lint i = 0;
- lint xt, x_n;
- int cache[64] = { 0 };
- xt = x0;
- if (n == 0) {
- printOut("no solution");
- }
- else
- {
- while (i < n) {
- x_n = (xt*a + c) % m;
- xt = x_n;
- for (int j = 0; j < 64 || x_n>0; j++) {
- cache[j] = cache[j] + (x_n % 2);
- x_n = x_n / 2;
- };
- i++;
- }
- for (int j = 63; j >= 0; j--) {
- pushOut(to_string(cache[j]).c_str());
- }
- }
- }
- int main()
- {
- //raise old output
- isFirstWrite = true;
- remove("output.txt");
- ifstream input;
- input.open("input.txt", ios::binary);
- input.seekg(0, SEEK_END);
- streamoff fSize = input.tellg();
- input.seekg(0, SEEK_SET);
- char temp1[4];
- SAVE_READ(input, fSize, temp1, 4);
- char inputBuff[500];
- if (strcmp(temp1,"get") == 0) {
- char temp2[3];
- SAVE_READ(input, fSize, temp2, 3);
- if (strcmp(temp2, "_a") == 0) {
- input.get(inputBuff, fSize - input.tellg() + (streamoff)1);
- string cppInput(inputBuff);
- lint m = 0;
- bool correctScan = scanArgument(cppInput, "m", m);
- if (!correctScan) {
- printError();
- return -1;
- }
- cmd_get_a(m);
- }
- else if (strcmp(temp2, "_c") == 0) {
- input.get(inputBuff, fSize - input.tellg() + (streamoff)1);
- string cppInput(inputBuff);
- lint cmax = 0, cmin = 0, m = 0;
- bool correctScan =
- scanArgument(cppInput, "cmax", cmax) &&
- scanArgument(cppInput, "cmin", cmin) &&
- scanArgument(cppInput, "m", m);
- if (!correctScan) {
- printError();
- return -1;
- }
- cmd_get_c(cmin, cmax, m);
- }
- else {
- SHOW_ERROR();
- }
- }
- else if (strcmp("lcg",temp1) == 0) {
- input.get(inputBuff, fSize - input.tellg() + (streamoff)1);
- string cppInput(inputBuff);
- lint a = 0, x0 = 0, c = 0, m = 0, n = 0;
- bool correctScan =
- scanArgument(cppInput, "a", a) &&
- scanArgument(cppInput, "x0", x0) &&
- scanArgument(cppInput, "c", c) &&
- scanArgument(cppInput, "m", m) &&
- scanArgument(cppInput, "n", n);
- if (!correctScan) {
- printError();
- return -1;
- }
- cmd_lcg(a, x0, c, m, n);
- }
- else if (strcmp(temp1,"bit")==0) {
- char temp2;
- SAVE_READ_CHAR(input, fSize, temp2);
- if (temp2 == 's') {
- input.get(inputBuff, fSize - input.tellg() + (streamoff)1);
- string cppInput(inputBuff);
- lint a = 0, x0 = 0, c = 0, m = 0, n = 0;
- bool correctScan =
- scanArgument(cppInput, "a", a) &&
- scanArgument(cppInput, "x0", x0) &&
- scanArgument(cppInput, "c", c) &&
- scanArgument(cppInput, "m", m) &&
- scanArgument(cppInput, "n", n);
- if (!correctScan) {
- printError();
- return -1;
- }
- cmd_bits(a, x0, c, m, n);
- }
- else {
- SHOW_ERROR();
- }
- }
- else {
- SHOW_ERROR();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement