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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const long double EPS = 1e-13;
const long double PI = acos(-1.0);
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
long double ABS(long double a) { return max(a, -a); }
int c[700];
long long f[700];
long long C[700][10];
int main() {
  long long a;
  scanf("%lld", &a);
  C[0][0] = 1;
  for (int i = 0; i < 650; i++) {
    for (int j = 0; j < 9; j++) {
      C[i + 1][j] += C[i][j];
      C[i + 1][j + 1] += C[i][j];
    }
  }
  for (int i = 0; i < 600; i++) {
    f[i] = C[i + 7][7];
  }
  for (int i = 599; i >= 0; i--) {
    c[i] = a / f[i];
    a %= f[i];
  }
  for (int i = 0; i < 600; i++) {
    printf("FESTIVA");
    for (int j = 0; j < c[i]; j++)
      printf("L");
  }
  printf("\\n");
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = 500000004;
const double EPS = 1e-6;
const double PI = acos(-1.0);
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
long long A[10];
long long B[10];
char wolf[10] = "FESTIVAL";
int main() {
  long long K;
  scanf("%lld", &K);
  if (K < 4950) {
    printf("FESTIVA");
    for (int i = 0; i < K; i++)
      printf("L");
    printf("\\n");
    return 0;
  }
  int lim = max(7, (int)(pow((double)K, 0.125)));
  // printf("%f\\n",(pow((double)K,0.125)));return 0;
  //	if(K>10000000000LL)lim=10;
  // int lim=7;
  while (1) {
    for (int i = 0; i < 8; i++) {
      A[i] = rand() % lim + 1;
    }
    int len = 0;
    for (int i = 0; i < 8; i++)
      len += A[i];

    int UL = 1;
    for (int i = 0; i < 7; i++)
      B[i] = rand() % lim + 1;
    for (int i = 7; i < 8; i++) {
      int left = UL;
      int right = 5050;
      while (left + 1 < right) {
        int M = (left + right) / 2;
        for (int j = i; j < 8; j++) {
          B[j] = M;
        }
        long long rem = K;
        for (int j = 0; j <= 8; j++) {
          long long now = 1;
          for (int k = 0; k < 8; k++) {
            long long mul = A[k];
            if (j <= k)
              mul = B[k];
            if (rem / now < mul) {
              rem = -1;
              break;
            }
            now *= mul;
          }
          rem -= now;
          if (rem < 0)
            break;
        }
        if (rem < 0) {
          right = M;
        } else
          left = M;
      }
      B[i] = left;
      UL = B[i];
    }

    int tot = 0;
    for (int i = 0; i < 8; i++)
      tot += A[i] + B[i];
    if (tot > 5000)
      continue;
    long long val = 0;
    for (int i = 0; i <= 8; i++) {
      long long tmp = 1;
      for (int j = 0; j < 8; j++) {
        if (i <= j)
          tmp *= B[j];
        else
          tmp *= A[j];
      }
      val += tmp;
    }
    //	for(int i=0;i<8;i++)printf("%lld ",A[i]);
    //		printf(":");
    //	for(int i=0;i<8;i++)printf("%lld ",B[i]);
    //		printf(": %lld\\n",val);
    if (val == K) {
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < A[i]; j++)
          printf("%c", wolf[i]);
      }
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < B[i]; j++)
          printf("%c", wolf[i]);
      }
      printf("\\n");
      return 0;
    }
  }
}