Advertisement
shek_shek

Algebra_4

Mar 6th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <stack>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <iostream>
  8. #include <algorithm>
  9. #include <string>
  10. #include <set>
  11. #include <iomanip>
  12. #include <vector>
  13. #include <map>
  14. #include <cassert>
  15. #include <queue>
  16. #include <fstream>
  17.  
  18. using namespace std;
  19.  
  20. typedef long long li;
  21. typedef long double ld;
  22.  
  23. #define forn(i, n) for (int i = 0; i < int(n); ++i)
  24. #define pb push_back
  25. #define mp make_pair
  26. #define all(v) v.begin(),v.end()
  27. #define EPS 1e-9
  28. #define PI 3.1415926535897932384626433832795
  29.  
  30.  
  31. int gcd (int a, int b) {
  32.     return b ? gcd (b, a % b) : a;
  33. }
  34.  
  35. int main() {
  36.     srand(time(NULL));
  37.     ifstream key_stream("key.txt");
  38.     ifstream text_stream("text.txt");
  39.     freopen("output.txt", "w", stdout);
  40.  
  41.     string alph = "абвгдежзийклмнопрстуфхцчшщъыьэюя0123456789.,!?";
  42.     map <char, int> pos;
  43.     forn (i, alph.size())
  44.         pos[alph[i]] = i;
  45.  
  46.     int n, m = alph.size(), k;
  47.     key_stream >> n >> k;
  48.  
  49.     if (gcd(n, m) != 1 || n < m) {
  50.         cout << "incorrect key" << endl;
  51.         return 0;
  52.     }
  53.  
  54.     while (!text_stream.eof()) {
  55.         string text_temp;
  56.         text_stream >> text_temp;
  57.         forn (i, text_temp.size()) {
  58.             if (text_temp[i] >= 'А' && text_temp[i] <= 'Я')
  59.                 text_temp[i] += 'а' - 'А';
  60.             cout << alph[(pos[text_temp[i]] * n + k) % m];
  61.         }
  62.     }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.     return 0;
  69. }
  70.  
  71.  
  72.  
  73. ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
  74.  
  75. #define _CRT_SECURE_NO_WARNINGS
  76.  
  77. #include <stdio.h>
  78. #include <stack>
  79. #include <math.h>
  80. #include <time.h>
  81. #include <iostream>
  82. #include <algorithm>
  83. #include <string>
  84. #include <set>
  85. #include <iomanip>
  86. #include <vector>
  87. #include <map>
  88. #include <cassert>
  89. #include <queue>
  90. #include <fstream>
  91.  
  92. using namespace std;
  93.  
  94. typedef long long li;
  95. typedef long double ld;
  96.  
  97. #define forn(i, n) for (int i = 0; i < int(n); ++i)
  98. #define pb push_back
  99. #define mp make_pair
  100. #define all(v) v.begin(),v.end()
  101. #define EPS 1e-9
  102. #define PI 3.1415926535897932384626433832795
  103.  
  104.  
  105. int gcd (int a, int b) {
  106.     return b ? gcd (b, a % b) : a;
  107. }
  108.  
  109. int main() {
  110.     srand(time(NULL));
  111.     ifstream key_stream("key.txt");
  112.     ifstream text_stream("text.txt");
  113.     freopen("output.txt", "w", stdout);
  114.  
  115.     string alph = "абвгдежзийклмнопрстуфхцчшщъыьэюя0123456789.,!?";
  116.     map <char, int> pos;
  117.     forn (i, alph.size())
  118.         pos[alph[i]] = i;
  119.  
  120.     int n, m = alph.size(), k;
  121.     key_stream >> n >> k;
  122.  
  123.     if (gcd(n, m) != 1) {
  124.         cout << "НОД(n, m) != 1" << endl;
  125.         return 0;
  126.     }
  127.  
  128.     string text;
  129.     text_stream >> text;
  130.     forn (i, text.size()) {
  131.         int y = pos[text[i]];
  132.         int x = (n * n * (y - k)) % m;
  133.         cout << alph[x];
  134.     }
  135.  
  136.  
  137.  
  138.  
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement