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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

int main() {
  // S A C
  const int M = 256;
  //    vector<vector<vector<vector<int> > > >R(16,vector<vector<vector<int> >
  //    >(16,vector<vector<int> >(16,vector<int>(M+1)))); for(int
  //    s=0;s<=15;s++){
  //        for(int a=0;a<=15;a++){
  //            for(int c=0;c<=15;c++){
  //                for(int i=0;i<=M;i++){
  //                    if(i == 0){
  //                        R[s][a][c][i] = s;
  //                    }else{
  //                        R[s][a][c][i] = (a*R[s][a][c][i-1]+c) % M;
  //                    }
  //                }
  //            }
  //        }
  //    }

  while (true) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    vector<int> I(N);
    vector<int> O(N);
    vector<int> Ht(N + 1);
    int ms, ma, mc;
    double mh = 100000000;
    for (int i = 0; i < N; i++) {
      cin >> I[i];
    }
    for (int s = 0; s <= 15; s++) {
      for (int a = 0; a <= 15; a++) {
        for (int c = 0; c <= 15; c++) {
          vector<int> num(M);
          int R = s;
          for (int i = 0; i < N; i++) {
            R = (a * R + c) % M;
            num[(I[i] + R) % M]++;
          }
          double H = 0;
          for (int i = 0; i < num.size(); i++) {
            if (num[i] != 0) {
              H += -(1.0 * num[i] / N * log(1.0 * num[i] / N));
            }
          }
          if (H + EPS < mh) {
            mh = H;
            ms = s;
            ma = a;
            mc = c;
          }
        }
      }
    }
    cout << ms << " " << ma << " " << mc << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

int main() {
  // S A C
  const int M = 256;
  //    vector<vector<vector<vector<int> > > >R(16,vector<vector<vector<int> >
  //    >(16,vector<vector<int> >(16,vector<int>(M+1)))); for(int
  //    s=0;s<=15;s++){
  //        for(int a=0;a<=15;a++){
  //            for(int c=0;c<=15;c++){
  //                for(int i=0;i<=M;i++){
  //                    if(i == 0){
  //                        R[s][a][c][i] = s;
  //                    }else{
  //                        R[s][a][c][i] = (a*R[s][a][c][i-1]+c) % M;
  //                    }
  //                }
  //            }
  //        }
  //    }

  while (true) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    vector<int> I(N);
    vector<int> O(N);
    vector<int> Ht(N + 1);
    int ms, ma, mc;
    double mh = 100000000;
    for (int i = 0; i < N; i++) {
      cin >> I[i];
    }
    for (int i = 0; i < N; i++) {
      for (int s = 0; s <= 15; s++) {
        for (int a = 0; a <= 15; a++) {
          for (int c = 0; c <= 15; c++) {
            vector<int> num(M);
            int R = s;
            for (int i = 0; i < N; i++) {
              R = (a * R + c) % M;
              num[(I[i] + R) % M]++;
            }
            double H = 0;
            for (int i = 0; i < num.size(); i++) {
              if (num[i] != 0) {
                H += -(1.0 * num[i] / N * log(1.0 * num[i] / N));
              }
            }
            if (H < mh) {
              mh = H;
              ms = s;
              ma = a;
              mc = c;
            }
          }
        }
      }
    }
    cout << ms << " " << ma << " " << mc << endl;
  }
  return 0;
}