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

#include <algorithm>
#include <iostream>
using namespace std;

int n, s, ans;
int f[330];

void cheat() {
  f[200] = 13918;
  f[201] = 14519;
  f[202] = 15611;
  f[203] = 16278;
  f[204] = 16780;
  f[205] = 18686;
  f[206] = 19280;
  f[207] = 19586;
  f[208] = 20795;
  f[209] = 22385;
  f[210] = 22896;
  f[211] = 23948;
  f[212] = 24970;
  f[213] = 26012;
  f[214] = 27096;
  f[215] = 28467;
  f[216] = 28427;
  f[217] = 30540;
  f[218] = 31272;
  f[219] = 31774;
  f[220] = 33264;
  f[221] = 34748;
  f[222] = 34499;
  f[223] = 36299;
  f[224] = 36596;
  f[225] = 38456;
  f[226] = 38926;
  f[227] = 39984;
  f[228] = 39386;
  f[229] = 42068;
  f[230] = 42848;
  f[231] = 42424;
  f[232] = 42925;
  f[233] = 45044;
  f[234] = 44584;
  f[235] = 46040;
  f[236] = 45496;
  f[237] = 46890;
  f[238] = 47036;
  f[239] = 47341;
  f[240] = 47646;
  f[241] = 48887;
  f[242] = 48840;
  f[243] = 48044;
  f[244] = 48540;
  f[245] = 50066;
  f[246] = 48970;
  f[247] = 49062;
  f[248] = 49062;
  f[249] = 48970;
  f[250] = 50066;
  f[251] = 48540;
  f[252] = 48044;
  f[253] = 48840;
  f[254] = 48887;
  f[255] = 47646;
  f[256] = 47341;
  f[257] = 47036;
  f[258] = 46890;
  f[259] = 45496;
  f[260] = 46040;
  f[261] = 44584;
  f[262] = 45044;
  f[263] = 42925;
  f[264] = 42424;
  f[265] = 42848;
  f[266] = 42068;
  f[267] = 39386;
  f[268] = 39984;
  f[269] = 38926;
  f[270] = 38456;
  f[271] = 36596;
  f[272] = 36299;
  f[273] = 34499;
  f[274] = 34748;
  f[275] = 33264;
  f[276] = 31774;
  f[277] = 31272;
  f[278] = 30540;
  f[279] = 28427;
  f[280] = 28467;
  f[281] = 27096;
  f[282] = 26012;
  f[283] = 24970;
  f[284] = 23948;
  f[285] = 22896;
  f[286] = 22385;
  f[287] = 20795;
  f[288] = 19586;
  f[289] = 19280;
  f[290] = 18686;
  f[291] = 16780;
  f[292] = 16278;
  f[293] = 15611;
  f[294] = 14519;
  f[295] = 13918;
  f[296] = 13026;
  f[297] = 12141;
  f[298] = 11647;
  f[299] = 10678;
  f[300] = 9892;
  f[301] = 9392;
  f[302] = 8773;
  f[303] = 7830;
  f[304] = 7424;
  f[305] = 6909;
  f[306] = 6156;
  f[307] = 5672;
  f[308] = 5130;
  f[309] = 4493;
  f[310] = 4305;
  f[311] = 3676;
  f[312] = 3180;
  f[313] = 2837;
  f[314] = 2533;
  f[315] = 2010;
  f[316] = 1824;
  f[317] = 1528;
  f[318] = 1252;
  f[319] = 1040;
  f[320] = 819;
  f[321] = 619;
  f[322] = 498;
  f[323] = 360;
  f[324] = 234;
  f[325] = 177;
  f[326] = 107;
  f[327] = 51;
  f[328] = 28;
  f[329] = 9;
}

void solve(int k, int sum, int bits) {
  if (sum < 0)
    return;

  if (k == n + 1) {
    if (sum == 0)
      ans++;
    return;
  }

  for (int i = 0; i <= 9; i++) {
    if (bits & (1 << i))
      continue;
    solve(k + 1, sum - i * k, bits | (1 << i));
  }
}

int main() {
  cheat();
  while (cin >> n >> s) {
    int max_sum = 0;
    int a[10];
    for (int i = 0; i < n; i++) {
      a[i] = 10 - n + i;
    }
    for (int i = 1; i <= n; i++) {
      max_sum += i * a[i - 1];
    }

    if (s > max_sum) {
      cout << 0 << endl;
    } else if (s == max_sum) {
      cout << 1 << endl;
    } else {
      if (n == 10 && s >= 200) {
        cout << f[s] << endl;
      } else {
        ans = 0;
        solve(1, s, 0);
        cout << ans << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int n, s, ans;

void solve(int k, int sum, int bits) {
  if (sum < 0)
    return;

  if (k == n + 1) {
    if (sum == 0)
      ans++;
    return;
  }

  for (int i = 0; i <= 9; i++) {
    if (bits & (1 << i))
      continue;
    solve(k + 1, sum - i * k, bits | (1 << i));
  }
}

int main() {
  while (cin >> n >> s) {
    int max_sum = 0;
    int f[10];
    for (int i = 0; i < n; i++) {
      f[i] = 10 - n + i;
    }
    for (int i = 1; i <= n; i++) {
      max_sum += i * f[i - 1];
    }

    if (s > max_sum) {
      cout << 0 << endl;
    } else if (s == max_sum) {
      cout << 1 << endl;
    } else {
      ans = 0;
      solve(1, s, 0);
      cout << ans << endl;
    }
  }
}