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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>

using namespace std;

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

    int customer[10001] = {0};
    for (int i = 0; i < n; i++) {
      cin >> customer[i];
    }

    sort(customer, customer + n);

    long long int wait = 0;
    long long int ret = 0;

    for (int i = 0; i < n; i++) {
      ret += wait;
      wait += customer[i];
    }

    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>

using namespace std;

string code[200];

struct milk {
  int customer[10001];
  int time;
  int n;
  int ret;
};

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    milk temp;
    temp.n = n;
    temp.ret = 0;
    temp.time = 0;
    for (int i = 0; i < n; i++) {
      cin >> temp.customer[i];
    }

    int ret = 60 * 10000;

    queue<milk> qu;
    qu.push(temp);

    while (!qu.empty()) {
      temp = qu.front();
      if (temp.n == 1) {
        ret = min(ret, temp.ret);
      } else {
        for (int i = 0; i < n; i++) {
          temp = qu.front();
          if (temp.customer[i] != -1) {
            temp.time += temp.customer[i];
            temp.customer[i] = -1;
            temp.n--;
            temp.ret += temp.time;
            qu.push(temp);
          }
        }
      }
      qu.pop();
    }

    cout << ret << endl;
  }
}
Problem p00112 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;
int n, m, temp;
int t[10005];
long long int sum;
int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    m = n;
    while (m--) {
      cin >> t[m];
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < n - 1 - i; j++) {
        if (t[j] > t[j + 1]) {
          temp = t[j];
          t[j] = t[j + 1];
          t[j + 1] = temp;
        }
      }
    }
    sum = 0;
    for (int i = 0; i < n - 1; i++) {
      sum += (i + 1) * t[n - 2 - i];
    }
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;
int n, m, temp;
int t[100];
int sum;
int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    m = n;
    while (m--) {
      cin >> t[m];
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = 0; j < n - 1 - i; j++) {
        if (t[j] > t[j + 1]) {
          temp = t[j];
          t[j] = t[j + 1];
          t[j + 1] = temp;
        }
      }
    }
    sum = 0;
    for (int i = 0; i < n - 1; i++) {
      sum += (i + 1) * t[n - 2 - i];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00112 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int main() {
  int n;
  while (cin >> n && n) {
    vector<ll> v(n);
    rep(i, n) cin >> v[i];
    sort(ALL(v));

    ll res = 0, wait = 0;

    REP(i, 1, n)
    res += wait += v[i - 1];

    cout << res << endl;
  }
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int main() {
  int n;
  while (cin >> n && n) {
    vector<ll> v(n), r(n, 0);
    rep(i, n) cin >> v[i];
    sort(ALL(v));

    if (1 < n)
      partial_sum(ALL(v), r.begin() + 1);

    cout << accumulate(ALL(r), 0LL) << endl;
  }
}
Problem p00112 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int i;
  long long a, b, c[10000], sum = 0, dd = 0;
  while (1) {
    cin >> a;
    if (a == 0)
      break;
    for (i = 0; i < a; i++) {
      cin >> c[i];
    }
    sort(c, c + a);
    for (i = 0; i < a; i++) {
      sum += dd;
      dd += c[i];
    }
    cout << sum << endl;
    sum = 0;
    dd = 0;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int i, n, a[n], sum = 0, dd = 0;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    for (i = 0; i < n; i++) {
      sum += dd;
      dd += a[i];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00112 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    vector<int> ts(n);
    for (int i = 0; i < n; i++) {
      cin >> ts[i];
    }
    sort(ts.begin(), ts.end());
    long long sum = 0, ans = 0;
    for (int i = 0; i < n; i++) {
      ans += sum;
      sum += ts[i];
    }
    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    vector<int> ts;
    for (int i = 0; i < n; i++) {
      cin >> ts[i];
    }
    sort(ts.begin(), ts.end());
    long long sum = 0, ans = 0;
    for (int i = 0; i < n; i++) {
      ans += sum;
      sum += ts[i];
    }
    cout << ans << endl;
  }
}
Problem p00112 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#define MAX 50000
int judge(const void *a, const void *b) {
  return *(long long int *)a - *(long long int *)b;
}

int main() {
  long long int a[MAX];
  long long int i, n, ans, wait;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < MAX; i++) {
      a[i] = 0;
    }
    for (i = 0; i < n; i++) {
      scanf("%lld", &a[i]);
    }
    qsort(a, n, sizeof(long long int), judge);
    ans = 0;
    wait = 0;
    for (i = 0; i < n - 1; i++) {
      wait += a[i];
      ans += wait;
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int x[10000] = {0}, fake;
    long long int sum = 0;

    for (int t = 0;; t++) {
      scanf("%d", &x[t]);

      if (x[t] == 0) {
        break;
      }
      if (t != 0 || t < n) {
        for (int r = 0; r <= t; r++) {
          if (x[t] < x[r]) {
            fake = x[t];
            x[t] = x[r];
            x[r] = fake;
          }
        }
      }
    }

    for (int t = 0; t < n; t++) {
      sum += x[t] * (n - t - 1);
    }
    printf("%d\\n", sum);
  }
  return 0;
}
Problem p00112 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

long long int ans = 0;
long long int res[10001];

int main(void) {
  priority_queue<int, vector<int>, greater<int>> que;
  int n;
  while (1) {
    ans = 0;
    memset(res, 0, sizeof(res));
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      int a;
      scanf("%d", &a);
      que.push(a);
    }
    for (int i = 0; i < n; i++) {
      int r = que.top();
      que.pop();
      res[i + 1] = r + res[i];
      ans += res[i];
      //	printf("%d\\n",res[i]);
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main(void) {
  priority_queue<int, vector<int>, greater<int>> que;
  int n;
  while (1) {
    int ans = 0;
    int res[10001];
    memset(res, 0, sizeof(res));
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      int a;
      scanf("%d", &a);
      que.push(a);
    }
    for (int i = 0; i < n; i++) {
      int r = que.top();
      que.pop();
      res[i + 1] = r += res[i];
      ans += res[i];
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00112 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))

int main() {
  int i, cnt;
  std::vector<long long> vec;

  while (std::cin >> cnt, cnt) {
    vec.push_back(0);

    while (cnt--)
      std::cin >> i, vec.push_back(i);

    std::sort(vec.begin(), vec.end());

    rep(i, 2) rep(l, (int)vec.size() - 2) vec[l + 1] += vec[l];

    std::cout << vec[(int)vec.size() - 2] << std::endl;

    vec.clear();
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))

int main() {
  int i, cnt;
  std::vector<int> vec;

  while (std::cin >> cnt, cnt) {
    while (cnt--)
      std::cin >> i, vec.push_back(i);

    std::sort(vec.begin(), vec.end());

    rep(i, 2) rep(l, vec.size() - 2) vec[l + 1] += vec[l];

    std::cout << vec[std::min((int)vec.size() - 2, 0)] << std::endl;

    vec.clear();
  }
  return 0;
}
Problem p00112 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

long long n, a[10002];

void solve() {
  sort(a, a + n);
  long long cur = 0, sum = 0;
  rep(i, n - 1) {
    cur += a[i];
    sum += cur;
  }
  printf("%lld\\n", sum);
}

int main() {
  while (cin >> n, n) {
    rep(i, n) cin >> a[i];
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

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

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

int n, l[1024];

int solve() {
  sort(l, l + n);
  int ans, cur;
  ans = cur = l[0];
  for (int i = 1; i <= n - 2; ++i) {
    cur += l[i];
    ans += cur;
  }
  return ans;
}

int main() {
  while (cin >> n, n) {
    rep(i, n) cin >> l[i];
    cout << solve() << endl;
  }
  return 0;
}
Problem p00112 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
long long n, s[10001], count;
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    count = 0;
    for (int i = 0; i < n; i++)
      cin >> s[i];
    for (int i = 0; i < n - 1; i++) {
      for (int j = n - 1; j > i; j--) {
        if (s[j] < s[j - 1]) {
          int tmp = s[j];
          s[j] = s[j - 1];
          s[j - 1] = tmp;
        }
      }
    }
    for (int i = 0; i < n; i++)
      count += s[i] * (n - 1 - i);
    cout << count << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, t[10001], count;
  while (true) {
    cin >> n;
    count = 0;
    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int i = 0; i < n - 1; i++) {
      for (int j = n - 1; j > i; j--) {
        if (t[j] < t[j - 1]) {
          int tmp = t[j];
          t[j] = t[j - 1];
          t[j - 1] = tmp;
        }
      }
    }
    for (int i = 0; i < n - 1; i++)
      count += (n - 1 - i) * t[i];
    cout << count << endl;
  }
  return 0;
}
Problem p00112 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  for (;;) {
    long long n, a[10001], sum;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    sort(a, a + n, greater<int>());
    sum = 0;
    for (int i = 1; i < n; i++)
      sum += a[i] * i;
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  for (;;) {
    int n, a[10000], sum;
    cin >> n;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    sort(a, a + n, greater<int>());
    sum = 0;
    for (int i = 1; i < n; i++)
      sum += a[i] * i;
    cout << sum << endl;
  }
  return 0;
}
Problem p00113 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int n, m, a, b, c, d;
int ans[10000000];
int flg[10000000];
int main() {
  for (int i = 0; i <= 10000000; i++)
    flg[i] = -1;
  while (cin >> n >> m) {
    for (int i = 0; i <= d; i++)
      flg[i] = -1;

    n = n % m;
    d = c = -1;
    for (int i = 0; i <= 10000000; i++) {
      n *= 10;
      if (flg[n] != -1) {
        c = i;
        break;
      }
      flg[n] = i;
      d = max(d, n);
      ans[i] = n / m;
      cout << ans[i];
      n = n % m;
      if (n == 0)
        break;
    }
    cout << endl;
    if (c != -1) {
      for (int i = 1; i <= c; i++) {
        if (i <= flg[n]) {
          cout << ' ';
        } else {
          cout << '^';
        }
      }
      cout << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n, m, a, b, c;
int ans[10000000];
int flg[10000000];
int main() {
  while (cin >> n >> m) {
    for (int i = 0; i <= 10000000; i++)
      flg[i] = -1;
    n = n % m;
    c = -1;
    for (int i = 0; i <= 10000000; i++) {
      n *= 10;
      if (flg[n] != -1) {
        c = i;
        break;
      }
      flg[n] = i;
      ans[i] = n / m;
      cout << ans[i];
      n = n % m;
      if (n == 0)
        break;
    }
    cout << endl;
    if (c != -1) {
      for (int i = 1; i <= c; i++) {
        if (i <= flg[n]) {
          cout << ' ';
        } else {
          cout << '^';
        }
      }
      cout << endl;
    }
  }
  return 0;
}
Problem p00113 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

int res[100];

int main() {
  int p, q;
  while (cin >> p >> q) {
    memset(res, 0, sizeof(res));
    p *= 10;
    int i = 0;
    int k = 0;
    while (p) {
      for (k = 0; k < i; k++) {
        if (res[k] == p)
          goto end;
      }
      res[i++] = p;
      cout << p / q;
      p -= p / q * q;
      p *= 10;
    }
  end:;
    cout << endl;
    int j;
    if (p) {
      for (j = 0; j < i; j++) {
        if (j >= k)
          cout << "^";
        else
          cout << " ";
      }
      cout << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

int res[10000010];

int main() {
  int p, q;
  while (cin >> p >> q) {
    memset(res, 0, sizeof(res));
    p *= 10;
    int i = 0;
    while (p && !res[p]) {
      res[p] = i++;
      cout << p / q;
      p -= p / q * q;
      p *= 10;
    }
    cout << endl;
    int j;
    if (p) {
      for (j = 0; j < i; j++) {
        if (j >= res[p])
          cout << "^";
        else
          cout << " ";
      }
      cout << endl;
    }
  }
  return 0;
}
Problem p00113 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
map<int, bool> EX;
int p, q;
bool found;
int debug;

int dfs(int v, int &cnt) {
  if (v == 0)
    return v;
  if (EX[v])
    return v;
  EX[v] = true;

  int va = v / q;
  if (va >= 9)
    va = 9;
  cout << va;
  int res = dfs((v - q * va) * 10, cnt);
  debug++;
  if (found)
    return res;
  if (v == res)
    found = true;
  cnt++;
  return res;
}

int main() {
  while (cin >> p >> q) {
    debug = 0;
    found = false;
    EX.clear();
    int cnt = 0;
    int res = dfs(p * 10, cnt);
    cout << endl;
    if (!res)
      continue;
    rep(i, debug - cnt) cout << ' ';
    rep(i, cnt) cout << '^';
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
using namespace std;
typedef unsigned long long ll;
ll p, q;
bool ch;
bool flag;
string result;
string result2;
map<ll, bool> index;
int debug;

ll dfs(ll value) {
  debug++;
  assert(debug <= 85);
  if (!value) {
    ch = true;
    flag = false;
    return -(1 << 29);
  }
  // cout<< "value = " << value << " : " << result << endl;
  assert(value >= 0);
  if (index[value])
    return value;
  index[value] = true;
  if (value < q) {
    result += "0";
    ll res = dfs(value * 10);
    if (flag)
      result2 += "^";
    else
      result2 += " ";
    if (res == value)
      flag = false;
    return res;
  } else {
    ll v = value / q;
    if (v >= 9)
      v = 9;

    result += (char)('0' + v);

    ll res = dfs(10 * (value - q * v));
    if (flag)
      result2 += "^";
    else
      result2 += " ";
    if (res == value)
      flag = false;
    return res;
  }
}

int main() {

  while (cin >> p >> q) {
    ch = false;
    debug = 0;
    result2.clear();
    result.clear();
    index.clear();
    flag = true;
    dfs(p);
    reverse(result2.begin(), result2.end());
    cout << result.substr(1) << endl;
    if (!ch) {
      string res = result.substr(1);
      // cout << result2.substr(1) << endl;
      int counter = 0;
      rep(i, result2.size()) if (result2[i] == '^') counter++;
      string add =
          string((int)res.size() - counter, ' ') + string(counter, '^');
      cout << add << endl;
    }
  }
  return 0;
}
Problem p00113 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

int fin(vector<pii> vec, int x) {
  rep(i, vec.size()) {
    if (vec[i].first == x)
      return vec[i].second;
  }
  return -1;
}

int main() {
  int a, b;
  while (cin >> a >> b) {
    static vector<pii> used[1000001];
    rep(i, 1000001) used[i].clear();
    string ans = "";
    int c = 0;
    a *= 10;
    while (1) {
      int x = a / b;
      int y = a % b;
      //            cout<<x<<" "<<y<<endl;
      if (y == 0) {
        ans += '0' + x;
        cout << ans << endl;
        break;
      }
      int res = -1;
      if (used[y].size() && (res = fin(used[y], x)) != -1) {
        cout << ans << endl;
        rep(i, res) cout << " ";
        rep(i, ans.size() - res) cout << '^';
        cout << endl;
        break;
      }
      ans += '0' + x;
      used[y].pb(pii(x, c));
      a -= b * x;
      c++;
      a *= 10;
    }
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999
int ans[11][11] = {};

int main() {
  int a, b;
  while (cin >> a >> b) {
    static int used[10000001] = {};
    memset(used, -1, sizeof(used));
    rep(i, 10000001) used[i] = -1;
    string ans = "";
    int c = 0;
    a *= 10;
    while (1) {
      int x = a / b;
      int y = a % b;
      ans += '0' + x;
      if (y == 0) {
        cout << ans << endl;
        break;
      }
      if (used[y] != -1) {
        cout << ans << endl;
        rep(i, used[y] + 1) cout << " ";
        rep(i, ans.size() - 1 - used[y]) cout << '^';
        cout << endl;
        break;
      }
      used[y] = c;
      a -= b * x;
      c++;
      a *= 10;
    }
  }
}