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


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#endif

#define ITER(c) __typeof__((c).begin())
#define FOREACH(it, c) for (ITER(c) it = (c).begin(); it != (c).end(); ++it)
#define RITER(c) __typeof__((c).rbegin())
#define RFOREACH(it, c) for (RITER(c) it = (c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n) REPEAT(i, 0, n)
#define RREP(i, n) RREPEAT(i, 0, n);
#define REPEAT(i, k, n) for (int i = (k); i < (k + n); ++i)
#define RREPEAT(i, k, n) for (int i = (k) + (n)-1; i >= (k); ++i)
#define ALL(c) (c).begin(), (c).end()
#define LLPOW(p1, p2) ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A) (sizeof(A) / sizeof((A)[0]))
#define MIN(p1, p2) (((p1) < (p2)) ? (p1) : (p2))
#define MAX(p1, p2) (((p1) > (p2)) ? (p1) : (p2))
#define CIN_NO_SYNC                                                            \\
  do {                                                                         \\
    cin.tie(0);                                                                \\
    ios_base::sync_with_stdio(false);                                          \\
  } while (0)
#define GETSTR(p) fgets((p), sizeof(p), stdin)
#define CHOMP(p)                                                               \\
  do {                                                                         \\
    char *_q = (p) + strlen(p) - 1;                                            \\
    if (*_q == '\\n' || *_q == '\\r')                                            \\
      *_q = 0;                                                                 \\
  } while (0)
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;

#define X real()
#define Y imag()
typedef double D;
typedef complex<D> P;
#define EPS (1e-9)

#ifdef _DEBUG
#define pf printf
#define pr(n)                                                                  \\
  do {                                                                         \\
    cout << #n " = " << (n) << endl;                                           \\
  } while (0)
#else
#define pf(...) /* */
#define pr(...) /* */
#endif

inline void bitset(unsigned int &p, int n) { p |= (1 << n); }
inline void bitunset(unsigned int &p, int n) { p &= ~(1 << n); }
inline bool bitisset(unsigned int p, int n) { return !!(p & (1 << n)); };
inline int bitcount(int n) {
  int c = 0;
  for (; n; n &= n - 1)
    c++;
  return c;
}

int memo[11][331][1 << 10];
int hit = 0;

int rec(int n, int idx, int s, unsigned int used) {
  int count = 0;
  if (n < idx) {
    if (s == 0) {
      // pf("hit!!\\n");
      return 1;
    }
    return 0;
  }

  if (memo[n - idx][s][used] >= 0) {
    hit++;
    return memo[n - idx][s][used];
  }

  REP(i, 10) {
    if (bitisset(used, i))
      continue;
    if (s < idx * i)
      break;

    unsigned int used2 = used;
    bitset(used2, i);

    // #ifdef _DEBUG
    //         REP(j,idx-1) printf("   ");
    //         printf("%d*%d : %d %08x\\n", idx, i, s, used);
    // #endif
    count += rec(n, idx + 1, s - idx * i, used2);
  }
  memo[n - idx][s][used] = count;
  return count;
}

void solve2(int n, int s) {
  memset(memo, -1, sizeof(memo));
  if (s > 0 * 1 + 1 * 2 + 2 * 3 + 3 * 4 + 4 * 5 + 5 * 6 + 6 * 7 + 7 * 8 +
              8 * 9 + 9 * 10) { // 330
    cout << 0 << endl;
    return;
  }
  int count = rec(n, 1, s, 0);
  cout << count << endl;
}

// TLEする
void solve1(int n, int s) {
  char t[10];
  REP(i, 10) t[i] = i;
  int count = 0;
  int pre = -1;
  do {
    if (t[n - 1] == pre)
      continue;
    // REP(i,n) pf("%d ", t[i]); pf("\\n");
    int total = 0;
    REP(i, n) total += t[i] * (i + 1);
    if (total == s) {
      // REP(i,n) pf("%d*%d ", i+1, t[i]); pf("\\n");
      count++;
    }
    pre = t[n - 1];
  } while (next_permutation(t, t + 10));
  cout << count << endl;
}

int main() {
  int n, s;
  while (cin >> n >> s) {
    solve2(n, s);
  }
  return 0;
}

Error: TLE

Fixed submission:


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#endif

#define ITER(c) __typeof__((c).begin())
#define FOREACH(it, c) for (ITER(c) it = (c).begin(); it != (c).end(); ++it)
#define RITER(c) __typeof__((c).rbegin())
#define RFOREACH(it, c) for (RITER(c) it = (c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n) REPEAT(i, 0, n)
#define RREP(i, n) RREPEAT(i, 0, n);
#define REPEAT(i, k, n) for (int i = (k); i < (k + n); ++i)
#define RREPEAT(i, k, n) for (int i = (k) + (n)-1; i >= (k); ++i)
#define ALL(c) (c).begin(), (c).end()
#define LLPOW(p1, p2) ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A) (sizeof(A) / sizeof((A)[0]))
#define MIN(p1, p2) (((p1) < (p2)) ? (p1) : (p2))
#define MAX(p1, p2) (((p1) > (p2)) ? (p1) : (p2))
#define CIN_NO_SYNC                                                            \\
  do {                                                                         \\
    cin.tie(0);                                                                \\
    ios_base::sync_with_stdio(false);                                          \\
  } while (0)
#define GETSTR(p) fgets((p), sizeof(p), stdin)
#define CHOMP(p)                                                               \\
  do {                                                                         \\
    char *_q = (p) + strlen(p) - 1;                                            \\
    if (*_q == '\\n' || *_q == '\\r')                                            \\
      *_q = 0;                                                                 \\
  } while (0)
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;

#define X real()
#define Y imag()
typedef double D;
typedef complex<D> P;
#define EPS (1e-9)

#ifdef _DEBUG
#define pf printf
#define pr(n)                                                                  \\
  do {                                                                         \\
    cout << #n " = " << (n) << endl;                                           \\
  } while (0)
#else
#define pf(...) /* */
#define pr(...) /* */
#endif

inline void bitset(unsigned int &p, int n) { p |= (1 << n); }
inline void bitunset(unsigned int &p, int n) { p &= ~(1 << n); }
inline bool bitisset(unsigned int p, int n) { return !!(p & (1 << n)); };
inline int bitcount(int n) {
  int c = 0;
  for (; n; n &= n - 1)
    c++;
  return c;
}

int rec(int n, int idx, int s, unsigned int used) {
  int count = 0;
  if (n < idx) {
    if (s == 0) {
      // pf("hit!!\\n");
      return 1;
    }
    return 0;
  }
  REP(i, 10) {
    if (bitisset(used, i))
      continue;
    if (s < idx * i)
      break;

    unsigned int used2 = used;
    bitset(used2, i);

    // #ifdef _DEBUG
    //         REP(j,idx-1) printf("   ");
    //         printf("%d*%d : %d %08x\\n", idx, i, s, used);
    // #endif
    count += rec(n, idx + 1, s - idx * i, used2);
  }
  return count;
}

void solve2(int n, int s) {
  int count = rec(n, 1, s, 0);
  cout << count << endl;
}

// TLEする
void solve1(int n, int s) {
  char t[10];
  REP(i, 10) t[i] = i;
  int count = 0;
  int pre = -1;
  do {
    if (t[n - 1] == pre)
      continue;
    // REP(i,n) pf("%d ", t[i]); pf("\\n");
    int total = 0;
    REP(i, n) total += t[i] * (i + 1);
    if (total == s) {
      // REP(i,n) pf("%d*%d ", i+1, t[i]); pf("\\n");
      count++;
    }
    pre = t[n - 1];
  } while (next_permutation(t, t + 10));
  cout << count << endl;
}

int main() {
  int n, s;
  while (cin >> n >> s) {
    solve2(n, s);
  }
  return 0;
}