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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

const ld day = 86400;

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n, l;
  while (cin >> n >> l, n) {
    vector<pair<pii, int>> v;
    vi use(day);
    REP(i, n) {
      int s, t, u;
      cin >> s >> t >> u;
      FOR(j, s, t) use[j] = u;
    }
    ld lb = 0, ub = 1000000;
    REP(bs, 150) {
      ld mid = (lb + ub) / 2.0;
      ld vol = l;
      ld tmp;
      REP(i, day) {
        vol -= use[i];
        vol += mid;
        if (vol > l)
          vol = l;
        if (vol < 0) {
          lb = mid;
          goto end;
        }
      }
      tmp = vol;
      REP(i, day) {
        vol -= use[i];
        vol += mid;
        if (vol > l)
          vol = l;
        if (vol < 0) {
          lb = mid;
          goto end;
        }
      }
      if (tmp > vol)
        lb = mid;
      else
        ub = mid;
    end:;
    }
    cout << lb << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

const ld day = 86400;

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n, l;
  while (cin >> n >> l, n) {
    vector<pair<pii, int>> v;
    vi use(day);
    REP(i, n) {
      int s, t, u;
      cin >> s >> t >> u;
      FOR(j, s, t) use[j] = u;
    }
    ld lb = 0, ub = 1000000;
    REP(bs, 200) {
      ld mid = (lb + ub) / 2.0;
      ld vol = l;
      ld tmp;
      REP(i, day) {
        vol -= use[i];
        vol += mid;
        if (vol > l)
          vol = l;
        if (vol < 0) {
          lb = mid;
          goto end;
        }
      }
      tmp = vol;
      REP(i, day) {
        vol -= use[i];
        vol += mid;
        if (vol > l)
          vol = l;
        if (vol < 0) {
          lb = mid;
          goto end;
        }
      }
      if (tmp > vol)
        lb = mid;
      else
        ub = mid;
    end:;
    }
    cout << lb << endl;
  }
  return 0;
}
Problem p01298 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int MOD = 1e9 + 7;

vector<double> cap(200000, 0);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  while (cin >> n, n) {
    double l;
    cin >> l;
    double low = 0, high = 2000000;
    vector<double> v(200000, 0);
    for (int i = 0; i < n; i++) {
      int st, gt;
      cin >> st >> gt;
      double u;
      cin >> u;
      for (int j = st + 1; j <= gt; j++) {
        v[j] = v[j + 86400] = u;
      }
    }
    for (int k = 0; k < 100; k++) {
      cap[0] = l;
      double mid = (low + high) / 2;
      bool f = true;
      for (int i = 1; i <= 86400 * 2; i++) {
        cap[i] = min(l, cap[i - 1] + (mid - v[i]));
        if (cap[i] < 0)
          f = false;
      }
      if (cap[86400] <= cap[86400 * 2] && f) {
        high = mid;
      } else {
        low = mid;
      }
    }
    cout << fixed << setprecision(10) << low << endl;
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int MOD = 1e9 + 7;

vector<double> cap(200000, 0);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  while (cin >> n, n) {
    double l;
    cin >> l;
    double low = 0, high = 2000000;
    vector<double> v(200000, 0);
    for (int i = 0; i < n; i++) {
      int st, gt;
      cin >> st >> gt;
      double u;
      cin >> u;
      for (int j = st + 1; j <= gt; j++) {
        v[j] = v[j + 86400] = u;
      }
      for (int k = 0; k < 50; k++) {
        cap[0] = l;
        double mid = (low + high) / 2;
        bool f = true;
        for (int i = 1; i <= 86400 * 2; i++) {
          cap[i] = min(l, cap[i - 1] + (mid - v[i]));
          if (cap[i] < 0)
            f = false;
        }
        if (cap[86400] <= cap[86400 * 2] && f) {
          high = mid;
        } else {
          low = mid;
        }
      }
    }
    cout << fixed << setprecision(10) << low << endl;
  }
}
Problem p01298 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

#define DAY 86400
#define EPS 1e-8

int u[DAY], N, ft, L;

double calc() {
  double f, hi = 1e6, lo = 0, pv = 0, d1;
  for (;;) {
    f = (hi + lo) / 2;
    bool g = true;
    double v = L;
    for (int i = 0; i < DAY * 2; i++) {
      v += (f - 1.0 * u[i % DAY]);

      if (v >= L)
        v = L;
      if (v < 0) {
        g = false;
        lo = f;
        break;
      }
      if (i == DAY - 1)
        d1 = v;
    }

    if (g && fabs(d1 - v) < EPS) {
      if (fabs(f - pv) < EPS)
        break;
      pv = f;
      hi = f;
    } else
      lo = f;
  }

  return f;
}

int main() {
  while (scanf("%d%d", &N, &L), (N || L)) {
    memset(u, 0, sizeof(u));
    for (int i = 0; i < N; i++) {
      int s, t, f;
      scanf("%d%d%d", &s, &t, &f);
      for (int j = s; j < t; j++)
        u[j] = f;
    }
    printf("%.8lf\\n", calc());
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;

double L, s[100], t[100], u[100];
int N;

bool simulate(double &v, double f, int i) {
  int pt = (i == 0 ? 0 : t[i - 1]);
  v += 1.0 * (s[i] - pt) * f;
  if (v > L)
    v = L;

  v += (-u[i] + f) * (t[i] - s[i]);

  return v >= 0;
}

double bsearch(int M, double f) {
  double hi = 1e6, lo = f, mi, v;
  while (1) {
    mi = (hi + lo) / 2;
    v = 0;
    bool g = true;
    for (int i = 0; i <= M; i++) {
      if (!simulate(v, mi, i)) {
        g = false;
        break;
      }
    }

    if (g)
      hi = mi;
    else
      lo = mi;

    if (g && (hi - lo) < 1e-8)
      break;
  }

  return (hi + lo) / 2;
}

double calc() {
  double f = 0.0, v;
  while (1) {
    bool g = true;
    v = L;
    for (int i = 0; i < N; i++) {
      if (!simulate(v, f, i)) {
        g = false;
        f = bsearch(i, f);
        break;
      }
    }

    if (g)
      break;
  }

  return f;
}

int main() {
  cout << setiosflags(ios::fixed) << setprecision(8);
  // ifstream cin("F.txt");

  while (cin >> N >> L, (N || L)) {
    for (int i = 0; i < N; i++)
      cin >> s[i] >> t[i] >> u[i];

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

#include <bits/stdc++.h>
#define range(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
const int T = 86400;
const double eps = 1e-9;

int main(void) {
  double L;
  for (int n; cin >> n >> L, n;) {
    vector<tuple<int, int, double>> in(n);
    for (auto &e : in) {
      int s, t;
      double u;
      cin >> s >> t >> u;
      e = tie(s, t, u);
    }

    double lb = 0.0, ub = 1e6;
    rep(loop, 100) {
      double mid = (lb + ub) / 2.0;

      vector<double> ls(2);
      double rest = L;
      bool ok = true;
      [&] {
        rep(_, 2) {
          int curt = 0;
          for (auto &e : in) {
            int s, t;
            double u;
            tie(s, t, u) = e;

            rest = min(rest + (s - curt) * mid, L);
            curt = s;

            rest = min(rest + (t - curt) * (mid - u), L);
            curt = t;

            if (rest < 0) {
              ok = false;
              return;
            }
          }
          rest = min(rest + (T - curt) * mid, L);

          ls[_] = rest;
        }
      }();

      if (ok and abs(ls[0] - ls[1]) < eps) {
        ub = mid;
      } else {
        lb = mid;
      }
    }

    printf("%.12f\\n", ub);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
const int N = 86400;
const double eps = 1e-10;

int main(void) {
  for (int n, L; cin >> n >> L, n;) {
    vector<int> tbl(N);
    rep(loop, n) {
      int s, t, u;
      cin >> s >> t >> u;
      range(i, s, t) { tbl[i] = u; }
    }

    double lb = 0.0, ub = 1e6 + 1;
    rep(loop, 1000) {
      double mid = (lb + ub) / 2.0;

      vector<int> ls(2);
      double rest = L;
      bool ok = true;
      [&] {
        rep(i, 2) {
          rep(i, N) {
            rest = min(rest - tbl[i] + mid, 1.0 * L);

            if (rest < eps) {
              ok = false;
              return;
            }
          }

          ls[i] = rest;
        }
      }();

      if (ok and abs(ls[0] - ls[1]) < eps) {
        ub = mid;
      } else {
        lb = mid;
      }
    }

    cout.precision(12);
    cout << ub << endl;
  }

  return 0;
}
Problem p01300 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdlib.h>

int main() {
  while (1) {
    // 入力
    std::string number;
    std::cin >> number;
    // 演算
    if (number == "0")
      return 0;
    int N = number.length();
    int dp[80001][11];
    int answer = 0;
    for (int i = 0; i < 11; i++) {
      dp[0][i] = 0;
    }
    for (int i = 0; i < N; i++) {
      int digit = number.at(i) - '0';
      for (int j = 0; j < 11; j++) {
        dp[i + 1][(j * 10 + digit) % 11] = dp[i][j];
      }
      if (digit != 0) {
        dp[i + 1][digit]++;
      }
      /* for (int j = 0; j < 11; j++) {
              std::cout << "dp[" << i + 1 << "][" << j << "]: " << dp[i + 1][j]
      << std::endl;
      } */
      answer += dp[i + 1][0];
    }
    // 出力
    /* for (int i = 0; i < 11; i++) {
            std::cout << "dp[" << N << "][" << i << "]: " << dp[N][i] <<
    std::endl;
    } */
    std::cout << answer << std::endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdlib.h>

int main() {
  while (1) {
    // 入力
    std::string number;
    std::cin >> number;
    // 演算
    if (number == "0")
      return 0;
    int N = number.length();
    int dp[801][11];
    int answer = 0;
    for (int i = 0; i < 11; i++) {
      dp[0][i] = 0;
    }
    for (int i = 0; i < N; i++) {
      int digit = number.at(i) - '0';
      for (int j = 0; j < 11; j++) {
        dp[i + 1][(j * 10 + digit) % 11] = dp[i][j];
      }
      if (digit != 0) {
        dp[i + 1][digit]++;
      }
      /* for (int j = 0; j < 11; j++) {
              std::cout << "dp[" << i + 1 << "][" << j << "]: " << dp[i + 1][j]
      << std::endl;
      } */
      answer += dp[i + 1][0];
    }
    // 出力
    /* for (int i = 0; i < 11; i++) {
            std::cout << "dp[" << N << "][" << i << "]: " << dp[N][i] <<
    std::endl;
    } */
    std::cout << answer << std::endl;
  }
}
Problem p01300 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;
const int MOD = 1000000007;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

string S;

void solve() {
  int N = S.length();
  vector<vector<int>> s(N, vector<int>(11));

  rep(i, N) {
    int d = S[i] - '0';
    s[i][d] = d > 0;
    if (i > 0) {
      rep(j, 11) { s[i][(j * 10 + d) % 11] += s[i - 1][j]; }
    }
  }
  int ans = 0;
  rep(i, N) { ans += s[i][0]; }
  cout << ans << endl;
}

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout.precision(16);
  // cout.setf(ios::fixed);

  while (cin >> S, S != "0")
    solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

typedef long long int ll;
typedef double D;     // ??§?¨?????????????double???long double?????????
typedef complex<D> P; // Point
typedef pair<P, P> L; // Line
typedef vector<P> VP;

const int INF = (1 << 30) - 1;
const D EPS = 1e-9;
const int MOD = 1000000007;

#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

string S;

void solve() {
  int N = S.length();
  int ans = 0;
  for (int i = 0; i < N; i++)
    if (S[i] != '0') {
      for (int j = i + 1; j < N; j++) {
        int s = 0;
        for (int k = i; k <= j; k++) {
          s += (S[k] - '0') * (k % 2 ? 1 : -1);
        }
        if (s % 11 == 0)
          ans++;
      }
    }
  cout << ans << endl;
}

// g++ -std=c++0x -msse4.2 -O3
// #include <bits/stdc++.h>
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout.precision(16);
  // cout.setf(ios::fixed);

  while (cin >> S, S != "0")
    solve();

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

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int main(void) {
  for (;;) {
    string dig;
    cin >> dig;
    if (dig == "0")
      return 0;
    int answer = 0;
    vector<vector<int>> dp(dig.size(), vector<int>(11, 0));
    REP(i, dig.size()) {
      if (i)
        REP(j, 11) dp[i][(j * 10 + dig[i] - '0') % 11] = dp[i - 1][j];
      if (dig[i] != '0')
        ++dp[i][dig[i] - '0'];
      answer += dp[i][0];
    }
    cout << answer << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int main(void) {
  for (;;) {
    string dig;
    cin >> dig;
    if (dig == "0")
      return 0;
    int answer = 0;
    REP(i, dig.size()) {
      if (dig[i] == '0')
        continue;
      int tmp = 0;
      FOR(j, i, dig.size()) {
        tmp = -(tmp + dig[j] - '0') % 11;
        if (tmp == 0)
          ++answer;
      }
    }
    cout << answer << endl;
  }
}
Problem p01300 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int convert(char c);

int main() {
  string str;
  int length = 0;
  long long count = 0;
  while (1) {
    cin >> str;
    if (str == "0")
      break;

    length = str.size();
    vector<int> digit(length);
    for (int i = 0; i < length; i++) {
      digit[i] = convert(str[i]);
    }

    vector<vector<int>> cnt;
    // res[end][res]はend+1桁目で終わる数で、11を法としてresに等しい物の個数
    cnt.resize(length);
    for (int i = 0; i < length; i++) {
      cnt[i].resize(11);
    }
    if (digit[0] != 0)
      cnt[0][digit[0]]++;
    for (int end = 1; end < length; end++) {
      for (int res = 0; res < 11; res++) {
        int temp = (res * 10 + digit[end]) % 11;
        cnt[end][temp] = cnt[end - 1][res];
      }
      if (digit[end] != 0)
        cnt[end][digit[end]]++;
    }

    count = 0;
    for (int end = 1; end < length; end++) {
      count += cnt[end][0];
    }
    cout << count << endl;
  }
  return 0;
}

int convert(char c) {
  int a = c - 48;
  return a;
}

Error: 0

Fixed submission:

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

using namespace std;

int convert(char c);

int main() {
  string str;
  int length = 0;
  long long count = 0;
  while (1) {
    cin >> str;
    if (str == "0")
      break;

    length = str.size();
    vector<int> digit(length);
    for (int i = 0; i < length; i++) {
      digit[i] = convert(str[i]);
    }

    vector<vector<int>> res;
    // res[end][start]は(上から)start+1桁目からend+1桁目までを整数とみなした時の、11を法とした剰余
    res.resize(length);
    for (int i = 0; i < length; i++) {
      res[i].resize(i + 1);
      res[i][i] = digit[i];
    }

    count = 0;
    for (int end = 1; end < length; end++) {
      for (int start = 0; start < end; start++) {
        if (digit[start] == 0)
          continue;
        res[end][start] = (res[end - 1][start] * 10 + digit[end]) % 11;
        if (res[end][start] == 0)
          count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}

int convert(char c) {
  int a = c - 48;
  return a;
}
Problem p01300 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  string str;
  while (cin >> str, str != "0") {
    int n = str.size();
    int D[n];
    for (int i = 0; i < n; i++) {
      D[i] = ((i & 1) == 1 ? -1 : 1) * (str[i] - '0');
    }
    int C[n];
    fill(C, C + n, 0);
    for (int i = 0; i < n; i++) {
      int sum = 0;
      if (D[i] == 0 && (i > 0 ? C[i - 1] : 0) == 0)
        continue;
      for (int j = i; j < n; j++) {
        sum += D[j];
        sum %= 11;
        if (!sum) {
          if (D[i] == 0) {
            C[j] += (i > 0 ? C[i - 1] : 0);
          } else {
            C[j] += 1 + (i > 0 ? C[i - 1] : 0);
          }
          break;
        }
      }
    }
    int count = 0;
    for (auto &c : C)
      count += c;
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  string str;
  while (cin >> str, str != "0") {
    int n = str.size();
    int D[n];
    for (int i = 0; i < n; i++) {
      D[i] = ((i & 1) == 1 ? -1 : 1) * (str[i] - '0');
    }
    int c = 0;
    for (int i = 0; i < n; i++) {
      int sum = D[i];
      if (!sum)
        continue;
      for (int j = i + 1; j < n; j++) {
        sum += D[j];
        sum %= 11;
        if (!sum)
          c++;
      }
    }
    cout << c << endl;
  }
  return 0;
}
Problem p01300 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
#define MAX 80001
using namespace std;

class state {
public:
  int n, ans;
  bool f;
};

int main() {
  string str;
  while (cin >> str && str != "0") {
    int an, ans;
    vector<state> v;
    state s;
    an = ans = 0;
    for (int i = 0; i < str.size(); i++) {
      int n = str[i] - '0';
      if (n == 0) {
        for (int j = 0; j < v.size(); j++)
          v[j].f = (v[j].f + 1) % 2;
        ans += an;
      } else {
        s.n = n;
        s.ans = 1 + an;
        s.f = false;
        an = 0;
        v.push_back(s);
        vector<state>::iterator it = v.end();
        it -= 2;

        for (int j = v.size() - 2; j >= 0; j--, it--) {
          if (v[j].f)
            v[j].n += n;
          else
            v[j].n -= n;
          v[j].f = (v[j].f + 1) % 2;

          if (v[j].n < 0)
            v[j].n += 11;
          else if (v[j].n >= 11)
            v[j].n -= 11;

          if (v[j].n == 0) {
            an += v[j].ans;
            it = v.erase(it);
          }
        }
        ans += an;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#define MAX 80001
using namespace std;

int main() {
  string str;
  while (cin >> str && str != "0") {
    int a[MAX], an, ans;
    an = ans = 0;
    for (int i = 0; i < str.size(); i++) {
      int n = str[i] - '0';
      if (n == 0) {
        a[i] = -1;
        ans += an;
      } else {
        an = 0;
        a[i] = n;
        for (int j = i - 1; j >= 0; j--) {
          if (a[j] == -1)
            continue;
          if ((i - j) % 2 == 0)
            a[j] += n;
          else
            a[j] -= n;
          if (a[j] < 0)
            a[j] += 11;
          else if (a[j] >= 11)
            a[j] -= 11;
          if (a[j] == 0)
            an++;
        }
        ans += an;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01300 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string s;
  while (cin >> s && s != "0") {
    int dp[2][11];
    memset(dp, 0, sizeof(dp));
    dp[0][s[0] - '0'] = 1;
    int ans = 0;
    for (int i = 1; i < s.size(); i++) {
      for (int j = 0; j < 11; j++)
        dp[1][(j * 10 + s[i] - '0') % 11] = dp[0][j];
      for (int j = 0; j < 11; j++)
        dp[0][j] = dp[1][j];
      if (s[i] != '0')
        dp[0][s[i] - '0']++;
      ans += dp[0][0];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

string str;

void solve() {
  int sum, cnt;
  sum = cnt = 0;
  for (int i = 0; i < str.size(); i++) {
    sum = 0;
    if (str[i] == '0')
      continue;
    for (int j = i; j < str.size(); j++) {
      sum += ((j - i) % 2 == 0 ? 1 : -1) * (str[j] - '0');
      if ((sum + 11) % 11 == 0)
        cnt++;
    }
  }
  cout << cnt << endl;
}

int main() {
  while (1) {
    cin >> str;
    if (str == "0")
      break;
    solve();
  }
}