alexjercan/bugnet huggingface dataset:
Problem p00469 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define INF 100000
int min(int a, int b) { return a < b ? a : b; }
int getk(int a) {
  int res = 0;
  while (a > 0) {
    res++;
    a /= 10;
  }
  return res;
}
int pow(int a, int b) {
  int l;
  int rt = 1;
  for (l = 0; l < b; l++)
    rt *= 10;
  return rt;
}
int card[10];
int pos[10000];
int cpos;
int main() {
  int i, j;
  int n, k;
  while (1) {
    scanf("%d", &n);
    scanf("%d", &k);
    if (n == 0 && k == 0)
      return 0;
    for (i = 0; i < n; i++)
      scanf("%d", &card[i]);
    cpos = 0;
    int max = 1;
    int ket;
    int next;
    int ni;
    int num;
    int ans = 0;
    bool used[10];
    for (i = 0; i < k; i++)
      max *= n;
    for (i = 0; i < max; i++) {
    cont:
      for (j = 0; j < n; j++)
        used[j] = false;
      ket = 1;
      ni = i;
      num = 0;
      for (j = 0; j < k; j++) {
        next = ni % n;
        ni /= n;
        if (used[next]) {
          i++;
          if (i == max)
            goto end;
          goto cont;
        } else
          used[next] = true;
        num += card[next] * ket;
        ket *= pow(10, getk(card[next]));
      }
      for (j = 0; j < cpos; j++) {
        if (num == pos[j])
          break;
      }
      if (j == cpos) {
        pos[cpos++] = num;
        ans++;
      }
    }
  end:
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
#define INF 100000
int min(int a, int b) { return a < b ? a : b; }
int getk(int a) {
  int res = 0;
  while (a > 0) {
    res++;
    a /= 10;
  }
  return res;
}
int pow(int a, int b) {
  int l;
  int rt = 1;
  for (l = 0; l < b; l++)
    rt *= 10;
  return rt;
}
int card[10];
bool pos[20000000];
int main() {
  int i, j;
  int n, k;
  while (1) {
    scanf("%d", &n);
    scanf("%d", &k);
    if (n == 0)
      return 0;
    for (i = 0; i < n; i++)
      scanf("%d", &card[i]);
    for (i = 0; i < 20000000; i++)
      pos[i] = false;
    int max = 1;
    int ket;
    int next;
    int ni;
    int num;
    int ans = 0;
    bool used[10];
    for (i = 0; i < k; i++)
      max *= n;
    for (i = 0; i < max; i++) {
    cont:
      for (j = 0; j < n; j++)
        used[j] = false;
      ket = 1;
      ni = i;
      num = 0;
      for (j = 0; j < k; j++) {
        next = ni % n;
        ni /= n;
        if (used[next]) {
          i++;
          if (i == max)
            goto end;
          goto cont;
        } else
          used[next] = true;
        num += card[next] * ket;
        ket *= pow(10, getk(card[next]));
      }
      if (!pos[num]) {
        ans++;
        pos[num] = true;
      }
    }
  end:
    printf("%d\\n", ans);
  }
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

int main() {
  int n;
  while (cin >> n, n) {
    int k;
    cin >> k;
    vs cards(n);
    REP(i, n) { cin >> cards[i]; }

    vi num;
    REP(i, n) {
      if (i < k) {
        num.push_back(1);
      } else {
        num.push_back(0);
      }
    }
    sort(ALL(num));

    set<string> ans;
    do {
      vs choice;
      REP(i, n) {
        if (num[i]) {
          choice.push_back(cards[i]);
        }
      }

      sort(ALL(choice));
      do {
        string s;
        REP(i, k) { s += choice[i]; }
        ans.insert(s);
      } while (next_permutation(ALL(choice)));
    } while (next_permutation(ALL(num)));

    cout << ans.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

int main() {
  int n;
  while (cin >> n, n) {
    int k;
    cin >> k;
    vs cards(n);
    REP(i, n) { cin >> cards[i]; }

    map<string, int> num;
    sort(ALL(cards));
    do {
      string s;
      REP(i, k) { s += cards[i]; }
      num[s]++;
    } while (next_permutation(ALL(cards)));

    cout << num.size() << endl;
  }
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int func(vector<int> v, int k) {
  stringstream ss;
  rep(i, k) { ss << v[i]; }

  int ret;
  ss >> ret;

  return ret;
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    int k;
    cin >> k;

    vector<int> v(n);
    rep(i, n) cin >> v[i];

    sort(v.begin(), v.end());

    set<int> ans;
    set<vector<int>> st;

    do {
      vector<int> ret(k);
      rep(i, k) ret[i] = v[i];

      if (st.find(ret) == st.end()) {
        st.insert(ret);
        ans.insert(func(v, k));
      } else
        continue;
    } while (next_permutation(v.begin(), v.end()));

    cout << ans.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

ll func(vector<int> v, int k) {
  stringstream ss;
  rep(i, k) { ss << v[i]; }

  ll ret;
  ss >> ret;
  return ret;
}

int main() {
  int n;
  while (cin >> n && n) {
    int k;
    cin >> k;

    vector<int> v(n);
    rep(i, n) cin >> v[i];

    sort(v.begin(), v.end());

    set<ll> ans;

    do {
      ans.insert(func(v, k));
    } while (next_permutation(v.begin(), v.end()));

    cout << ans.size() << endl;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define debug(x) cerr << #x << ':' << x << endl
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef long long ll;

void dfs(int i, int cnt, string *C) {}

int main() {
  int n, k;
  while (cin >> n >> k, n | k) {
    vector<string> C(n);
    REP(j, n) cin >> C[j];
    set<string> strs;
    for (int mask = 0; mask < (1 << n); mask++) {
      int total_1 = 0;
      for (int i = 0; i < n; i++)
        if (mask >> i & 1)
          total_1++;
      if (total_1 != k)
        continue;
      vector<string> cards;
      for (int i = 0; i < n; i++)
        if (mask >> i & 1)
          cards.push_back(C[i]);
      sort(ALL(cards));
      do {
        string tmp;
        for (auto num : cards)
          tmp += num;
        if (strs.find(tmp) == strs.end())
          strs.insert(tmp);
      } while (next_permutation(ALL(cards)));
    }
    cout << strs.size() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define debug(x) cerr << #x << ':' << x << endl
using namespace std;
typedef long long ll;
const int INF = 1000000;

int main() {
  int n, k, C[1000];
  while (1) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> C[i];
    sort(C, C + n);

    set<string> nums;
    do {
      string num = "";
      for (int i = 0; i < k; i++)
        num += to_string(C[i]);
      nums.insert(num);
    } while (next_permutation(C, C + n));

    cout << nums.size() << endl;

    memset(C, 0, 1000 * sizeof(int));
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int bit(long bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

int main() {
  int n, k;
  string str;
  while (cin >> n >> k && n || k) {
    set<string> ret;
    vector<string> num(n);
    for (int i = 0; i < n; i++) {
      cin >> num[i];
    }
    sort(num.begin(), num.end());
    for (int mask = 0; mask < (1 << n); mask++) {
      if (bit(mask) != k)
        continue;
      vector<string> uuu;
      for (int j = 0; j < n; j++) {
        if (mask & (1 << j))
          uuu.push_back(num[j]);
      }

      do {
        str = "";
        for (int i = 0; i < k; i++) {
          str += uuu[i];
        }
        ret.insert(str);
      } while (next_permutation(uuu.begin(), uuu.end()));
    }
    cout << ret.size() << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  string str;
  while (cin >> n >> k && n || k) {
    set<string> ret;
    vector<string> num(n);
    for (int i = 0; i < n; i++) {
      cin >> num[i];
    }
    sort(num.begin(), num.end());
    do {
      str = "";
      for (int i = 0; i < k; i++) {
        str += num[i];
      }
      ret.insert(str);
    } while (next_permutation(num.begin(), num.end()));

    cout << ret.size() << endl;
  }
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define LL long long
static const LL INF = 1 << 50;

using namespace std;

int n, k, ans;
LL s[10001];
string num[11];
int t = 0;
void cal(string sum, int depth, int bit) {
  if (depth == k) {
    int num;
    num = atoi(sum.data());
    s[t] = num;
    t++;
    return;
  }
  for (int i = 1; i <= n; ++i) {
    int b = 1 << i;
    if (bit & b) {
      int k = bit - b;
      cal(sum + num[i], depth + 1, k);
    }
  }
}

int main() {
  while (1) {
    t = 0;
    for (int i = 0; i < 11; ++i)
      num[i] = '0';
    memset(s, 0, sizeof(s));
    int p = 1;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    string a;
    for (int i = 1; i <= n; ++i) {
      p += pow(2, i);
    }
    for (int i = 1; i <= n; ++i) {
      cin >> num[i];
    }
    cal(a, 0, p);
    sort(s, s + t);
    for (int i = 0; i < t; ++i) {
      if (s[i] && s[i] != s[i + 1]) {
        ans++;
      }
    }
    cout << ans << endl;
    ans = 0;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
static const LL INF = 1 << 50;

using namespace std;

int n, k, ans;
LL s[10001];
string num[11];
int t = 0;
void cal(string sum, int depth, int bit) {
  int res = 0;
  if (depth == k) {
    int num;
    num = atoi(sum.data());
    s[t] = num;
    t++;
  }
  for (int i = 1; i <= n; ++i) {
    int b = 1 << i;
    if (bit & b) {
      int k = bit - b;
      cal(sum + num[i], depth + 1, k);
    }
  }
}

int main() {
  while (1) {
    t = 0;
    for (int i = 0; i < 11; ++i)
      num[i] = '0';
    memset(s, 0, sizeof(s));
    int p = 1;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    string a;
    for (int i = 1; i <= n; ++i) {
      p += pow(2, i);
    }
    for (int i = 1; i <= n; ++i) {
      cin >> num[i];
    }
    cal(a, 0, p);
    sort(s, s + t);
    for (int i = 0; i < t; ++i) {
      if (s[i] && s[i] != s[i + 1]) {
        ans++;
      }
    }
    cout << ans << endl;
    ans = 0;
  }
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <string>

int n, k;
std::string cards[11];
std::set<std::string> s;

void makecard(int p, std::string res, bool used[11]) {
  if (p == k) {
    s.insert(res);
    return;
  }

  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      used[i] = true;
      makecard(p + 1, res + cards[i], used);
      used[i] = false;
    }
  }
}

int main() {

  while (std::cin >> n >> k, n || k) {
    s.clear();
    for (int i = 0; i < n; i++) {
      std::cin >> cards[i];
    }
    bool used[11];
    for (int j = 0; j < 11; j++)
      used[j] = false;

    makecard(0, "", used);

    std::cout << s.size() << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <sstream>
#include <string>

int n, k;
int cards[11];
std::set<std::string> s;

std::string toStr(int n) {
  std::stringstream z;
  std::string res;

  z << n;
  z >> res;

  return res;
}

void makecard() {
  int id[11];
  for (int i = 0; i < n; i++)
    id[i] = i;
  s.clear();

  do {
    std::string res;
    for (int i = 0; i < k; i++) {
      res += toStr(cards[id[i]]);
    }
    s.insert(res);
  } while (std::next_permutation(id, id + n));
}

int main() {

  while (std::cin >> n >> k, n || k) {

    for (int i = 0; i < n; i++) {
      std::cin >> cards[i];
    }

    makecard();

    std::cout << s.size() << std::endl;
  }
  return 0;
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
  for (int n, k; cin >> n >> k, n;) {
    vector<string> v;
    string s;
    for (; n; n--)
      cin >> s, v.push_back(s);
    map<string, int> m;
    sort(v.begin(), v.end());
    do {
      m[accumulate(v.begin(), v.begin() + k, string(),
                   [](string x, string y) { return x + y; })] = 1;
      // http://origin.googlecode.com/svn-history/r366/trunk/core/include/origin/algorithm/boost_combination.hpp
      if (v.begin() == v.begin() + k)
        break;
      reverse(v.begin() + k, v.end());
    } while (next_permutation(v.begin(), v.end()));
    cout << m.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
int main() {
  int n, k;
  map<string, bool> ret;
  while (cin >> n >> k) {
    vector<int> data(n);
    for (int i = 0; i < n; i++)
      cin >> data[i];
    sort(data.begin(), data.end());
    do {
      stringstream ss;
      for (int i = 0; i < k; i++)
        ss << data[i];
      ret[ss.str()];
    } while (next_permutation(data.begin(), data.end()));
    cout << ret.size() << endl;
  }
}
Problem p00469 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <set>

using namespace std;

set<int> se;

int N, K;

int card[10];
bool used[10];

void rec(int num, int k) {
  if (k == K) {
    se.insert(num);
    return;
  }
  for (int i = 0; i < N; i++) {
    if (used[i] == true)
      continue;
    used[i] = true;
    int nnum;
    if (card[i] < 10)
      nnum = num * 10 + card[i];
    else
      nnum = num * 100 + card[i];
    rec(nnum, k + 1);
    used[i] = false;
  }
}

int main() {
  while (true) {
    scanf("%d%d", &N, &K);
    if (N == 0 && K == 0)
      break;
    se.clear();
    for (int i = 0; i < N; i++) {
      scanf("%d", card + i);
    }
    memset(used, false, sizeof(used));
    rec(0, 0);
    printf("%d\\n", se.size());
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <set>

using namespace std;

set<int> se;

int N, K;

int card[10];
bool used[10];

void rec(int num, int k) {
  if (k == K) {
    se.insert(num);
    return;
  }
  for (int i = 0; i < N; i++) {
    if (used[i] == true)
      continue;
    used[i] = true;
    int nnum;
    if (card[i] < 10)
      nnum = num * 10 + card[i];
    else
      nnum = num * 100 + card[i];
    rec(nnum, k + 1);
    used[i] = false;
  }
}

int main() {
  while (true) {
    scanf("%d%d", &N, &K);
    se.clear();
    for (int i = 0; i < N; i++) {
      scanf("%d", card + i);
    }
    memset(used, false, sizeof(used));
    rec(0, 0);
    printf("%d\\n", se.size());
  }
  return 0;
}
Problem p00470 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;
int w, h;
int dp[101][101][2][2][3];
int solve(int x, int y, int x_f, int y_f, int prev) {
  // 1:true 0:false
  // prev 1: x , 2: y
  if (dp[y][x][x_f][y_f][prev] != -1)
    return dp[y][x][x_f][y_f][prev];
  if (x == w && y == h) {
    // ans++;
    return 1;
  }
  if (x > w || y > h)
    return 0;
  int ret = 0;
  if (x_f == 1 && y_f == 1) {
    // cout<<"ff:"<<x<<"\\t"<<y<<endl;
    if (prev == 1) {
      ret = solve(x + 1, y, 1, 1, 1) + solve(x, y + 1, 0, 1, 2);
    } else if (prev == 2) {
      ret = solve(x, y + 1, 1, 1, 2) + solve(x + 1, y, 1, 0, 1);
    } else {
      ret = solve(x, y + 1, 1, 1, 2) + solve(x + 1, y, 1, 1, 1);
    }
  } else if (x_f == 0 && y_f == 1) {
    // cout<<"ft"<<x<<"\\t"<<y<<endl;
    ret = solve(x, y + 1, 1, 1, 2);
  } else if (x_f == 1 && y_f == 0) {
    // cout<<"tf"<<x<<"\\t"<<y<<endl;
    ret = solve(x + 1, y, 1, 1, 1);
  }

  return dp[y][x][x_f][y_f][prev] = ret % 100000;
}

int main() {

  while (cin >> w >> h) {
    if (w == 0 && h == 0)
      break;
    w--;
    h--;
    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0, 1, 1, 0) % 100000 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;
int w, h;
int ans;
void solve(int x, int y, bool x_f, bool y_f, int prev) {
  // prev 1: x , 2: y
  if (x == w && y == h) {
    ans++;
    return;
  }
  if (x > w || y > h)
    return;

  if (x_f && y_f) {
    // cout<<"ff:"<<x<<"\\t"<<y<<endl;
    if (prev == 1) {
      solve(x + 1, y, true, true, 1);
      solve(x, y + 1, false, true, 2);
    } else if (prev == 2) {
      solve(x, y + 1, true, true, 2);
      solve(x + 1, y, true, false, 1);
    } else {
      solve(x, y + 1, true, true, 2);
      solve(x + 1, y, true, true, 1);
    }
  }
  if (!x_f && y_f) {
    // cout<<"ft"<<x<<"\\t"<<y<<endl;
    solve(x, y + 1, true, true, 2);
  } else if (x_f && !y_f) {
    // cout<<"tf"<<x<<"\\t"<<y<<endl;
    solve(x + 1, y, true, true, 1);
  }
}

int main() {

  while (cin >> w >> h) {
    if (w == 0 && h == 0)
      break;
    solve(1, 1, true, true, 0);
    cout << ans % 100000 << endl;
    ans = 0;
  }

  return 0;
}
Problem p00470 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int W, H;
int e1[100][100], e2[100][100], n1[100][100], n2[100][100];
const int mod = 100000;

int main() {
  while (cin >> W >> H, W) {
    for (int i = 1; i < W; i++) {
      e2[i][0] = 1;
    }
    for (int j = 1; j < H; j++) {
      n2[0][j] = 1;
      for (int i = 1; i < W; i++) {
        e1[i][j] = (n2[i - 1][j]) % mod;
        e2[i][j] = (e1[i - 1][j] + e2[i - 1][j]) % mod;
        n1[i][j] = (e2[i][j - 1]) % mod;
        n2[i][j] = (n1[i][j - 1] + n2[i][j - 1]) % mod;
      }
    }
    int s = (e1[W - 1][H - 1] + e2[W - 1][H - 1] + n1[W - 1][H - 1] +
             n2[W - 1][H - 1]) %
            mod;
    cout << s << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int W, H, dp[100][100][4];
const int mod = 100000;

int rec(int y, int x, int j) {
  if (y >= H || x >= W)
    return 0;
  if (!dp[y][x][j])
    return dp[y][x][j];
  if (y == H - 1 && x == W - 1)
    return 1;

  int ret = 0;
  if (!y && !x)
    ret = rec(y + 1, x, 0) + rec(y, x + 1, 1);
  else if (j == 0)
    ret = rec(y + 1, x, 0) + rec(y, x + 1, 3);
  else if (j == 1)
    ret = rec(y + 1, x, 2) + rec(y, x + 1, 1);
  else if (j == 2)
    ret = rec(y + 1, x, 0);
  else if (j == 3)
    ret = rec(y, x + 1, 1);

  return dp[y][x][j] = ret % mod;
}

int main() {
  while (cin >> W >> H, W) {
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        for (int k = 0; k < 4; k++) {
          dp[i][j][k] = -1;
        }
      }
    }
    cout << rec(0, 0, 0) << endl;
  }
  return 0;
}
Problem p00470 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define LL long long
static const LL INF = 1 << 24;
static const LL LL_INF = 1 << 60;
static const int mod = 1e+5;
static const int MAX_N = 101;
int dx[] = {0, 1};
int dy[] = {1, 0};
using namespace std;

int w, h;
LL mem[MAX_N][MAX_N][2][2][2][2];
LL cal(int x, int y, int px, int py, int ppx, int ppy) {
  LL res = 0;
  if (mem[x][y][px][py][ppx][ppy] != 0)
    return mem[x][y][px][py][ppx][ppy];
  if (x >= w || y >= h)
    return 0;
  if (x == w - 1 && y == h - 1)
    return 1;
  FOR(i, 0, 1) {
    if (px != ppx && py != ppy && px != dx[i] && py != dy[i])
      continue;
    res += cal(x + dx[i], y + dy[i], dx[i], dy[i], px, py) % mod;
  }
  return mem[x][y][px][py][ppx][ppy] = res % mod;
}

int main() {
  while (1) {
    cin >> w >> h;
    memset(mem, 0, sizeof(mem));
    if (w == 0 && h == 0)
      break;
    cout << cal(0, 0, 0, 0, 0, 0) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define LL long long
static const LL INF = 1 << 24;
static const LL LL_INF = 1 << 60;
static const int mod = 1e+5;
static const int MAX_N = 100;
int dx[] = {0, 1};
int dy[] = {1, 0};
using namespace std;

int w, h;
LL mem[MAX_N][MAX_N][2][2][2][2];
LL cal(int x, int y, int px, int py, int ppx, int ppy) {
  LL res = 0;
  if (mem[x][y][px][py][ppx][ppy] != 0)
    return mem[x][y][px][py][ppx][ppy];
  if (x >= w || y >= h)
    return 0;
  if (x == w - 1 && y == h - 1)
    return 1;
  FOR(i, 0, 1) {
    if (px != ppx && py != ppy && px != dx[i] && py != dy[i])
      continue;
    res += cal(x + dx[i], y + dy[i], dx[i], dy[i], px, py) % mod;
  }
  return mem[x][y][px][py][ppx][ppy] = res % mod;
}

int main() {
  while (1) {
    cin >> w >> h;
    memset(mem, 0, sizeof(mem));
    if (w == 0 && h == 0)
      break;
    cout << cal(0, 0, 0, 0, 0, 0) << endl;
  }
  return 0;
}