Problem p01283 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <map>
#define INF 10000000000
using namespace std;

int memo[300][20][20][20];

int R(int i, int S, int A, int C, int M) {
  if (memo[i][S][A][C] != -1)
    return memo[i][S][A][C];
  if (i == 0)
    return S;
  return memo[i][S][A][C] = (A * R(i - 1, S, A, C, M) + C) % M;
}

int O(int i, int I, int M, int S, int A, int C) {
  return (I + R(i, S, A, C, M)) % M;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  int I[300];
  int I_[300];
  for (int i = 0; i < 300; i++)
    for (int j = 0; j < 20; j++)
      for (int k = 0; k < 20; k++)
        for (int l = 0; l < 20; l++)
          memo[i][j][k][l] = -1;
  while (cin >> N, N) {
    for (int i = 0; i < N; i++)
      cin >> I[i];

    int ansS = -1, ansA = -1, ansC = -1, M = 256;
    double entropy = INF;
    for (int S = 0; S <= 15; S++) {
      for (int A = 0; A <= 15; A++) {
        for (int C = 0; C <= 15; C++) {
          double H = 0;
          map<int, int> occurence;
          for (int i = 0; i < N; i++) {
            I_[i] = O(i + 1, I[i], M, S, A, C);
            if (occurence.find(I_[i]) == occurence.end()) {
              occurence[I_[i]] = 1;
            } else {
              occurence[I_[i]] += 1;
            }
          }

          for (map<int, int>::iterator it = occurence.begin();
               it != occurence.end(); it++) {
            H -= ((*it).second / (double)N) * log((*it).second / (double)N);
          }
          // entropy = min(entropy, H);
          if (abs(entropy - H) < 0.00001) {
            if (ansS > S) {
              ansS = S;
              ansA = A;
              ansC = C;
            } else if (ansS == S) {
              if (ansA > A) {
                ansS = S;
                ansA = A;
                ansC = C;
              } else if (ansA == A) {
                if (ansC > C) {
                  ansS = S;
                  ansA = A;
                  ansC = C;
                }
              }
            }
          } else if (entropy > H) {
            entropy = H;
            ansS = S;
            ansA = A;
            ansC = C;
          }
        }
      }
    }
    cout << ansS << " " << ansA << " " << ansC << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <map>
#define INF 100000000
using namespace std;

int R(int i, int S, int A, int C, int M) {
  if (i == 0)
    return S;
  return (A * R(i - 1, S, A, C, M) + C) % M;
}

int O(int i, int I, int M, int S, int A, int C) {
  return (I + R(i, S, A, C, M)) % M;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  int I[300];
  int I_[300];
  while (cin >> N, N) {
    for (int i = 0; i < N; i++)
      cin >> I[i];

    int ansS = -1, ansA = -1, ansC = -1, M = 256;
    double entropy = INF;
    for (int S = 0; S <= 15; S++) {
      for (int A = 0; A <= 15; A++) {
        for (int C = 0; C <= 15; C++) {
          double H = 0;
          map<int, int> occurence;
          for (int i = 0; i < N; i++) {
            I_[i] = O(i + 1, I[i], M, S, A, C);
            if (occurence.find(I_[i]) == occurence.end()) {
              occurence[I_[i]] = 1;
            } else {
              occurence[I_[i]] += 1;
            }
          }

          for (map<int, int>::iterator it = occurence.begin();
               it != occurence.end(); it++) {
            H -= ((*it).second / (double)N) * log((*it).second / (double)N);
          }
          // entropy = min(entropy, H);
          if (entropy > H) {
            entropy = H;
            ansS = S;
            ansA = A;
            ansC = C;
          }
        }
      }
    }
    cout << ansS << " " << ansA << " " << ansC << endl;
  }
}