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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

// int solve(int n,int s)
//{
//	if(n<=0 || 10<n)
//		return 0;
//
//	vi c(n),k(10);
//	rep(i,n)
//		c[i]=i+1;
//	rep(i,10)
//		k[i]=i;
//	int res=0;
//	do{
//		if(inner_product(allof(c),k.begin(),0)==s)
//			res++;
//		reverse(n+allof(k));
//	}while(next_permutation(allof(k)));
//	return res;
// }

int memo[1 << 10][331];

void gen_answer() {
  rep(i, 10) memo[1 << i][i] = 1;
  repi(i, 1, 1 << 10) {
    int use = __builtin_popcount(i);
    rep(j, 10) {
      if (i & (1 << j))
        continue;
      int term = (use + 1) * j;
      rep(k, 331 - term) memo[i | (1 << j)][k + term] += memo[i][k];
    }
  }
}

int solve(int n, int s) {
  if (n <= 0 || 10 < n)
    return 0;
  if (s < 0 || 330 < s)
    return 0;

  int res = 0;
  rep(i, 1 << 10) {
    if (__builtin_popcount(i) == n)
      res += memo[i][s];
  }
  return res;
}

int main() {
  gen_answer();
  for (int n, s; cin >> n >> s;)
    // cout<<solve(n,s)<<endl;
    // printf("%d %d: %d\\n",n,s,solve(n,s));
    printf("%d\\n", solve(n, s));

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int solve(int n, int s) {
  if (n <= 0 || 10 < n)
    return 0;

  vi c(n), k(10);
  rep(i, n) c[i] = i + 1;
  rep(i, 10) k[i] = i;
  int res = 0;
  do {
    if (inner_product(allof(c), k.begin(), 0) == s)
      res++;
    reverse(n + allof(k));
  } while (next_permutation(allof(k)));
  return res;
}

int main() {
  for (int n, s; cin >> n >> s;)
    // cout<<solve(n,s)<<endl;
    // printf("%d %d: %d\\n",n,s,solve(n,s));
    printf("%d\\n", solve(n, s));

  return 0;
}