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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 109;
const int P = 1000000007;

long long ksm(long long a, int k) {
  long long ret = 1;
  a = (a % P + P) % P;
  for (; k; k >>= 1, a = a * a % P) {
    if (k & 1)
      ret = ret * a % P;
  }
  return ret;
}
long long INV(long long x) { return ksm(x, P - 2); }

struct STR {
  int n;
  long long xx[maxn], yy[maxn];
  long long L(long long x) {
    long long ret = 0;
    for (int i = 1; i <= n; ++i) {
      long long tmp = 1;
      for (int j = 1; j <= n; ++j) {
        if (i == j)
          continue;
        tmp = tmp * INV(xx[i] - xx[j] + P) % P * (x - xx[j] + P) % P;
      }
      ret = (ret + tmp * yy[i]) % P;
    }
    return ret;
  }
} F0, F1;

long long C(int n) { return n * (n - 1) * (n - 2) * (n - 3) / 1 / 2 / 3 / 4; }

int main() {
  for (int i = 0; i <= 40; ++i) {
    long long tmp = 0;
    for (int a = 1; a <= i; ++a) {
      for (int b = 1; b <= i; ++b) {
        for (int c = 1; c <= i; ++c) {
          for (int d = 1; d <= i; ++d) {
            for (int e = 1; e <= i; ++e) {
              int t = i - a - b - c - d - e;
              int k = a * b * c * d * e;
              if (t < 0)
                continue;
              t >>= 1;
              t += 5;
              while (t >= 4) {
                tmp = (tmp + k * C(t - 1)) % P;
                --t;
              }
            }
          }
        }
      }
    }
    if (i & 1) {
      int t = ++F1.n;
      F1.xx[t] = i;
      F1.yy[t] = tmp;
    } else {
      int t = ++F0.n;
      F0.xx[t] = i;
      F0.yy[t] = tmp;
    }
  }

  int Tn;
  scanf("%d", &Tn);
  while (Tn--) {
    long long x;
    scanf("%lld", &x);
    if (x & 1)
      printf("%lld\\n", F1.L(x));
    else
      printf("%lld\\n", F0.L(x));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 109;
const int P = 1000000007;

long long ksm(long long a, int k) {
  long long ret = 1;
  a = (a % P + P) % P;
  for (; k; k >>= 1, a = a * a % P) {
    if (k & 1)
      ret = ret * a % P;
  }
  return ret;
}
long long INV(long long x) { return ksm(x, P - 2); }

struct STR {
  int n;
  long long xx[maxn], yy[maxn];
  long long L(long long x) {
    long long ret = 0;
    for (int i = 1; i <= n; ++i) {
      long long tmp = 1;
      for (int j = 1; j <= n; ++j) {
        if (i == j)
          continue;
        tmp = tmp * INV(xx[i] - xx[j] + P) % P * (x - xx[j] + P) % P;
      }
      ret = (ret + tmp * yy[i]) % P;
    }
    return ret;
  }
} F0, F1;

int lim, ans;
int a[maxn];
void get() {
  long long tmp = 1;
  for (int i = 2; i <= 10; i += 2)
    tmp = tmp * (a[i] - a[i - 1]);
  ans = (ans + tmp) % P;
}
void dfs(int x, int s) {
  if (s > lim)
    return;
  if (x == 11) {
    get();
    return;
  }
  int l = (x & 1) ? 0 : a[x - 1] + 1;
  for (int i = l; i <= lim; ++i) {
    a[x] = i;
    dfs(x + 1, s + i);
  }
}
int main() {
  for (int i = 0; i <= 30; ++i) {
    lim = i;
    ans = 0;
    dfs(1, 0);
    if (i & 1) {
      int t = ++F1.n;
      F1.xx[t] = i;
      F1.yy[t] = ans;
    } else {
      int t = ++F0.n;
      F0.xx[t] = i;
      F0.yy[t] = ans;
    }
  }

  //	for(int i=1;i<=F0.n;++i){
  //		cout<<F0.xx[i]<<' '<<F0.yy[i]<<endl;
  //	}
  //
  //	F0.n=10;
  //	for(int i=F0.xx[10]+2;i<=40;i+=2){
  //		cout<<i<<' '<<F0.L(i)<<endl;
  //	}
  int Tn;
  scanf("%d", &Tn);
  while (Tn--) {
    long long x;
    scanf("%lld", &x);
    if (x & 1)
      printf("%lld\\n", F1.L(x));
    else
      printf("%lld\\n", F0.L(x));
  }
  return 0;
}