alexjercan/bugnet huggingface dataset:
Problem p01203 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
int p[20];
int q[20];
int dp[1 << 14];
int sum[1 << 14];
int main() {
  int a, b;
  while (scanf("%d%d", &a, &b), a) {
    for (int i = 0; i < a; i++)
      scanf("%d%d", p + i, q + i);
    for (int i = 0; i < (1 << a); i++) {
      sum[i] = 0;
      for (int j = 0; j < a; j++)
        if (i & (1 << j))
          sum[i] += q[j];
    }
    for (int i = 0; i < (1 << a); i++)
      dp[i] = 99999999;
    dp[0] = 0;
    for (int i = 0; i < (1 << a); i++) {
      int now = (1 << a) - 1 - i;
      int fr = b;
      for (int j = 0; j < a; j++)
        if (i & (1 << j))
          fr += p[j] - q[j];
      while (now) {
        int tmp = sum[now];
        if (fr >= tmp) {
          dp[i + now] = min(dp[i + now], dp[i] + 1);
        }
        now = (now - 1) & ((1 << a) - 1 - i);
      }
    }
    if (dp[(1 << a) - 1] > 99999)
      printf("Impossible\\n");
    else
      printf("%d\\n", dp[(1 << a) - 1]);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int p[20];
int q[20];
int dp[1 << 14];
int main() {
  int a, b;
  while (scanf("%d%d", &a, &b), a) {
    for (int i = 0; i < a; i++)
      scanf("%d%d", p + i, q + i);
    for (int i = 0; i < (1 << a); i++)
      dp[i] = 99999999;
    dp[0] = 0;
    for (int i = 0; i < (1 << a); i++) {
      int now = (1 << a) - 1 - i;
      int fr = b;
      for (int j = 0; j < a; j++)
        if (i & (1 << j))
          fr += p[j] - q[j];
      while (now) {
        int tmp = 0;
        for (int j = 0; j < a; j++)
          if (now & (1 << j))
            tmp += q[j];
        if (fr >= tmp) {
          dp[i + now] = min(dp[i + now], dp[i] + 1);
        }
        now = (now - 1) & ((1 << a) - 1 - i);
      }
    }
    if (dp[(1 << a) - 1] > 99999)
      printf("Impossible\\n");
    else
      printf("%d\\n", dp[(1 << a) - 1]);
  }
}
Problem p01205 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1010;
const int MAXM = 13;
int N, M;
int L[MAXN];
int dp[MAXN][1 << MAXM], prevj[MAXN][1 << MAXM];

int main() {
  while (cin >> N >> M && (N | M)) {
    for (int i = 0; i < N; ++i) {
      cin >> L[i];
    }
    memset(dp, 0, sizeof(dp));
    for (int j = 0; j < (1 << M); ++j) {
      dp[1][j] = (__builtin_popcount(j) == L[0]);
    }
    for (int i = 1; i < N; ++i) {
      for (int j = 0; j < (1 << M); ++j) {
        if (dp[i][j] == 0)
          continue;
        for (int k = 0; k < 2; ++k) {
          int nj = (j | (k << M)) >> 1;
          if (__builtin_popcount(nj) != L[i])
            continue;
          dp[i + 1][nj] = min(2, dp[i + 1][nj] + dp[i][j]);
          prevj[i + 1][nj] = j;
        }
      }
    }
    int b = -1;
    for (int j = 0; j < (1 << M); ++j) {
      if (dp[N][j])
        b = j;
    }
    if (b != -1) {
      vector<int> v;
      int i, j;
      for (i = N, j = b; i > 1; --i) {
        int nj = prevj[i][j];
        v.push_back(j >> (M - 1) & 1);
        j = nj;
      }
      for (int k = M - 1; k >= 0; --k) {
        v.push_back(j >> k & 1);
      }
      reverse(v.begin(), v.end());
      for (int k = 0; k < v.size(); ++k) {
        cout << v[k];
      }
      cout << endl;
    } else {
      cout << "Goofy" << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1010;
const int MAXM = 13;
int N, M;
int L[MAXN];
int dp[MAXN][1 << MAXM], previ[MAXN][1 << MAXM], prevj[MAXN][1 << MAXM],
    prevk[MAXN][1 << MAXM];

int main() {
  while (cin >> N >> M && (N | M)) {
    for (int i = 0; i < N; ++i) {
      cin >> L[i];
    }
    memset(dp, 0, sizeof(dp));
    for (int j = 0; j < (1 << M); ++j) {
      dp[1][j] = (__builtin_popcount(j) == L[0]);
    }
    for (int i = 1; i < N; ++i) {
      for (int j = 0; j < (1 << M); ++j) {
        for (int k = 0; k < 2; ++k) {
          int nj = (j | (k << M)) >> 1;
          if (__builtin_popcount(nj) != L[i])
            continue;
          dp[i + 1][nj] = min(2, dp[i + 1][nj] + dp[i][j]);
          previ[i + 1][nj] = i;
          prevj[i + 1][nj] = j;
          prevk[i + 1][nj] = k;
        }
      }
    }
    int b = -1;
    for (int j = 0; j < (1 << M); ++j) {
      if (dp[N][j])
        b = j;
    }
    if (b != -1) {
      vector<int> v;
      int i, j;
      for (i = N, j = b; i > 1;) {
        int ni = previ[i][j];
        int nj = prevj[i][j];
        int nk = prevk[i][j];
        v.push_back(nk);
        i = ni;
        j = nj;
      }
      for (int k = M - 1; k >= 0; --k) {
        v.push_back(j >> k & 1);
      }
      reverse(v.begin(), v.end());
      for (int k = 0; k < v.size(); ++k) {
        cout << v[k];
      }
      cout << endl;
    } else {
      cout << "Goofy" << endl;
    }
  }
  return 0;
}
Problem p01205 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

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

using namespace std;

int main() {
  for (int n, m; scanf("%d%d", &n, &m), n;) {
    int a[1000];
    rep(i, n) scanf("%d", a + i);

    bool ok;
    int b[1012];
    rep(S, 1 << m) {
      int sum = 0;
      rep(i, m) sum += b[i] = S >> i & 1;
      rep(i, n - 1) b[m + i] = a[i + 1] - a[i] + b[i];

      ok = (sum == a[0]);
      rep(i, n + m - 1) if (b[i] != 0 && b[i] != 1) ok = false;
      if (ok)
        break;
    }

    if (ok) {
      rep(i, n + m - 1) printf("%d", b[i]);
      puts("");
    } else
      puts("Goofy");
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <vector>

int N, M;
int main(void) {
  while (std::cin >> N >> M) {
    std::vector<int> L(N);

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

    bool f;
    std::vector<int> b(N + M + 1);
    for (int S = 0; S < (1 << M); ++S) {
      int sum = 0;
      for (int i = 0; i < M; ++i) {
        sum += b[i] = ((S >> i) & 1);
      }
      for (int i = 0; i < N - 1; ++i) {
        b[M + i] = L[i + 1] - L[i] + b[i];
      }

      f = (sum == L[0]);
      for (int i = 0; i < N + M - 1; ++i) {
        if (b[i] != 0 && b[i] != 1) {
          f = false;
        }
      }
      if (f) {
        break;
      }
    }

    if (f) {
      for (int i = 0; i < N + M - 1; ++i) {
        std::cout << b[i];
      }
      std::cout << std::endl;
    } else {
      std::cout << "Goofy" << std::endl;
    }
  }
}
Problem p01209 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef unsigned long long ll;
int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
int main() {
  int n, nfac[11];
  ll m, fac[11];
  string b;
  while (cin >> n >> b, n) {
    m = 0;
    rep(i, 11) nfac[i] = fac[i] = 0;
    rep(i, b.size()) m *= n,
        m += ('0' <= b[i] && b[i] <= '9' ? b[i] - '0' : b[i] - 'A' + 10);
    ll ans = 1184595334580404209LL;
    rep(i, 11) {
      while (n % p[i] == 0)
        n /= p[i], nfac[i]++;
      if (nfac[i]) {
        for (ll j = p[i]; j <= m; j *= p[i])
          fac[i] += m / j;
        if (ans > fac[i] / nfac[i])
          ans = fac[i] / nfac[i];
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
int main() {
  int n, nfac[11];
  ll m, fac[11];
  string b;
  while (cin >> n >> b, n) {
    m = 0;
    rep(i, 11) nfac[i] = fac[i] = 0;
    rep(i, b.size()) m *= n,
        m += ('0' <= b[i] && b[i] <= '9' ? b[i] - '0' : b[i] - 'A' + 10);
    ll ans = 4611686018427387904LL;
    rep(i, 11) {
      while (n % p[i] == 0)
        n /= p[i], nfac[i]++;
      if (nfac[i]) {
        for (ll j = p[i]; j <= m; j *= p[i])
          fac[i] += m / j;
        if (ans > fac[i] / nfac[i])
          ans = fac[i] / nfac[i];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01209 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int PRIMES_COUNT = 11;
int PRIMES[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};

int main() {
  while (true) {
    int base;
    string digits;
    cin >> base >> digits;
    if (base == 0) {
      break;
    }
    ll w = 1ll, input = 0;
    for (int i = digits.size() - 1; i >= 0; --i, w *= base) {
      if ('0' <= digits[i] && digits[i] <= '9') {
        input += (digits[i] - '0') * w;
      } else {
        input += (digits[i] - 'A' + 10) * w;
      }
    }
    vector<ll> counts(PRIMES_COUNT, 0);
    for (int i = 0; i < PRIMES_COUNT; ++i) {
      ll tmp = input;
      while (tmp >= PRIMES[i]) {
        tmp /= PRIMES[i];
        counts[i] += tmp;
      }
    }
    ll result = 0x7fffffffffffffffll;
    for (int i = 0; i < PRIMES_COUNT; ++i) {
      int tmp = base, div = 0;
      while (tmp % PRIMES[i] == 0) {
        tmp /= PRIMES[i];
        ++div;
      }
      if (div != 0) {
        result = min(result, counts[i] / div);
      }
    }
    cout << result << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int PRIMES_COUNT = 11;
int PRIMES[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};

int main() {
  while (true) {
    int base;
    string digits;
    cin >> base >> digits;
    if (base == 0) {
      break;
    }
    ll w = 1ll, input = 0;
    for (int i = digits.size() - 1; i >= 0; --i, w *= base) {
      if ('0' <= digits[i] && digits[i] <= '9') {
        input += (digits[i] - '0') * w;
      } else {
        input += (digits[i] - 'A' + 10) * w;
      }
    }
    vector<ll> counts(PRIMES_COUNT, 0);
    for (int i = 0; i < PRIMES_COUNT; ++i) {
      ll tmp = input;
      while (tmp % PRIMES[i] == 0) {
        tmp /= PRIMES[i];
        counts[i] += tmp;
      }
    }
    cout << min(counts[0], counts[2]) << endl;
  }
  return 0;
}
Problem p01209 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

void factorize(vector<ll> &p, vector<ll> &cnt, ll x) {
  p.clear();
  cnt.clear();
  if (x == 1 || x == 0) {
    p.push_back(1);
    cnt.push_back(1);
    return;
  }

  int pp = 2;
  while (true) {
    if (x <= 1)
      break;
    if (x % pp == 0) {
      p.push_back(pp);
      cnt.push_back(0);
      while (x % pp == 0) {
        cnt[cnt.size() - 1]++;
        x /= pp;
      }
    }
    pp++;
  }
  return;
}

int main() {
  int base;
  string a;
  while (cin >> base >> a) {
    if (base == 0 && a == "0")
      break;
    unsigned long long m = 0;
    for (int i = 0; i < a.length(); ++i) {
      m *= base;
      if (a[i] <= '9')
        m += a[i] - '0';
      else
        m += a[i] - 'A' + 10;
    }

    // cout<<base<<" "<<m<<endl;
    vector<ll> p, cnt;
    factorize(p, cnt, base);
    // for(int i=0; i<p.size(); ++i) cout<<p[i]<<" : "<<cnt[i]<<endl;

    vector<ll> pp(p.size(), 0);
    for (int i = 0; i < p.size(); ++i) {
      ll n = p[i];
      while (n <= m) {
        pp[i] += m / n;
        n *= p[i];
      }
    }

    ll ans = 1ll << 62;
    for (int i = 0; i < p.size(); ++i) {
      // cout<<p[i]<<" "<<pp[i]/cnt[i]<<endl;
      ans = min(ans, pp[i] / cnt[i]);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

void factorize(vector<ll> &p, vector<ll> &cnt, ll x) {
  p.clear();
  cnt.clear();
  if (x == 1) {
    p.push_back(1);
    cnt.push_back(1);
    return;
  }

  int pp = 2;
  while (true) {
    if (x <= 1)
      break;
    if (x % pp == 0) {
      p.push_back(pp);
      cnt.push_back(0);
      while (x % pp == 0) {
        cnt[cnt.size() - 1]++;
        x /= pp;
      }
    }
    pp++;
  }
  return;
}

int main() {
  int base;
  string a;
  while (cin >> base >> a) {
    if (base == 0 && a == "0")
      break;
    ll m = 0;
    for (int i = 0; i < a.length(); ++i) {
      m *= base;
      if (a[i] <= '9')
        m += a[i] - '0';
      else
        m += a[i] - 'A' + 10;
    }

    // cout<<base<<" "<<m<<endl;
    vector<ll> p, cnt;
    factorize(p, cnt, base);
    // for(int i=0; i<p.size(); ++i) cout<<p[i]<<" : "<<cnt[i]<<endl;

    vector<ll> pp(p.size(), 0);
    for (int i = 0; i < p.size(); ++i) {
      ll n = p[i];
      while (n < m) {
        pp[i] += m / n;
        n *= p[i];
      }
    }

    ll ans = 1ll << 62;
    for (int i = 0; i < p.size(); ++i) {
      // cout<<p[i]<<" "<<pp[i]/cnt[i]<<endl;
      ans = min(ans, pp[i] / cnt[i]);
    }
    cout << ans << endl;
  }
}
Problem p01209 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
#include <map>
#include <string>
using namespace std;

unsigned long long to(string w, int base) {
  unsigned long long ans = 0;
  for (int i = 0; i < w.size(); i++)
    ans = ans * base + (w[i] >= 'A' ? w[i] - 'A' + 10 : w[i] - '0');
  return ans;
}

map<int, int> factor(int n) {
  map<int, int> w;
  for (int i = 2; i * i <= n; i++)
    while (n % i == 0)
      w[i]++, n /= i;
  if (n != 1)
    w[n]++;
  return w;
}

int main() {
  int n;
  string m;
  while (cin >> n >> m, n) {
    unsigned long long v = to(m, n), ans = -1;
    map<int, int> r = factor(n);
    for (map<int, int>::iterator it = r.begin(); it != r.end(); ++it) {
      int a = it->first;
      int d = it->second;
      unsigned long long t = v;
      unsigned long long ok = 0;
      while (t) {
        ok += (t / a);
        t /= a;
      }
      ans = min(ok / d, ans);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
vector<int> prime;
bool isPrime[100001];

void PrimeCalc() {
  memset(isPrime, 1, sizeof(isPrime));
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i <= 100000; ++i) {
    if (isPrime[i]) {
      prime.push_back(i);
      for (int j = 2 * i; j <= 100000; j += i) {
        isPrime[j] = false;
      }
    }
  }
}

void solve() {
  PrimeCalc();
  int N;
  while (cin >> N, N) {
    string M;
    cin >> M;
    map<int, unsigned long long> decimal_factor;
    int t = N;
    for (int i = 0; i < prime.size() && prime[i] <= 36; ++i) {
      if (t == 1) {
        break;
      }
      while (t % prime[i] == 0) {
        ++decimal_factor[prime[i]];
        t /= prime[i];
      }
    }
    unsigned long long m = strtoll(&M[0], NULL, N);
    map<int, unsigned long long> factor;
    for (unsigned long long i = 2; i <= m; ++i) {
      t = i;
      for (int j = 0; prime[j] * prime[j] <= m; ++j) {
        if (t == 1) {
          break;
        }
        while (t % prime[j] == 0) {
          ++factor[prime[j]];
          t /= prime[j];
        }
      }
    }
    unsigned long long min_exist = 99999999999;
    map<int, unsigned long long>::iterator it = factor.begin();
    while (it != factor.end()) {
      if (decimal_factor[it->first] > 0 &&
          it->second / decimal_factor[it->first] < min_exist) {
        min_exist = it->second / decimal_factor[it->first];
      }
      ++it;
    }
    cout << min_exist << endl;
  }
}

int main() {
  solve();
  return (0);
}
Problem p01209 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void integerFactorization(int n, vector<int> &base, vector<int> &expo) {
  base.clear();
  expo.clear();
  int a = 2;
  while (a * a <= n) {
    int b = 0;
    while (n % a == 0) {
      ++b;
      n /= a;
    }
    if (b > 0) {
      base.push_back(a);
      expo.push_back(b);
    }
    ++a;
  }
  if (n > 1 || base.size() == 0) {
    base.push_back(n);
    expo.push_back(1);
  }
}

long long toInt(string s, int n) {
  long long ret = 0;
  for (unsigned i = 0; i < s.size(); ++i) {
    ret *= n;
    if (isdigit(s[i]))
      ret += s[i] - '0';
    else
      ret += s[i] - 'A' + 10;
  }
  return ret;
}

int main() {
  for (;;) {
    int n;
    string s;
    cin >> n >> s;
    if (n == 0)
      return 0;

    vector<int> base, expo;
    integerFactorization(n, base, expo);

    long long m = toInt(s, n);

    long long ret = LLONG_MAX;
    for (unsigned i = 0; i < base.size(); ++i) {
      long long m2 = m;
      long long tmp = 0;
      while (m2 > 0) {
        tmp += m2 / base[i];
        m2 /= base[i];
      }
      ret = min(ret, tmp / expo[i]);
    }

    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int toInt(string s, int n) {
  int ret = 0;
  for (unsigned i = 0; i < s.size(); ++i) {
    ret *= n;
    if (isdigit(s[i]))
      ret += s[i] - '0';
    else
      ret += s[i] - 'A' + 10;
  }
  return ret;
}

int main() {
  for (;;) {
    int n;
    string s;
    cin >> n >> s;
    if (n == 0)
      return 0;

    int m = toInt(s, n);
    int ret = 0;
    for (int i = 1; i <= m; ++i) {
      int x = i;
      while (x % 5 == 0) {
        ++ret;
        x /= 5;
      }
    }

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

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <string>

using namespace std;

typedef unsigned long long ll;
const ll INF = 18000000000000000000ULL;

map<int, int> prime_decompos(int a) {
  map<int, int> res;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      res[i]++;
      a /= i;
      i--;
    }
  }
  if (a != 1)
    res[a]++;
  return res;
}

int main() {
  int n;
  ll m;
  string str;
  while (cin >> n >> str && !(n == 0 && str == "0")) {
    ll mul = 1;
    m = 0;
    for (int i = str.size() - 1; i >= 0; i--) {
      ll a = str[i] - '0';
      if (str[i] >= 'A' && str[i] <= 'Z')
        a = 10 + str[i] - 'A';
      m += a * mul;
      mul *= n;
    }
    if (m == 0) {
      cout << 0 << endl;
      continue;
    }
    map<int, int> np = prime_decompos(n);
    ll res = INF;
    for (map<int, int>::iterator it = np.begin(); it != np.end(); it++) {
      ll a = it->first;
      int b = it->second;
      ll cnt = 0;
      int loop = (log((long double)m) / log((long double)a) + (1e-10));
      while (loop--) {
        cnt += (m / a);
        a *= it->first;
      }
      res = min(res, cnt / b);
    }
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <string>

using namespace std;

typedef unsigned long long ll;
const ll INF = 18000000000000000000ULL;

// ifstream ifs("input.txt");
// ofstream ofs("output.txt");
// #define cin ifs
// #define cout ofs

map<int, int> prime_decompos(int a) {
  map<int, int> res;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      res[i]++;
      a /= i;
      i--;
    }
  }
  if (a != 1)
    res[a]++;
  return res;
}

int main() {
  int n;
  ll m;
  string str;
  while (cin >> n >> str && !(n == 0 && str == "0")) {
    ll mul = 1;
    m = 0;
    for (int i = str.size() - 1; i >= 0; i--) {
      ll a = str[i] - '0';
      if (str[i] >= 'A' && str[i] <= 'Z')
        a = 10 + str[i] - 'A';
      m += a * mul;
      mul *= n;
    }
    map<int, int> np = prime_decompos(n);
    ll res = INF;
    for (map<int, int>::iterator it = np.begin(); it != np.end(); it++) {
      ll a = it->first;
      int b = it->second;
      ll cnt = 0;
      if (a == 2) {
        while (a <= m) {
          cnt += (m / a);
          a *= it->first;
        }
      } else {
        int loop = (log((long double)m) / log((long double)a) + 0.0001);
        while (loop--) {
          cnt += (m / a);
          a *= it->first;
        }
      }
      res = min(res, cnt / b);
    }
    cout << res << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};
int w, h, ans, swn[256], nsw;
char f[31][31];
bool V[30][30][256];
int main() {
  while (cin >> w >> h, w) {
    rep(i, 256) swn[i] = -1;
    nsw = 0;
    int sy, sx;
    rep(i, h) cin >> f[i];
    rep(i, h) rep(j, w) {
      if (f[i][j] == '@')
        sy = i, sx = j, f[i][j] = '.';
      if ('a' <= f[i][j] && f[i][j] <= 'z' && swn[f[i][j]] < 0)
        swn[f[i][j]] = nsw++;
      rep(k, 256) V[i][j][k] = 0;
    }
    ans = -1;
    V[sy][sx][0] = 1;
    queue<pair<pair<int, int>, pair<int, int>>> Q;
    Q.push(make_pair(make_pair(sy, sx), make_pair(0, 0)));
    while (!Q.empty()) {
      int y = Q.front().first.first, x = Q.front().first.second,
          s = Q.front().second.first, c = Q.front().second.second;
      Q.pop();
      if (f[y][x] == '>' || f[y][x] == '<') {
        ans = c;
        break;
      }
      rep(d, 4) {
        int ny = y + dy[d], nx = x + dx[d], ns = s;
        if (f[ny][nx] == '#' || 'A' <= f[ny][nx] && f[ny][nx] <= 'Z' &&
                                    !(s & 1 << swn[f[ny][nx] - 'A' + 'a']))
          continue;
        if ('a' <= f[ny][nx] && f[ny][nx] <= 'z')
          ns ^= 1 << swn[f[ny][nx]];
        if (!V[ny][nx][ns])
          V[ny][nx][ns] = 1,
          Q.push(make_pair(make_pair(ny, nx), make_pair(ns, c + 1)));
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1};
int w, h, C[30][30][256], ans, swn[256], nsw;
char f[31][31];
void dfs(int y, int x, int s) {
  if (f[y][x] == '>' || f[y][x] == '<') {
    ans = min(ans, C[y][x][s]);
    return;
  }
  rep(d, 4) {
    int ny = y + dy[d], nx = x + dx[d], ns = s;
    if (f[ny][nx] == '#' || 'A' <= f[ny][nx] && f[ny][nx] <= 'Z' &&
                                !(s & 1 << swn[f[ny][nx] - 'A' + 'a']))
      continue;
    if ('a' <= f[ny][nx] && f[ny][nx] <= 'z')
      ns ^= 1 << swn[f[ny][nx]];
    if (C[ny][nx][ns] > C[y][x][s] + 1)
      C[ny][nx][ns] = C[y][x][s] + 1, dfs(ny, nx, ns);
  }
}
int main() {
  while (cin >> w >> h, w) {
    rep(i, 256) swn[i] = -1;
    nsw = 0;
    int sy, sx;
    rep(i, h) cin >> f[i];
    rep(i, h) rep(j, w) {
      if (f[i][j] == '@')
        sy = i, sx = j, f[i][j] = '.';
      if ('a' <= f[i][j] && f[i][j] <= 'z' && swn[f[i][j]] < 0)
        swn[f[i][j]] = nsw++;
      rep(k, 256) C[i][j][k] = 1 << 20;
    }
    ans = 1 << 20;
    C[sy][sx][0] = 0;
    dfs(sy, sx, 0);
    cout << (ans != 1 << 20 ? ans : -1) << endl;
  }
  return 0;
}