prithivMLmods/Coder-Stat huggingface dataset:
Problem p03820 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
int n, k;
int pw[4010];
const int md = 1e9 + 7;
int qmul(int x, int p) {
  if (p == 0)
    return 1;
  if (p == 1)
    return x;
  int res = qmul(x, p >> 1);
  res = 1LL * res * res % md;
  if (p & 1)
    res = 1LL * res * x % md;
  return res;
}
int C(int x, int y) {
  if (x < y || x < 0 || y < 0)
    return 0;
  return 1LL * pw[x] * qmul(pw[x - y], md - 2) % md * qmul(pw[y], md - 2) % md;
}
int main() {
  scanf("%d%d", &n, &k);
  pw[0] = 1;
  for (int i = 1; i <= 4000; i++)
    pw[i] = 1LL * pw[i - 1] * i % md;
  printf("%d", 1LL * (C(n + k - 2, n - 1) + md - C(n + k - 2, n)) *
                   qmul(2, n - k - 1) % md);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
int n, k;
int pw[4010];
const int md = 1e9 + 7;
int qmul(int x, int p) {
  if (p <= 0)
    return 1;
  if (p == 1)
    return x;
  int res = qmul(x, p >> 1);
  res = 1LL * res * res % md;
  if (p & 1)
    res = 1LL * res * x % md;
  return res;
}
int C(int x, int y) {
  if (x < y || x < 0 || y < 0)
    return 0;
  return 1LL * pw[x] * qmul(pw[x - y], md - 2) % md * qmul(pw[y], md - 2) % md;
}
int main() {
  scanf("%d%d", &n, &k);
  pw[0] = 1;
  for (int i = 1; i <= 4000; i++)
    pw[i] = 1LL * pw[i - 1] * i % md;
  printf("%d", 1LL * (C(n + k - 2, n - 1) + md - C(n + k - 2, n)) *
                   qmul(2, n - k - 1) % md);
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
  mail: mleautomaton@foxmail.com
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define ll long long
#define REP(a, b, c) for (int a = b; a <= c; a++)
#define re register
#define file(a)                                                                \
  freopen(a ".in", "r", stdin);                                                \
  freopen(a ".out", "w", stdout)
inline int gi() {
  int f = 1, sum = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    sum = (sum << 3) + (sum << 1) + ch - '0';
    ch = getchar();
  }
  return f * sum;
}
const int N = 2010, Mod = 1e9 + 7;
int f[N][N];
int qpow(int a, int b) {
  int ret = 1;
  while (b) {
    if (b & 1)
      ret = 1ll * ret * a % Mod;
    b >>= 1;
    a = 1ll * a * a % Mod;
  }
  return ret;
}
int n, k;
int main() {
  n = gi();
  k = gi();
  f[0][n + 1] = 1;
  for (int i = 1; i < k; i++) {
    int sum = f[i - 1][n - i + 2];
    for (int j = n - i + 1; j >= 2; j--) {
      sum = (sum + f[i - 1][j]) % Mod;
      f[i][j] = sum;
    }
  }
  int sum = 0;
  for (int i = 2; i <= n - k + 2; i++)
    sum = (sum + f[k - 1][i]) % Mod;
  if (k == 1)
    sum = 1;
  printf("%lld\n", 1ll * qpow(2, n - k - 1) * sum % Mod);
  return 0;
}


Fixed submission:
/*
  mail: mleautomaton@foxmail.com
  author: MLEAutoMaton
  This Code is made by MLEAutoMaton
*/
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define ll long long
#define REP(a, b, c) for (int a = b; a <= c; a++)
#define re register
#define file(a)                                                                \
  freopen(a ".in", "r", stdin);                                                \
  freopen(a ".out", "w", stdout)
inline int gi() {
  int f = 1, sum = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    sum = (sum << 3) + (sum << 1) + ch - '0';
    ch = getchar();
  }
  return f * sum;
}
const int N = 2010, Mod = 1e9 + 7;
int f[N][N];
int qpow(int a, int b) {
  int ret = 1;
  while (b) {
    if (b & 1)
      ret = 1ll * ret * a % Mod;
    b >>= 1;
    a = 1ll * a * a % Mod;
  }
  return ret;
}
int n, k;
int main() {
  n = gi();
  k = gi();
  f[0][n + 1] = 1;
  for (int i = 1; i < k; i++) {
    int sum = f[i - 1][n - i + 2];
    for (int j = n - i + 1; j >= 2; j--) {
      sum = (sum + f[i - 1][j]) % Mod;
      f[i][j] = sum;
    }
  }
  int sum = 0;
  for (int i = 2; i <= n - k + 2; i++)
    sum = (sum + f[k - 1][i]) % Mod;
  if (k == 1)
    sum = 1;
  printf("%lld\n", 1ll * qpow(2, max(0, n - k - 1)) * sum % Mod);
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (int)j; i <= (int)k; i++)
#define per(i, j, k) for (int i = (int)j; i >= (int)k; i--)
using namespace std;
typedef long long LL;
const int N = 2005;
const int P = 1000000007;
inline int Pow(int a, int b) {
  int c = 1;
  for (; b; b >>= 1, a = a * 1ll * a % P)
    if (b & 1)
      c = c * 1ll * a % P;
  return c;
}
inline void add(int &a, int b) {
  a += b;
  if (a >= P)
    a -= P;
}
inline void dec(int &a, int b) {
  a -= b;
  if (a < 0)
    a += P;
}
int n, k;
int C[2 * N][2 * N];
int f[N][N];
int main() {
  scanf("%d%d", &n, &k);
  rep(i, 0, 2 * n) {
    C[i][0] = C[i][i] = 1;
    rep(j, 1, i - 1) {
      C[i][j] = C[i - 1][j];
      add(C[i][j], C[i - 1][j - 1]);
    }
  }
  f[0][0] = 1;
  rep(i, 0, n) {
    if (i) {
      rep(j, 1, n) add(f[i][j], f[i][j - 1]);
    }
    rep(j, 0, n) if (f[i][j]) {
      dec(f[i + 1][j + 2], f[i][j]);
      add(f[i + 1][1], f[i][j]);
    }
  }
  if (n == k) {
    int ans = 0;
    rep(i, 0, n - 1) add(ans, f[n - 1][i]);
    printf("%d\n", ans);
  }
  int ans = 0;
  rep(i, n - k + 1, n) {
    rep(j, 0, n - i) {
      add(ans, (f[n - i][j] * 1ll * C[i - 1 - (n - k) + j][j] % P) * 1ll *
                   C[i - 2][n - k - 1] % P);
    }
    // printf("__%d\n",ans);
  }
  ans = ans * 1ll * Pow(2, n - k - 1) % P;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (int)j; i <= (int)k; i++)
#define per(i, j, k) for (int i = (int)j; i >= (int)k; i--)
using namespace std;
typedef long long LL;
const int N = 2005;
const int P = 1000000007;
inline int Pow(int a, int b) {
  int c = 1;
  for (; b; b >>= 1, a = a * 1ll * a % P)
    if (b & 1)
      c = c * 1ll * a % P;
  return c;
}
inline void add(int &a, int b) {
  a += b;
  if (a >= P)
    a -= P;
}
inline void dec(int &a, int b) {
  a -= b;
  if (a < 0)
    a += P;
}
int n, k;
int C[2 * N][2 * N];
int f[N][N];
int main() {
  scanf("%d%d", &n, &k);
  rep(i, 0, 2 * n) {
    C[i][0] = C[i][i] = 1;
    rep(j, 1, i - 1) {
      C[i][j] = C[i - 1][j];
      add(C[i][j], C[i - 1][j - 1]);
    }
  }
  f[0][0] = 1;
  rep(i, 0, n) {
    if (i) {
      rep(j, 1, n) add(f[i][j], f[i][j - 1]);
    }
    rep(j, 0, n) if (f[i][j]) {
      dec(f[i + 1][j + 2], f[i][j]);
      add(f[i + 1][1], f[i][j]);
    }
  }
  if (n == k) {
    int ans = 0;
    rep(i, 0, n - 1) add(ans, f[n - 1][i]);
    printf("%d\n", ans);
    return 0;
  }
  int ans = 0;
  rep(i, n - k + 1, n) {
    rep(j, 0, n - i) {
      add(ans, (f[n - i][j] * 1ll * C[i - 1 - (n - k) + j][j] % P) * 1ll *
                   C[i - 2][n - k - 1] % P);
    }
    // printf("__%d\n",ans);
  }
  ans = ans * 1ll * Pow(2, n - k - 1) % P;
  printf("%d\n", ans);
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
typedef long long ll;
const int N = 2010;
const int P = 1e9 + 7;
int n, K;
int f[N][N], g[N][N];
inline ll Pow(ll x, int y) {
  ll ret = 1;
  for (; y; y >>= 1, x = x * x % P)
    if (y & 1)
      ret = ret * x % P;
  return ret;
}
inline int Add(int x, int y) {
  x += y;
  if (x >= P)
    x -= P;
  return x;
}
int main() {
  scanf("%d %d", &n, &K);
  f[0][0] = 1;
  for (int i = 2; i <= n; i++)
    f[1][i] = 1;
  for (int i = n; i >= 2; i--)
    g[1][i] = g[1][i + 1] + 1;
  for (int i = 2; i < K; i++) {
    for (int j = n - i + 1; j >= 2; j--) {
      f[i][j] = g[i - 1][j];
    }
    for (int j = n - i + 1; j >= 2; j--) {
      g[i][j] = Add(g[i][j + 1], f[i][j]);
    }
  }
  ll ans = 0;
  for (int i = 2; i <= n - K + 2; i++)
    ans = Add(ans, f[K - 1][i]);
  if (K == 1)
    ans = 1;
  ans = ans * Pow(2, n - K - 1) % P;
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
typedef long long ll;
const int N = 2010;
const int P = 1e9 + 7;
int n, K;
int f[N][N], g[N][N];
inline ll Pow(ll x, int y) {
  ll ret = 1;
  for (; y > 0; y >>= 1, x = x * x % P)
    if (y & 1)
      ret = ret * x % P;
  return ret;
}
inline int Add(int x, int y) {
  x += y;
  if (x >= P)
    x -= P;
  return x;
}
int main() {
  scanf("%d %d", &n, &K);
  f[0][0] = 1;
  for (int i = 2; i <= n; i++)
    f[1][i] = 1;
  for (int i = n; i >= 2; i--)
    g[1][i] = g[1][i + 1] + 1;
  for (int i = 2; i < K; i++) {
    for (int j = n - i + 1; j >= 2; j--) {
      f[i][j] = g[i - 1][j];
    }
    for (int j = n - i + 1; j >= 2; j--) {
      g[i][j] = Add(g[i][j + 1], f[i][j]);
    }
  }
  ll ans = 0;
  for (int i = 2; i <= n - K + 2; i++)
    ans = Add(ans, f[K - 1][i]);
  if (K == 1)
    ans = 1;
  ans = ans * Pow(2, n - K - 1) % P;
  printf("%lld\n", ans);
  return 0;
}
Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define N 2000 + 5
#define Mod 1000000007
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define dep(i, l, r) for (int i = r; i >= l; --i)
int n, k, sum[N], dp[N][N];
int read() {
  char c;
  int x = 0, f = 1;
  c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
int Inc(int a, int b) { return (a += b) >= Mod ? a - Mod : a; }
int Mul(int a, int b) { return 1ll * a * b % Mod; }
int Qpow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = Mul(ans, a);
    a = Mul(a, a), b >>= 1;
  }
  return ans;
}
int main() {
  n = read(), k = read();
  dp[0][n + 1] = 1;
  rep(i, 1, n + 1) sum[i] = 1;
  rep(i, 1, k) {
    dep(j, 1, n - i + 1) {
      if (j != 1)
        dp[i][j] = Inc(dp[i][j], dp[i - 1][j]);
      dp[i][j] = Inc(dp[i][j], sum[j + 1]);
    }
    sum[n + 1] = 0;
    dep(j, 1, n) sum[j] = Inc(sum[j + 1], dp[i][j]);
  }
  printf("%d", Mul(dp[k][1], Qpow(2, n - k - 1)));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 2000 + 5
#define Mod 1000000007
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define dep(i, l, r) for (int i = r; i >= l; --i)
int n, k, sum[N], dp[N][N];
int read() {
  char c;
  int x = 0, f = 1;
  c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
int Inc(int a, int b) { return (a += b) >= Mod ? a - Mod : a; }
int Mul(int a, int b) { return 1ll * a * b % Mod; }
int Qpow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = Mul(ans, a);
    a = Mul(a, a), b >>= 1;
  }
  return ans;
}
int main() {
  n = read(), k = read();
  dp[0][n + 1] = 1;
  rep(i, 1, n + 1) sum[i] = 1;
  rep(i, 1, k) {
    dep(j, 1, n - i + 1) {
      if (j != 1)
        dp[i][j] = Inc(dp[i][j], dp[i - 1][j]);
      dp[i][j] = Inc(dp[i][j], sum[j + 1]);
    }
    sum[n + 1] = 0;
    dep(j, 1, n) sum[j] = Inc(sum[j + 1], dp[i][j]);
  }
  if (n == k)
    printf("%d\n", dp[k][1]);
  else
    printf("%d", Mul(dp[k][1], Qpow(2, n - k - 1)));
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define dump(x) cout << (x) << endl
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> V;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  ll cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    a[i] += cnt;
    int x = b[i];
    while (a[i] > b[i])
      b[i] += x;
    cnt += b[i] % a[i];
  }
  dump(cnt);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define dump(x) cout << (x) << endl
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> V;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  ll cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    a[i] += cnt;
    if (a[i] % b[i] != 0) {
      cnt += b[i] - a[i] % b[i];
    }
    // if (a[i] >= b[i]) {
    //   if (a[i]%b[i] == 0) continue;
    //   else a[i] %= b[i];
    // }
    // cnt += b[i]-a[i];
  }
  dump(cnt);
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *    author:  yuya1234
 *    created: 14.07.2020 10:30:10
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15);

  int n;
  cin >> n;

  ll a[n], b[n];
  REP(i, n) cin >> a[i] >> b[i];

  ll ans = 0;
  REPD(i, n) {
    ll cnt = 0;
    if (a[i] % b[i] != 0) {
      cnt = b[i] - a[i] % b[i];
      ans += cnt;
      REP(j, i) { a[j] += cnt; }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
/**
 *    author:  yuya1234
 *    created: 14.07.2020 10:30:10
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15);

  int n;
  cin >> n;

  ll a[n], b[n];
  REP(i, n) cin >> a[i] >> b[i];

  ll ans = 0;
  REPD(i, n) {
    if ((a[i] + ans) % b[i] != 0) {
      ans += b[i] - (a[i] + ans) % b[i];
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
int main(void) {
  long long n, a[10000], b[10000];
  long long ans = 0;
  std::cin >> n;
  for (int i = 0; i != n; i++)
    std::cin >> a[i] >> b[i];
  for (int i = n - 1; i >= 0; i--) {
    long long t = (a[i] + ans) % b[i];
    if (t != 0)
      ans += b[i] - t;
  }
  std::cout << ans << std::endl;
  return 0;
}

Fixed submission:
#include <iostream>
int main(void) {
  long long n, a[100000], b[100000];
  long long ans = 0;
  std::cin >> n;
  for (int i = 0; i != n; i++)
    std::cin >> a[i] >> b[i];
  for (int i = n - 1; i >= 0; i--) {
    long long t = (a[i] + ans) % b[i];
    if (t != 0)
      ans += b[i] - t;
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  long long int n, a[100005], b[100005], cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int j = n - 1; j >= 0; j--) {
    if (cnt != 0) {
      a[j] += cnt;
    }
    while (a[j] % b[j] != 0) {
      a[j]++;
      cnt++;
    }
  }
  cout << cnt << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  long long int n, a[100005], b[100005], cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int j = n - 1; j >= 0; j--) {
    if (cnt != 0) {
      a[j] += cnt;
    }
    long long ans = a[j] / b[j];
    if (a[j] % b[j] != 0) {
      cnt += b[j] * (ans + 1) - a[j];
    }
  }
  cout << cnt << endl;

  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define ll long long
#define int ll
#define INF (1LL << 60)
#define mod 1000000007
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

signed main() {

  int N, a[12345], b[12345];

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i] >> b[i];

  int ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    if ((a[i] + ans) % b[i] == 0)
      continue;
    else
      ans += b[i] - (a[i] + ans) % b[i];
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define ll long long
#define int ll
#define INF (1LL << 60)
#define mod 1000000007
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

signed main() {

  int N, a[123456], b[123456];

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i] >> b[i];

  int ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    if ((a[i] + ans) % b[i] == 0)
      continue;
    else
      ans += b[i] - (a[i] + ans) % b[i];
  }

  cout << ans << endl;

  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
// agc009_a.cpp
// Sat Oct 27 11:43:32 2018

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  ll a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }

  ll count = 0;
  for (int i = n - 1; i >= 0; i--) {
    while ((a[i] + count) % b[i] != 0) {
      count++;
    }
  }

  cout << count << endl;
}

Fixed submission:
// agc009_a.cpp
// Sat Oct 27 11:43:32 2018

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  ll a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }

  ll count = 0;
  for (int i = n - 1; i >= 0; i--) {
    if ((a[i] + count) % b[i] != 0) {
      count += b[i] - (a[i] + count) % b[i];
    }
  }

  cout << count << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

//--------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define LL long long
#define INF INT_MAX / 3

//--------------------------------------------
const double EPS = 1e-14;
const double PI = acos(-1.0);

//--------------------------------------------

int main() {
  int n;
  LL a[100010];
  LL b[100010];

  scanf("%d", &n);

  REP(i, n) scanf("%lld %lld", &a[i], &b[i]);

  LL ans = 0;
  RREP(i, n) {
    if (a[i] % b[i] == 0)
      i--;
    else {
      int dif = b[i] - (a[i] % b[i]);
      REP(j, i + 1) a[j] = (a[j] + dif) % b[j];
      ans += dif;
    }
  }
  printf("%lld\n", ans);
}


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

//--------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define LL long long
#define INF INT_MAX / 3

//--------------------------------------------
const double EPS = 1e-14;
const double PI = acos(-1.0);

//--------------------------------------------

int main() {
  int n;
  LL a[100010];
  LL b[100010];

  scanf("%d", &n);

  REP(i, n) scanf("%lld %lld", &a[i], &b[i]);

  LL ans = 0;
  RREP(i, n) {
    if ((ans + a[i]) % b[i] != 0) {
      ans += b[i] - ((ans + a[i]) % b[i]);
    }
  }
  printf("%lld\n", ans);
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)

int main() {
  ll N;
  cin >> N;
  ll A[N], B[N];
  rep(i, N) cin >> A[i] >> B[i];
  ll cnt = 0;
  for (ll i = N - 1; i >= 0; i--) {
    cnt += B[i] - (B[i] % (A[i] + cnt));
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)

int main() {
  ll N;
  cin >> N;
  ll A[N], B[N];
  rep(i, N) cin >> A[i] >> B[i];
  ll cnt = 0;
  for (ll i = N - 1; i >= 0; i--) {
    if (((A[i] + cnt) % B[i]) != 0) {
      cnt += B[i] - ((A[i] + cnt) % B[i]);
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
// #define INF ((LLONG_MAX) / (2))
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef long long int ll;
typedef pair<ll, int> LL_IP;
#define INF 1e9 + 7

int main() {

  int n;
  cin >> n;
  vector<pair<ll, ll>> arr(n);
  REP(i, n) { cin >> arr[i].first >> arr[i].second; }

  ll tmp = 0;
  for (int i = n - 1; i >= 0; i--) {

    arr[i].first += tmp; /*今までに加えた回数分足しておく*/
    while ((arr[i].first % arr[i].second) != 0) {
      arr[i].first++;
      tmp++;
    }
  }
  cout << tmp << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
// #define INF ((LLONG_MAX) / (2))
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef long long int ll;
typedef pair<ll, int> LL_IP;
#define INF 1e9 + 7

int main() {

  int n;
  cin >> n;
  vector<pair<ll, ll>> arr(n);
  REP(i, n) { cin >> arr[i].first >> arr[i].second; }

  ll tmp = 0;
  for (int i = n - 1; i >= 0; i--) {

    arr[i].first += tmp; /*今までに加えた回数分足しておく*/

    if (arr[i].first % arr[i].second == 0) {
      continue;
    }

    if (arr[i].second != 1) {
      tmp += (arr[i].second - (arr[i].first % arr[i].second));
    }
  }
  cout << tmp << endl;

  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
typedef long long ll;
#define pb push_back

void solve() {
  ll n;
  cin >> n;
  vector<ll> A(n), B(n);
  REP(i, n) {
    ll a, b;
    cin >> a >> b;
    A[i] = a;
    B[i] = b;
  }
  ll sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    while (A[i] % B[i] != 0) {
      A[i]++;
      sum++;
    }
    if (i != 0)
      A[i - 1] += sum;
  }
  cout << sum << endl;
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
typedef long long ll;
#define pb push_back

void solve() {
  ll n;
  cin >> n;
  vector<ll> A(n), B(n);
  REP(i, n) {
    ll a, b;
    cin >> a >> b;
    A[i] = a;
    B[i] = b;
  }
  ll sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    ll rem = A[i] % B[i];
    if (rem != 0)
      sum += B[i] - rem;
    if (i != 0)
      A[i - 1] += sum;
  }
  cout << sum << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep2l(i, s, n) for (ll i = (ll)(s); i < (ll)n; i++)

int main() {
  int N;
  cin >> N;
  deque<ll> A;
  deque<ll> B;
  ll count = 0;
  rep(i, N) {
    ll a, b;
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }
  for (int i = N - 1; i >= 0; i--) {
    A[i] += count;
    while (A[i] % B[i] != 0) {
      A[i]++;
      count++;
    }
  }
  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep2l(i, s, n) for (ll i = (ll)(s); i < (ll)n; i++)

int main() {
  int N;
  cin >> N;
  deque<ll> A;
  deque<ll> B;
  ll count = 0;
  rep(i, N) {
    ll a, b;
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }
  for (int i = N - 1; i >= 0; i--) {
    A[i] += count;
    if (A[i] % B[i] != 0) {
      count += (B[i] - (A[i] % B[i]));
    }
  }
  cout << count << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;

  vector<long long> A(N);
  vector<long long> B(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    cin >> B.at(i);
  }

  long long ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    A.at(i) += ans;
    while (A.at(i) % B.at(i) != 0) {
      A.at(i)++;
      ans++;
    }
  }

  cout << ans << endl;
}

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

int main() {
  int N;
  cin >> N;

  vector<long long> A(N);
  vector<long long> B(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    cin >> B.at(i);
  }

  long long ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    A.at(i) += ans;
    if (A.at(i) % B.at(i) == 0)
      continue;
    ans += B.at(i) - A.at(i) % B.at(i);
  }

  cout << ans << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;

  vector<ll> a(n), b(n);
  rep(i, n) cin >> a[i] >> b[i];

  ll ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    a[i] += ans;
    if (a[i] % b[i] == 0)
      continue;
    ll nb = b[i];
    while (a[i] > nb)
      nb += b[i];
    ans += nb - a[i];
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;

  vector<ll> a(n), b(n);
  rep(i, n) cin >> a[i] >> b[i];

  ll ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    a[i] += ans;
    if (a[i] % b[i] == 0)
      continue;
    ll nb = (a[i] + b[i] - 1) / b[i] * b[i];
    ans += nb - a[i];
  }
  cout << ans << endl;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, a, N) for (int i = 0 + a; i < N; i++)
#define lint long long int
#define SIZE 10005
#define pb push_back
#define MP make_pair

using namespace std;

int main() {
  lint a[SIZE], b[SIZE];
  lint n, ans = 0;
  cin >> n;
  rep(i, 0, n) { cin >> a[i] >> b[i]; }
  for (int i = n - 1; i >= 0; i--) {
    if ((a[i] + ans) % b[i] != 0) {
      ans += (b[i] - (a[i] + ans) % b[i]);
    }
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, a, N) for (int i = 0 + a; i < N; i++)
#define lint long long int
#define SIZE 100005
#define pb push_back
#define MP make_pair

using namespace std;

int main() {
  lint a[SIZE], b[SIZE];
  lint n, ans = 0;
  cin >> n;
  rep(i, 0, n) { cin >> a[i] >> b[i]; }
  for (int i = n - 1; i >= 0; i--) {
    if ((a[i] + ans) % b[i] != 0) {
      ans += (b[i] - (a[i] + ans) % b[i]);
    }
  }
  cout << ans;

  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];

  long long ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    A[i] += ans;
    long long k = (A[i] + B[i] - 1) / B[i];
    ans += B[i] * k % A[i];
  }

  cout << ans << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<long long> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];

  long long ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    A[i] += ans;
    long long r = A[i] % B[i];
    if (r == 0)
      continue;
    ans += B[i] - r;
  }

  cout << ans << endl;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long long a[10005], b[10005];

int main() {
  long long n, ans = 0;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld %lld", &a[i], &b[i]);
  for (int i = n; i >= 1; i--) {
    if ((a[i] + ans) % b[i] > 0) {
      ans = ans + b[i] - (a[i] + ans) % b[i];
    }
  }
  printf("%lld\n", ans);
  return 0;
}


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

long long a[100005], b[100005];

int main() {
  long long n, ans = 0;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld %lld", &a[i], &b[i]);
  for (int i = n; i >= 1; i--) {
    if ((a[i] + ans) % b[i] > 0) {
      ans = ans + b[i] - (a[i] + ans) % b[i];
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long int> A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  long long int cnt = 0;
  for (int i = N - 1; i >= 0; i--) {
    A[i] += cnt;
    while (A[i] % B[i] != 0) {
      A[i]++;
      cnt++;
    }
  }
  cout << cnt << endl;
}


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

int main() {
  int N;
  cin >> N;
  vector<long long int> A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  long long int cnt = 0;
  for (int i = N - 1; i >= 0; i--) {
    A[i] += cnt;
    if (A[i] % B[i] == 0)
      continue;
    else {
      if (A[i] > B[i]) {
        long long int rem = A[i] % B[i];
        cnt += B[i] - rem;
      } else if (A[i] < B[i]) {
        cnt += B[i] - A[i];
      }
    }
  }
  cout << cnt << endl;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
// lcmとか__builtin_popcountとかはg++ -std=c++17 default.cppみたいなかんじで

#include <bits/stdc++.h>
#define mod 1000000007
#define INF 1001001001
#define ll long long
#define ln cout << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;

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

  ll c, d, m, n, maxi = 0, f = 0, mini = INF, sum = 0;
  string str;
  cin >> n;
  vector<ll> b(n);
  vector<ll> a(n);
  rep(i, n) cin >> a[i] >> b[i];
  for (ll i = n - 1; i >= 0; i--) {
    // cout<<a[i]+sum<<" "<<b[i]<<" "<<(b[i])-(a[i]+sum)%b[i]<<endl;
    if (b[i] != 1 || (a[i] + sum || a[i] != 0) % a[i] != 0)
      sum += abs((b[i]) - (a[i] + sum) % b[i]);
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
// lcmとか__builtin_popcountとかはg++ -std=c++17 default.cppみたいなかんじで

#include <bits/stdc++.h>
#define mod 1000000007
#define INF 1001001001
#define ll long long
#define ln cout << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;

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

  ll c, d, m, n, maxi = 0, f = 0, mini = INF, sum = 0;
  string str;
  cin >> n;
  vector<ll> b(n);
  vector<ll> a(n);
  rep(i, n) cin >> a[i] >> b[i];
  for (ll i = n - 1; i >= 0; i--) {
    // cout<<a[i]+sum<<" "<<b[i]<<" "<<(b[i])-(a[i]+sum)%b[i]<<endl;
    if (a[i] != 0 && b[i] != 1 && (a[i] + sum) % b[i] != 0)
      sum += abs((b[i]) - (a[i] + sum) % b[i]);
  }
  cout << sum << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  ll ab[2][100010];
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> ab[0][i] >> ab[1][i];
  }
  for (int i = n - 1; i >= 0; --i) {
    ab[0][i] += ans;
    if (ab[0][i] % ab[1][i] == 0)
      continue;
    while (1) {
      ab[0][i]++;
      ans++;
      if (ab[0][i] % ab[1][i] == 0)
        break;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  ll ab[2][100010];
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> ab[0][i] >> ab[1][i];
  }
  for (int i = n - 1; i >= 0; --i) {
    ab[0][i] += ans;
    if (ab[0][i] % ab[1][i] == 0)
      continue;
    ans += ab[1][i] - (ab[0][i]) % ab[1][i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];
  long ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    while ((A[i] + ans) % B[i] != 0)
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];
  long ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    long mod = (A[i] + ans) % B[i];
    if (mod != 0)
      ans += B[i] - mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
const int MAX = 10005;
long long int a[MAX], b[MAX];
int n;
int main() {
  long long int inc = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i] >> b[i];
  }
  for (int i = n; i > 0; --i) {
    a[i] += inc;
    long long int c = a[i] % b[i];
    if (c != 0) {
      inc += (b[i] - c);
    }
  }
  cout << inc << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
const int MAX = 100005;
long long int a[MAX], b[MAX];
int n;
int main() {
  long long int inc = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i] >> b[i];
  }
  for (int i = n; i > 0; --i) {
    a[i] += inc;
    long long int c = a[i] % b[i];
    if (c != 0) {
      inc += (b[i] - c);
    }
  }
  cout << inc << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N, S = 0;
  cin >> N;
  long long int A[N], B[N];
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];
  for (int i = N - 1; i >= 0; i++)
    S += (B[i] - (A[i] + S) % B[i]) % B[i];
  cout << S << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N, S = 0;
  cin >> N;
  long long int A[N], B[N];
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];
  for (int i = N - 1; i >= 0; i--)
    S += (B[i] - (A[i] + S) % B[i]) % B[i];
  cout << S << endl;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int_least32_t n;
  cin >> n;

  vector<int_least32_t> a{n}, b{n};
  for (auto i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
  }

  int_fast32_t count{0}, temp;
  for (auto i = n - 1; i >= 0; --i) {
    temp = (count + a[i]) % b[i];
    if (temp == 0) {
      continue;
    }
    count += b[i] - temp;
  }

  cout << count << endl;
}


Fixed submission:
#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int_least32_t n;
  cin >> n;

  vector<int_least32_t> a(n), b(n);
  for (auto i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
  }

  int_fast32_t count{0}, temp;
  for (auto i = n - 1; i >= 0; --i) {
    temp = (count + a[i]) % b[i];
    if (temp == 0) {
      continue;
    }
    count += b[i] - temp;
  }

  cout << count << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int N;
int A[110000], B[110000];
signed main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];
  int ans = 0;
  for (int i = N - 1; i >= 0; i++)
    ans += (A[i] + ans) % B[i];
  cout << ans << endl;
}


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

#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int N;
int A[110000], B[110000];
signed main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];
  int ans = 0;
  for (int i = N - 1; i >= 0; i--)
    ans += (B[i] - (A[i] + ans) % B[i]) % B[i];
  cout << ans << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, n) for (long long int(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (long long int i = (a); i < (b); i++)
#define rrrep(i, a, b) for (long long int i = (a); i >= (b); i--)
#define P pair<int, int>
typedef long long ll;

using namespace std;

const int INF = 1e9, MOD = 1e9 + 7;
const ll LINF = 1e18;

// while(x!=0){
// sum+=x%10;
// x/=10;
//}
// 各桁の和

// s.find(w[i])==string::npos
// findの使い方

long long int n, cnt = 0, ans = 0, a[100000], b[100000], c, cmp[100000],
                 data[100000], m, h, x, y;

string s, w;

int main(void) {
  cin >> n;
  rep(i, n) {
    cin >> a[i] >> b[i];
    cmp[i] = a[i];
  }
  rrrep(i, n - 1, 0) {
    if (a[i] % b[i] != 0) {
      a[i] = a[i] + (b[i] - (a[i] % b[i]));
      ans += a[i] - cmp[i];
      rrrep(j, i - 1, 0) {
        a[j] += a[i] - cmp[i];
        cmp[j] += a[i] - cmp[i];
      }
    } else {
      continue;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (long long int(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (long long int i = (a); i < (b); i++)
#define rrrep(i, a, b) for (long long int i = (a); i >= (b); i--)
#define P pair<int, int>
typedef long long ll;

using namespace std;

const int INF = 1e9, MOD = 1e9 + 7;
const ll LINF = 1e18;

// while(x!=0){
// sum+=x%10;
// x/=10;
//}
// 各桁の和

// s.find(w[i])==string::npos
// findの使い方

long long int n, cnt = 0, ans = 0, a[100000], b[100000], c, cmp[100000],
                 data[100000], m, h, x, y;

string s, w;

int main(void) {
  cin >> n;
  rep(i, n) { cin >> a[i] >> b[i]; }
  rrrep(i, n - 1, 0) { ans += (b[i] - ((a[i] + ans) % b[i])) % b[i]; }
  cout << ans << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
const double pi = 3.14159265358979323846;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n;
  cin >> n;
  ll a[n], b[n], c[n];

  rep(i, n) {
    cin >> a[i] >> b[i];
    c[i] = -(a[i] % b[i]);
    if (a[i] % b[i] != 0)
      c[i] += b[i];
  }
  /*
          rep(i,n) cout<<c[i]<<" ";
          cout<<"\n";
  */
  for (int i = n - 1; i > 0; i--) {
    while (c[i - 1] < c[i])
      c[i - 1] += b[i - 1];
  }
  /*
          rep(i,n) cout<<c[i]<<" ";
          cout<<"\n";
  */
  cout << c[0] << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
const double pi = 3.14159265358979323846;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n;
  cin >> n;
  ll a[n], b[n], c[n];

  rep(i, n) {
    cin >> a[i] >> b[i];
    c[i] = -(a[i] % b[i]);
    if (a[i] % b[i] != 0)
      c[i] += b[i];
  }
  /*
          rep(i,n) cout<<c[i]<<" ";
          cout<<"\n";
  */
  for (int i = n - 1; i > 0; i--) {
    if (c[i - 1] < c[i]) {
      ll cnt = (c[i] - c[i - 1]) / b[i - 1];
      if ((c[i] - c[i - 1]) % b[i - 1] != 0)
        cnt++;
      c[i - 1] += b[i - 1] * cnt;
    }
  }
  /*
          rep(i,n) cout<<c[i]<<" ";
          cout<<"\n";
  */
  cout << c[0] << "\n";
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = n; i < (int)(m); i++)
#define rrep(i, n, m) for (int i = ((int)(n)-1); i >= m; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  rrep(i, n, 0) cin >> a[i] >> b[i];
  ll ans = 0;
  rep(i, 0, n) {
    if (a[i] % b[i] != 0)
      ans += b[i] - a[i] % b[i];
    a[i + 1] += ans;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = n; i < (int)(m); i++)
#define rrep(i, n, m) for (int i = ((int)(n)-1); i >= m; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  rrep(i, n, 0) cin >> a[i] >> b[i];
  ll ans = 0;
  rep(i, 0, n) {
    if (a[i] % b[i] != 0)
      ans += b[i] - a[i] % b[i];
    if (i + 1 < n)
      a[i + 1] += ans;
  }
  cout << ans << endl;
}
Problem p03821 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int, int>, int>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

// 最小公倍数
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  cout << fixed << setprecision(15);
  ll N;
  cin >> N;

  vector<ll> A(N);
  vector<ll> B(N);

  REP(i, N) { cin >> A[i] >> B[i]; }

  ll count = 0;
  ll button = 0;
  for (ll i = N - 1; i >= 0; i--) {
    count = button + A[i];
    // cout << "count:" << count << " A[i]:" << A[i] <<  " B[i]:" << B[i] <<
    // endl;

    while (count % B[i] != 0) {
      button++;
      count++;
    }
    // cout << button << endl;
  }
  cout << button << endl;
  return 0;
}

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

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int, int>, int>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

// 最大公約数
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

// 最小公倍数
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  cout << fixed << setprecision(15);
  ll N;
  cin >> N;

  vector<ll> A(N);
  vector<ll> B(N);

  REP(i, N) { cin >> A[i] >> B[i]; }

  ll count = 0;
  ll button = 0;
  for (ll i = N - 1; i >= 0; i--) {
    count = button + A[i];
    // cout << "count:" << count << " A[i]:" << A[i] <<  " B[i]:" << B[i] <<
    // endl;

    if (count % B[i] != 0) {
      button += B[i] - (count % B[i]);
    }
    // cout << button << endl;
  }
  cout << button << endl;
  return 0;
}
Problem p03821 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main(void) {
  long long int i, N, A[10000], B[10000], ans = 0;
  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  for (i = N - 1; i >= 0; i--) {
    if ((A[i] + ans) % B[i] != 0) {
      ans += B[i] - (A[i] + ans) % B[i];
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main(void) {
  long long int i, N, A[100000], B[100000], ans = 0;
  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  for (i = N - 1; i >= 0; i--) {
    if ((A[i] + ans) % B[i] != 0) {
      ans += B[i] - (A[i] + ans) % B[i];
    }
  }
  cout << ans << endl;

  return 0;
}