fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  5. #define endl "\n"
  6. #define inarr(a,n) for(int i=0;i<n;i++) cin>>a[i]
  7. #define outarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl
  8. #define maxa(a,n) *max_element(a,a+n)
  9. #define mina(a,n) *min_element(a,a+n)
  10. #define Max(x,y,z) max(x,max(y,z))
  11. #define Min(x,y,z) min(x,min(y,z))
  12. #define fr(i,s,e) for(ll i=s;i<e;i++)
  13. #define rf(i,s,e) for(ll i=s-1;i>=e;i--)
  14. #define eb emplace_back
  15. #define pb push_back
  16. #define mp make_pair
  17. #define mt make_tuple
  18. #define F first
  19. #define S second
  20. #define sz size()
  21. #define all(v) v.begin(),v.end()
  22.  
  23. typedef int ll;
  24. typedef unsigned int uint;
  25. typedef unsigned long long ull;
  26. typedef pair<int, int> pii;
  27. typedef pair<long long, long long> pll;
  28. typedef vector<int> vi;
  29. typedef vector<long long> vll;
  30. typedef vector<pair<long long,long long> > vpll;
  31.  
  32.  
  33. #define PI 3.141592653589793
  34. #define MOD 1000000007
  35.  
  36. bool flag=0,flag1=0,flag2=0;
  37. ll d,x,y;
  38. ll p=7919,b=11,len; // len - overall length of inut string
  39.  
  40. void Eu(ll a,ll v)
  41. {
  42. if(v==0)
  43. {
  44. d=a;
  45. x=1;
  46. y=0;
  47. }
  48. else
  49. {
  50. Eu(v,a%v);
  51. ll temp=x;
  52. x=y;
  53. y=temp-(a/v)*y;
  54. }
  55. }
  56. ll Eux(ll a,ll m)
  57. {
  58. Eu(a%m,m);
  59. return (x%m+m)%m;
  60. }
  61.  
  62. string encode(int p,int b,string a)
  63. {
  64. fr(i,0,len)
  65.  
  66. a[i]=(char)((int)((p*(int)a[i]+b)+31)%158);
  67.  
  68. return a;
  69. }
  70.  
  71. string decode(int p,int b,string c)
  72. {
  73. ll inverse=Eux(p,158);
  74.  
  75. fr(i,0,len)
  76. c[i]=(char)(((((((int)c[i]+127)%127-b)*inverse)-31)%127));
  77.  
  78. return c;
  79. }
  80.  
  81. int main()
  82. {
  83. IOS
  84. int p,b;
  85. cin>>p>>b;
  86. string a,e,c; getline(cin,a);
  87.  
  88. len=a.length(); // input string- a , encoded string- b , decoded string- c
  89.  
  90. e=encode(p,b,a);
  91. c=decode(p,b,e);
  92.  
  93. cout<<e<<endl;
  94. cout<<c<<endl;
  95. // cout<<(p*(int)'a'+b)<<endl;
  96. // cout<<(char)((((p*(int)''+b)%127-b)*Eux(p,127))%127);
  97. }
Success #stdin #stdout 0.01s 5292KB
stdin
2 3 hey guys this is our message which we want to encode and decode.
 
stdout
bTNvbRnvjblTVjbVjbbnhb^NjjFRNbrTVJTbrNbrF`lblbbN`JbLNbF`LbLNJbLN~
@2,T@0LTH@J24H@4H@@LF@<,HH$0,@P24(2@P,@P$>J@J@@,>(@*,@$>*@*,(@*,\