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

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int dfs(int s, int g) {
  if (s == g)
    return 0;
  int n = 1;
  while (1) {
    int a = g / n, b = s / n;
    if (g < 0 && g % n)
      a--;
    if (s > 0 && s % n)
      b++;
    if (a - b > 0)
      n <<= 1;
    else {
      n >>= 1;
      int x = s / n, y = g / n;
      if (s > 0 && s % n)
        x++;
      if (g < 0 && g % n)
        y--;
      return y - x + dfs(s, x * n) + dfs(y * n, g);
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    int s, d;
    scanf("%d%d", &s, &d);
    printf("%d\\n", dfs(s, d));
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int dfs(int s, int g) {
  if (s == g)
    return 0;
  int n = 1;
  while (1) {
    int a = g / n, b = s / n;
    if (g < 0 && g % n)
      a--;
    if (s > 0 && s % n)
      b++;
    if (a - b > 0)
      n <<= 1;
    else {
      n >>= 1;
      int x = s / n, y = g / n;
      if (s > 0 && s % n)
        x++;
      if (g < 0 && g % n)
        y++;
      return 1 + dfs(s, x * n) + dfs(y * n, g);
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    int s, d;
    scanf("%d%d", &s, &d);
    printf("%d\\n", dfs(s, d));
  }
}
Problem p00284 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int BITS = 31;

vector<long long int> memo(BITS);
long long int power(int i) { return memo[i] = (i == 0 ? 1 : 2 * power(i - 1)); }

int Dijkstra(long long int s, long long int d) {
  typedef tuple<int, long long int> Node;

  bitset<BITS> start(s > 0 ? s : -s);

  unordered_set<long long int> visited;
  priority_queue<Node, vector<Node>, greater<Node>> q;
  q.emplace(0, s);
  while (!q.empty()) {
    int step;
    long long int station;
    tie(step, station) = q.top();
    q.pop();
    if (station == d)
      return step;
    visited.insert(station);

    for (int i = BITS - 1; 0 <= i; --i) {
      if (station != 0 && (station % power(i)) != 0)
        continue;
      long long int next = station + power(i);
      if (d < next)
        continue;
      if (visited.count(next))
        continue;
      q.emplace(step + 1, next);
      break;
    }
  }

  return 0;
};

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    long long int s, d;
    cin >> s >> d;

    cout << Dijkstra(s, d) << endl;
  }
}

Error: TLE

Fixed submission:

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

const int BITS = 30;

vector<long long int> memo(BITS);
long long int power(int i) { return memo[i] = (i == 0 ? 1 : 2 * power(i - 1)); }

int Dijkstra(long long int s, long long int d) {
  typedef tuple<int, long long int> Node;

  bitset<BITS> start(s > 0 ? s : -s);

  unordered_set<long long int> visited;
  priority_queue<Node, vector<Node>, greater<Node>> q;
  q.emplace(0, s);
  while (!q.empty()) {
    int step;
    long long int station;
    tie(step, station) = q.top();
    q.pop();
    if (station == d)
      return step;
    visited.insert(station);

    for (int i = BITS - 1; 0 <= i; --i) {
      if (station != 0 && (station % power(i)) != 0)
        continue;
      long long int next = station + power(i);
      if (d < next)
        continue;
      if (visited.count(next))
        continue;
      q.emplace(step + 1, next);
    }
  }

  return 0;
};

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    long long int s, d;
    cin >> s >> d;

    cout << Dijkstra(s, d) << endl;
  }
}
Problem p00284 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, a, b;
  cin >> n;
  while (n--) {
    cin >> a >> b;

    if ((a < 0 && b < 0) || (a > 0 && b > 0)) {
      if (a < 0) {
        a *= -1;
        b *= -1;
        swap(a, b);
      }
      int l = 0;
      while (a < b) {
        int k = __builtin_ctz(a);
        while (a > b - (1 << k))
          k--;
        a += 1 << k;
        l++;
      }
      cout << l << endl;
    } else
      cout << __builtin_popcount(-a) + __builtin_popcount(b) << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n, a, b;
  cin >> n;
  while (n--) {
    cin >> a >> b;

    if (a * b > 0) {
      if (a < 0)
        a *= -1, b *= -1, swap(a, b);
      assert(a > 0);
      int l = 0;
      while (a < b) {
        int k = __builtin_ctz(a);
        while (a > b - (1 << k))
          k--;
        a += 1 << k;
        l++;
      }
      cout << l << endl;
    } else
      cout << __builtin_popcount(-a) + __builtin_popcount(b) << endl;
  }
}
Problem p00284 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
long long power[32];
int main() {
  int s, t;
  power[0] = 1;
  for (int i = 1; i < 31; i++) {
    power[i] = power[i - 1] * 2;
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s >> t;
    int cnt = 0;
    long long v = s;
    while (t > v) {
      for (int i = 30; i >= 0; i--) {
        if (t - v < power[i]) {
          continue;
        }
        if ((v % power[i]) == 0) {
          cnt++;
          v += power[i];
          break;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int power[32];
int main() {
  int s, t;
  power[0] = 1;
  for (int i = 1; i < 31; i++) {
    power[i] = power[i - 1] * 2;
  }
  while (true) {
    cin >> s >> t;
    if (s == 0 && t == 0) {
      break;
    }
    int cnt = 0;
    int v = s;
    while (t > v) {
      for (int i = 30; i >= 0; i--) {
        if (t - v < power[i]) {
          continue;
        }
        if ((v % power[i]) == 0) {
          cnt++;
          v += power[i];
          break;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00285 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define N 12
using namespace std;
int Cal(int, int);

int main() {
  int M, W, inbm, inbw, ans, dp[(1 << N)];
  vector<int> bm, bw;
  while (1) {
    cin >> M >> W;
    if (!M && !W)
      break;
    for (int i = 0; i < M; i++)
      cin >> inbm, bm.push_back(inbm);
    for (int i = 0; i < W; i++)
      cin >> inbw, bw.push_back(inbw);
    if (M > W) {
      swap(M, W);
      swap(bm, bw);
    }
    for (int i = 0; i < (1 << N); i++)
      dp[i] = 0;
    ans = 0;
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < (1 << W); j++) {
        if (i == __builtin_popcount(j)) {
          for (int k = 0; k < W; k++) {
            if (!(j & (1 << k))) {
              dp[(j | (1 << k))] =
                  max(dp[(j | (1 << k))], dp[j] + Cal(bm[i], bw[k]));
              ans = max(ans, dp[(j | (1 << k))]);
            }
          }
        }
      }
    }
    bm.clear();
    bw.clear();
    cout << ans << endl;
  }
  return 0;
}

int Cal(int m, int w) {
  return abs(m - w) * (abs(m - w) - 30) * (abs(m - w) - 30);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int dp[1 << 12][1 << 12];
int main() {
  int m, w, bm[12], bw[12];
  while (1) {
    cin >> m >> w;
    if (m == 0)
      break;
    int ans = 0;
    for (int i = 0; i < (1 << m); i++)
      for (int j = 0; j < (1 << w); j++)
        dp[i][j] = 0;
    for (int i = 0; i < m; i++)
      cin >> bm[i];
    for (int i = 0; i < w; i++)
      cin >> bw[i];

    for (int i = 0; i < (1 << m); i++) {
      for (int j = 0; j < (1 << w); j++) {
        if (__builtin_popcount(i) != __builtin_popcount(j))
          continue;
        for (int k = 0; k < m; k++)
          for (int l = 0; l < w; l++) {
            if ((i & (1 << k)) != 0 || (j & (1 << l)) != 0)
              continue;
            int t = abs(bm[k] - bw[l]) * (abs(bm[k] - bw[l]) - 30) *
                    (abs(bm[k] - bw[l]) - 30);
            dp[i + (1 << k)][j + (1 << l)] =
                max(dp[i + (1 << k)][j + (1 << l)], dp[i][j] + t);
            ans = max(ans, dp[i + (1 << k)][j + (1 << l)]);
          }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00285 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

int M, W;
int bm[12], bw[12];

inline int calc(int m, int w) {
  int d = abs(bm[m] - bw[w]);
  return d * (d - 30) * (d - 30);
}

int dp[13][4100];
int dfs(int x, int mask) {
  if (dp[x][mask] >= 0)
    return dp[x][mask];
  if (x == M || mask == (1 << W) - 1)
    return 0;

  int ret = dfs(x + 1, mask);
  rep(i, W) {
    if (mask >> i & 1)
      continue;
    ret = max(ret, dfs(x + 1, mask + (1 << i)) + calc(x, i));
  }

  return dp[x][mask] = ret;
}

int main() {
  while (scanf(" %d %d", &M, &W), M) {
    rep(i, M) scanf(" %d", &bm[i]);
    rep(i, W) scanf(" %d", &bw[i]);

    memset(dp, -1, sizeof(dp));
    printf("%d\\n", dfs(0, 0));
  }
  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

int M, W;
int bm[12], bw[12];

inline int calc(int m, int w) {
  int d = abs(bm[m] - bw[w]);
  return d * (d - 30) * (d - 30);
}

int dp[4100][4100];
int dfs(int x, int y) {
  if (dp[x][y] >= 0)
    return dp[x][y];
  if (x == (1 << M) - 1 || y == (1 << W) - 1)
    return 0;

  int ret = 0;
  rep(i, M) rep(j, W) {
    if (x >> i & 1 || y >> j & 1)
      continue;
    ret = max(ret, dfs(x + (1 << i), y + (1 << j)) + calc(i, j));
  }

  return dp[x][y] = ret;
}

int main() {
  while (scanf(" %d %d", &M, &W), M) {
    rep(i, M) scanf(" %d", &bm[i]);
    rep(i, W) scanf(" %d", &bw[i]);

    memset(dp, -1, sizeof(dp));
    printf("%d\\n", dfs(0, 0));
  }
  return 0;
}
Problem p00285 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef long long ll;
typedef long double ld;

#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define fr first
#define sc second
#define pb push_back

const int INF = 1000000000;

int cal(int bm, int bw) {
  int d = abs(bm - bw);
  return d * (d - 30) * (d - 30);
}

int main() {
  while (1) {
    int m, w;
    int bm[15], bw[15];
    scanf("%d%d", &m, &w);
    if (m == 0 && w == 0)
      break;
    rep(i, m) scanf("%d", &bm[i]);
    rep(i, w) scanf("%d", &bw[i]);
    static int dp[15][1 << 12] = {};
    rep(i, 15) rep(j, (1 << 12)) { dp[i][j] = 0; }
    int ret = 0;
    rep(i, m) {
      rep(j, (1 << w)) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        rep(k, w) {
          if ((j >> k) & 1)
            continue;
          dp[i + 1][j | (1 << k)] =
              max(dp[i + 1][j | (1 << k)], dp[i][j] + cal(bm[i], bw[k]));
        }
        ret = max(ret, dp[i][j]);
      }
    }
    rep(j, (1 << w)) ret = max(ret, dp[m][j]);
    printf("%d\\n", ret);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef long long ll;
typedef long double ld;

#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define fr first
#define sc second
#define pb push_back

const int INF = 1000000000;

int cal(int bm, int bw) {
  int d = abs(bm - bw);
  return d * (d - 30) * (d - 30);
}

int main() {
  while (1) {
    int m, w;
    int bm[15], bw[15];
    scanf("%d%d", &m, &w);
    if (m == 0 && w == 0)
      break;
    rep(i, m) scanf("%d", &bm[i]);
    rep(i, w) scanf("%d", &bw[i]);
    int dp[12][1 << 12] = {};
    int ret = 0;
    rep(i, m) {
      rep(j, (1 << w)) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        rep(k, w) {
          if ((j >> k) & 1)
            continue;
          dp[i + 1][j | (1 << k)] =
              max(dp[i + 1][j | (1 << k)], dp[i][j] + cal(bm[i], bw[k]));
        }
        ret = max(ret, dp[i][j]);
      }
    }
    rep(j, (1 << w)) ret = max(ret, dp[m][j]);
    printf("%d\\n", ret);
  }
}
Problem p00285 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int m, w;
int bm[22], bw[22];
int memo[22][1 << 13];

int e(int a, int b) {

  int p = abs(bm[a] - bw[b]);
  return p * (p - 30) * (p - 30);
}

int solve(int cntm, int bitw) {

  if (~memo[cntm][bitw])
    return memo[cntm][bitw];
  if (cntm == m)
    return 0;

  int ret = 0;
  for (int i = 0; i < w; i++)
    if (!(bitw & (1 << i)))
      ret = max(ret, solve(cntm + 1, (bitw | (1 << i))) + e(cntm, i));

  ret = max(ret, solve(cntm + 1, bitw));

  return memo[cntm][bitw] = ret;
}

int main() {

  while (cin >> m >> w, m | w) {
    for (int i = 0; i < m; i++)
      cin >> bm[i];
    for (int i = 0; i < w; i++)
      cin >> bw[i];

    memset(memo, -1, sizeof(memo));
    cout << solve(0, 0) << endl;
  }
}

Error: MLE

Fixed submission:

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

int m, w;
int bm[22], bw[22];
int memo[1 << 12][1 << 12];

int e(int i, int j) {

  int p = abs(bm[i] - bw[j]);
  return p * (p - 30) * (p - 30);
}

int solve(int bitm, int bitw) {

  if (~memo[bitm][bitw])
    return memo[bitm][bitw];

  int ret = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < w; j++) {
      if (!(bitm & (1 << i)) && !(bitw & (1 << j))) {
        ret = max(ret, solve((bitm | (1 << i)), (bitw | (1 << j))) + e(i, j));
      }
    }
  }

  return memo[bitm][bitw] = ret;
}
int main() {

  while (cin >> m >> w) {
    for (int i = 0; i < m; i++)
      cin >> bm[i];
    for (int i = 0; i < w; i++)
      cin >> bw[i];

    memset(memo, -1, sizeof(memo));
    cout << solve(0, 0) << endl;
  }
}
Problem p00285 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int dp[13][(1 << 13)];
vector<int> bmin;
vector<int> bmax;

int fn(int bm, int bw) { return abs(bm - bw) * pow((abs(bm - bw) - 30), 2); }

int solve(int idx, int s) {

  if (idx == bmin.size())
    return dp[idx][s] = 0;
  if (dp[idx][s] != -1)
    return dp[idx][s];

  int res = -1;
  for (int i = 0; i < bmax.size(); i++) {
    int cost = fn(bmin[idx], bmax[i]);
    if (s >> i & 1)
      continue;
    res = max(res, solve(idx + 1, s | (1 << i)) + cost);
  }

  return dp[idx][s] = res;
}

int main() {

  int M, W;
  while (1) {

    vector<int> bm;
    vector<int> bw;
    cin >> M >> W;
    if (M == 0 && W == 0)
      break;
    bm.resize(M);
    bw.resize(W);
    for (int i = 0; i < M; i++)
      cin >> bm[i];
    for (int i = 0; i < W; i++)
      cin >> bw[i];

    memset(dp, -1, sizeof(dp));

    if (M < W) {
      bmin = bm;
      bmax = bw;
    } else {
      bmin = bw;
      bmax = bm;
    }

    cout << solve(0, 0) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[12][(1 << 12)];
vector<int> bmin;
vector<int> bmax;

int fn(int bm, int bw) { return abs(bm - bw) * pow((abs(bm - bw) - 30), 2); }

int solve(int idx, int s) {

  if (idx == bmin.size())
    return dp[idx][s] = 0;
  if (dp[idx][s] != -1)
    return dp[idx][s];

  int res = -1;
  for (int i = 0; i < bmax.size(); i++) {
    int cost = fn(bmin[idx], bmax[i]);
    if (s >> i & 1)
      continue;
    res = max(res, solve(idx + 1, s | (1 << i)) + cost);
  }

  return dp[idx][s] = res;
}

int main() {

  int M, W;
  while (1) {
    // int bm[12], bw[12];
    vector<int> bm;
    vector<int> bw;
    cin >> M >> W;
    if (M == 0 && W == 0)
      break;
    bm.resize(M);
    bw.resize(W);
    for (int i = 0; i < M; i++)
      cin >> bm[i];
    for (int i = 0; i < W; i++)
      cin >> bw[i];

    memset(dp, -1, sizeof(dp));

    if (M < W) {
      bmin = bm;
      bmax = bw;
    } else {
      bmin = bw;
      bmax = bm;
    }

    cout << solve(0, 0) << endl;
  }

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

#include <bits/stdc++.h>

int main() {
  int N;
  std::cin >> N;
  int K[N], P[N];
  for (int a = 0; a < N; ++a) {
    std::cin >> K[a] >> P[a];
  }

  int roop;
  int sum;

  for (int b = 0; b < N; ++b) {
    roop = 1;
    while (1) {
      if (K[b] > (P[b] * roop)) {
        ++roop;
        continue;
      } else if (K[b] <= (P[b] * roop)) {
        --roop;
        sum = K[b] - (P[b] * roop);
        if (sum == 0) {
          std::cout << P[b] << std::endl;
          roop = 1;
          break;
        } else if (sum != 0) {
          std::cout << sum << std::endl;
          roop = 1;
          break;
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int main() {
  int N;
  std::cin >> N;
  int K[N], P[N];
  for (int a = 0; a < N; ++a) {
    std::cin >> K[a] >> K[a];
  }

  int roop;
  int sum;

  for (int b = 0; b < N; ++b) {
    roop = 1;
    while (1) {
      if (K[b] > P[b] * roop) {
        ++roop;
        continue;
      } else if (K[b] <= P[b] * roop) {
        --roop;
        sum = K[b] - (P[b] * roop);
        if (sum == 0) {
          std::cout << P[b] << std::endl;
          break;
        } else if (sum != 0) {
          std::cout << sum << std::endl;
          break;
        }
      }
    }
  }
}
Problem p00292 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
int main() {
  int n, k[1000], p[1000], b;
  scanf("%d", &n);
  for (int i = 0; i <= n - 1; i++) {
    scanf("%d %d", &k[i], &p[i]);
  }
  for (int i = 0; i <= n - 1; i++) {
    if (k[i] % p[i] == 0)
      printf("%d\\n", p[i]);
    else
      printf("%d\\n", k[i] % p[i]);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
int main() {
  int n, k, p, b;
  scanf("%d", &n);
  for (int i = 0; i <= n - 1; i++) {
    scanf("%d", &k, &p);
    if (k % p == 0)
      printf("%d\\n", p);
    else if (k % p != 0)
      b = k % p;
    printf("%d\\n", b);
  }
  return 0;
}
Problem p00292 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;

  cin >> n;
  int s = 0;
  vector<int> a(n);
  vector<int> b(n);
  while (s <= n - 1) {
    cin >> a[s] >> b[s];
    s++;
  }
  s = 0;
  while (s <= n - 1) {
    if (a[s] % b[s] == 0) {
      cout << b[s] << endl;
      s++;
    } else {
      cout << a[s] % b[s] << endl;
      s++;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;

  cin >> n;
  int s = 0;
  vector<int> a(n);
  vector<int> b(n);
  while (s <= n - 1) {
    cin >> a[s] >> b[s];
    s++;
  }
  s = 0;
  while (s <= n - 1) {
    if (a[s] % b[s] == 0) {
      cout << n << endl;
    } else {
      cout << a[s] % b[s] << endl;
      s++;
    }
  }
  return 0;
}
Problem p00292 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;
  int k[N], p[N];
  for (int a = 0; a < N; ++a) {
    cin >> k[a] >> p[a];
  }
  for (int i = 0; i < N; ++i) {
    if (k[i] % p[i] == 0) {
      cout << p[i] << endl;
    } else {
      cout << k[i] % p[i] << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  int k[N], p[N];
  cin >> k[N - 1] >> p[N - 1];
  for (int i = 0; i < N; ++i) {
    cout << k[i] % p[i] << endl;
  }
  return 0;
}
Problem p00292 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  int K[N];
  int P[N];
  for (int i = 0; i < N; i++) {
    cin >> K[i] >> P[i];
  }
  for (int j = 0; j < N; j++) {
    if (K[j] % P[j] == 0) {
      cout << P[j] << endl;
    } else {
      cout << K[j] % P[j] << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N;
  int K[N];
  int P[N];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> K[i] >> P[i];
  }
  for (int j = 0; j < N; j++) {
    if (K[j] % P[j] == 0) {
      cout << P[j] << endl;
    } else {
      cout << K[j] % P[j] << endl;
    }
  }
  return 0;
}
Problem p00292 (C++) — buggy submission, verdict: Runtime Error


#include <iostream>
using namespace std;

int main() {
  int N = 0;
  int K[10000], P[10000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    cin >> P[i];
  }
  for (int i = 0; i < N; i++) {
    int ans = K[i] % P[i];
    if (ans == 0) {
      cout << P[i] << endl;
    } else {
      cout << ans << endl;
    }
  }

  return 0;
}

Error: -8

Fixed submission:


#include <iostream>
using namespace std;

int main() {
  int N = 0;
  int K[10000], P[10000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> K[i];
    P[i];
  }
  for (int i = 0; i < N; i++) {
    int ans = K[i] % P[i];
    if (ans == 0) {
      cout << P[i] << endl;
    } else {
      cout << ans << endl;
    }
  }

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

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> s(n);
  vector<int> m(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> m[i];
  }
  for (int i = 0; i < n; i++) {
    if (s[i] % m[i] == 0) {
      cout << m[i] << endl;
    } else {
      cout << s[i] % m[i] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> s(n * 2 - 1);
  for (int i = 0; i < 2 * n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < 2 * n - 1; i + 2) {
    if (s[i] % s[i + 1] == 0) {
      cout << s[i + 1] << endl;
    } else {
      cout << s[i] % s[i + 1] << endl;
    }
  }
  return 0;
}