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

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int I[300], R[300], O[300];
int main() {
  int N;
  const int MOD = 256;
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    FOR(i, 1, N + 1) cin >> I[i];
    int ansS, ansA, ansC;
    double ansH = 100000000.0;
    REP(A, 16) {
      REP(S, 16) {
        REP(C, 16) {
          double H = 0;
          REP(i, 300) O[i] = 0;
          R[0] = S;
          for (int i = 1; i <= N; i++) {
            R[i] = (A * R[i - 1] + C) % MOD;
            O[(I[i] + R[i]) % MOD]++;
          }

          for (int i = 0; i < 256; i++) {
            if (O[i] != 0)
              H -= (O[i] / (double)(N)*log(O[i] / (double)(N)));
          }

          if (H + 1e-11 < ansH) {
            ansH = H;
            ansS = S;
            ansA = A;
            ansC = C;
          }
        }
      }
    }
    cout << ansS << " " << ansA << " " << ansC << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  int N;
  const int MOD = 256;
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    int I[300];
    FOR(i, 1, N) cin >> I[i];
    int R[300];
    int O[300];
    int ansS, ansA, ansC;
    double ansH = 100000000.0;
    REP(S, 16) {
      REP(A, 16) {
        REP(C, 16) {
          double H = 0;
          REP(i, 300) O[i] = 0;
          R[0] = S;
          for (int i = 1; i <= N; i++) {
            R[i] = (A * R[i - 1] + C) % MOD;
            O[(I[i] + R[i]) % MOD]++;
          }

          for (int i = 0; i < 256; i++) {
            if (O[i] != 0)
              H -= (O[i] / (double)(N)*log(O[i] / (double)(N)));
          }

          if (H + 1e-11 < ansH) {
            ansH = H;
            ansS = S;
            ansA = A;
            ansC = C;
          }
        }
      }
    }
    cout << ansS << " " << ansA << " " << ansC << endl;
  }
}