Note: There are appear to be trailing spaces on some lines, so gets() will not work =( .. Argh! Spent like an hour trying to figure out why it didn't work, even though I encrypted a file and decrypted to the same file and everything was the same. Gah =P
Guess what I did the same mistake at first. It was only when I finished coding tried the input 5 cs. and got the output czq or sth and I realized I was actually doing it WRONG because I didn't read the problem statement carefully. You did the same mistake. You have to convert the ciphertext back to plaincode and not vice versa! I doubt this will help you since it has been over 4 years...but may help future solvers.
Best wishes.
And btw...don't worry gets() works...no dirty inputs now (I think Carlos fixed it)
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson
How is this derived from the first formula? I don't quite understand that. In addition, there have to be some assertions about the min. and max. value of those code values, right?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int gcd(int a, int b)
{
int t;
while (a % b) t = a, a = b, b = t % b;
return b;
}
int plaincode[80], ciphercode[80];
int main(int argc, char *argv[])
{
string letters = "_abcdefghijklmnopqrstuvwxyz.";
srand(time(NULL));
for (int cases = 1; cases <= 100; cases++)
{
int n = rand() % 70 + 1;
while (true)
{
int k = rand() % 300 + 1;
if (gcd(k, n) == 1)
{
for (int i = 0; i < n; i++)
plaincode[i] = rand() % 28;
for (int i = 0; i < n; i++)
ciphercode[i] = (plaincode[(k * i) % n] - i + 84) % 28;
cout << k << ' ';
for (int i = 0; i < n; i++)
cout << letters[ciphercode[i]];
cout << '\n';
break;
}
}
}
cout << 0 << '\n';
return 0;
}