Advertisement
mihaimarcel21

Afin1

Feb 17th, 2021
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. ifstream fin("afin1.in");
  5. ofstream fout("afin1.out");
  6. int a, b, expr[27];
  7. string mess;
  8. int modInverse(int a, int b)
  9. {
  10.     int b0 = b, t, q;
  11.     int x0 = 0, x1 = 1;
  12.     if (b == 1) return 1;
  13.     while (a > 1)
  14.     {
  15.         q = a / b;
  16.         t = b, b = a % b, a = t;
  17.         t = x0, x0 = x1 - q * x0, x1 = t;
  18.     }
  19.     if (x1 < 0) x1 += b0;
  20.     return x1;
  21. }
  22. int main()
  23. {
  24.     fin>>a>>b;
  25.     fin>>mess;
  26.     for(int i=0; i<26; i++)
  27.         expr[i]=(a*i+b)%26;
  28.     int ok=0;
  29.     for(int i=0; i<25 && !ok; ++i)
  30.         for(int j=i+1; j<26 && !ok; ++j)
  31.             if(expr[i]==expr[j])
  32.                 ok=1;
  33.     if(ok)
  34.     {
  35.         fout<<-1;
  36.         return 0;
  37.     }
  38.     for(int i=0; mess[i]; ++i)
  39.         fout << (char)(modInverse(a, 26) * (26 + mess[i] - 'a' - b) % 26 + 'a');
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement