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

#include <cmath>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
  int N;
  int M = 256;
  cin >> N;
  while (N != 0) {
    std::vector<int> I(N);
    for (int i = 0; i < N; i++) {
      cin >> I[i];
    }

    bool find = false;
    int R, O;
    int S, A, C;
    int now;
    double ret = 0;
    for (int s = 0; s < 16 && !(find); s++) {
      for (int a = 0; a < 16 && !(find); a++) {
        for (int c = 0; c < 16 && !(find); c++) {
          std::vector<int> counter(M, 0);
          R = s;

          for (int i = 0; i < N; i++) {
            R = ((a * R) + c) % M;
            O = (I[i] + R) % M;
            counter[O]++;
          }

          now = 0;
          for (int i = 0; i < M; i++) {
            if (counter[i] != 0) {
              now += -((double)counter[i] / N) *
                     std::log((double)counter[i] / N) * pow(10, 5);
            }
          }
          if (now == 0) {
            S = s;
            A = a;
            C = c;
            find = true;
          } else {
            if (ret == 0) {
              S = s;
              A = a;
              C = c;
              ret = now;
            } else {
              if (now < ret) {
                S = s;
                A = a;
                C = c;
                ret = now;
              }
            }
          }
        }
      }
    }
    cout << S << " " << A << " " << C << endl;
    cin >> N;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

int digit(int n, int d) {
  int m = (int)(n / pow(10, d)) * pow(10, d);
  return (n - m) / pow(10, d - 1);
}

int check_digit(int n) {
  for (int i = 15; i > 0; i--) {
    if (digit(n, i) > 0) {
      return i;
    }
  }
  return 1;
}

int main(void) {
  int N;
  int M = 256;
  cin >> N;
  while (N != 0) {
    std::vector<int> I(N);
    for (int i = 0; i < N; i++) {
      cin >> I[i];
    }

    bool find = false;
    int R, O;
    int len = 0;
    int S, A, C;
    double now;
    double ret = 0;
    for (int s = 0; s < 15 && !(find); s++) {
      for (int a = 0; a < 15 && !(find); a++) {
        for (int c = 0; c < 15 && !(find); c++) {
          std::vector<int> counter(10, 0);
          R = s;

          for (int i = 0; i < N; i++) {
            R = ((a * R) + c) % M;
            O = (I[i] + R) % M;
            len = check_digit(O);
            for (int j = 1; j < len + 1; j++) {
              counter[digit(O, j)]++;
            }
          }

          now = 0;
          for (int i = 0; i < 10; i++) {
            if (counter[i] != 0) {
              now +=
                  -((double)counter[i] / N) * std::log((double)counter[i] / N);
            }
          }
          if (now == 0) {
            S = s;
            A = a;
            C = c;
            find = true;
          } else {
            if (ret == 0) {
              S = s;
              A = a;
              C = c;
              ret = now;
            } else {
              if (now < ret) {
                S = s;
                A = a;
                C = c;
                ret = now;
              }
            }
          }
        }
      }
    }
    cout << S << " " << A << " " << C << endl;
    cin >> N;
  }
  return 0;
}