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

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    ll tmp = n / i - 1;
    if (n / tmp == i && n % tmp == i)
      ans += tmp;
  }
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"

#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  for (ll i = 1; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    ll tmp = n / i - 1;
    if (n / tmp == i && n % tmp == i)
      ans += tmp;
  }
  cout << ans << endl;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  ll res = 0;
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      ll j = 0;
      j = n / i;
      if (n / (i - 1) == n % (i - 1))
        res += i - 1;
      if (n / (j - 1) == n % (j - 1))
        res += j - 1;
    }
  }
  if (n / (n - 1) == n % (n - 1))
    res += n - 1;
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  ll res = 0;
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      ll j = 0;
      j = n / i;
      if (n / (i - 1) == n % (i - 1))
        res += i - 1;
      if (n / (j - 1) == n % (j - 1))
        res += j - 1;
    }
  }
  if (n > 1)
    if (n / (n - 1) == n % (n - 1))
      res += n - 1;
  cout << res << endl;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;

int main() {
  ll n, ans = 0;
  cin >> n;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ll t = n / i - 1;
      if (n / t == n % t) {
        ans += t;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long int ll;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;

int main() {
  ll n, ans = 0;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ll t = n / i - 1;
      if (n / t == n % t) {
        ans += t;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug: " << x << endl;
#define bug2(x, y) cout << "zdongdebug: " << x << " " << y << endl;
using namespace std;
typedef long long ll;
const int maxn = 100005;

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
#endif
  ll x;
  cin >> x;
  ll ret = 0;
  for (int i = 1; (i * (ll)i <= x); i++) {
    if (x % i == 0) {
      ll m = (x / i - 1);
      if (x % m == x / m)
        ret += x / i - 1;
      //  cout<<x/i-1<<endl;
    }
  }
  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug: " << x << endl;
#define bug2(x, y) cout << "zdongdebug: " << x << " " << y << endl;
using namespace std;
typedef long long ll;
const int maxn = 100005;

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
#endif
  ll x;
  cin >> x;
  ll ret = 0;
  for (int i = 1; (i * (ll)i <= x); i++) {
    if (x % i == 0) {
      ll m = (x / i - 1);
      if (m >= 1 && x % m == x / m)
        ret += m;
      //  cout<<x/i-1<<endl;
    }
  }
  cout << ret << endl;
  return 0;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

long long ans, n;

int main() {
  cin >> n;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      long long a, b;
      a = n / i;
      b = i;
      if (n % (a - 1) == n / a)
        ans += a - 1;
      if (b != a && n % (b - 1) == n / b)
        ans += b - 1;
    }
  }
  if (n / (n - 1) == 1)
    ans += n - 1;
  cout << ans;
}

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

long long ans, n;

int main() {
  cin >> n;
  if (n == 1)
    return cout << 0, 0;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      long long a, b;
      a = n / i;
      b = i;
      if (n % (a - 1) == n / a)
        ans += a - 1;
      if (b != a && n % (b - 1) == n / b)
        ans += b - 1;
    }
  }
  if (n / (n - 1) == 1)
    ans += n - 1;
  cout << ans;
}
Problem p03050 (C++)

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

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

Int N;
Int sum;

int main(void) {
  cin >> N;

  for (Int p = 1; p <= sqrt(N); p++) {
    Int m = N / p - 1;
    if (N / m == N % m)
      sum += m;
  }

  cout << sum << endl;
}


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

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

Int N;
Int sum;

int main(void) {
  cin >> N;

  for (Int p = 1; p <= sqrt(N); p++) {
    Int m = (N / p) - 1;
    if (m <= 0)
      continue;
    if (N / m == N % m)
      sum += m;
  }

  cout << sum << endl;
}
Problem p03050 (C++)

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

using namespace std;

int main() {
  long long N;
  long long result = 0;
  cin >> N;
  long long n_sqrt = (long long)floor(sqrt((double)N));
  for (long long p = 1; p <= n_sqrt; p++) {
    if (N % p == 0) {
      long long l = N / p;
      if (p == l) {
        if (N % (l - 1) == p) {
          result += l - 1;
        }
      } else if (p > l) {
        break;
      } else {
        if (N % (l - 1) == p) {
          result += l - 1;
        }
      }
    }
  }
  cout << result << endl;
  return (0);
}


Fixed submission:
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  long long N;
  long long result = 0;
  cin >> N;
  long long n_sqrt = (long long)floor(sqrt((double)N));
  for (long long p = 1; p <= n_sqrt; p++) {
    if (N % p == 0) {
      long long l = N / p;
      if (l == 1) {
        continue;
      }
      if (p == l) {
        if (N % (l - 1) == p) {
          result += l - 1;
        }
      } else if (p > l) {
        break;
      } else {
        if (N % (l - 1) == p) {
          result += l - 1;
        }
      }
    }
  }
  cout << result << endl;
  return (0);
}
Problem p03050 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long

const int N = 2e5 + 5;
int p = 1e9 + 7;

int32_t main() {
  IOS;
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      int k = n / i - 1;
      if (n % k == n / k)
        ans += k;
    }
  }
  cout << ans;
  return 0;
}


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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long

const int N = 2e5 + 5;
int p = 1e9 + 7;

int32_t main() {
  IOS;
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      int k = n / i - 1;
      if (k)
        if (n % k == n / k)
          ans += k;
    }
  }
  cout << ans;
  return 0;
}
Problem p03050 (C++)

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

long long n, sm;
vector<long long> d;

bool fav(long long x) { return (n % x == (n / x)); }

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

  cin >> n;
  long long i;
  for (i = 1; 1LL * i * i < n; i++) {
    if (n % i == 0) {
      if (i > 1)
        if (fav(i - 1))
          sm += (i - 1);
      if (fav(1LL * n / i - 1))
        sm += (1LL * n / i - 1);
    }
  }
  if (i * i == n && fav(i - 1))
    sm += (i - 1);
  cout << sm;
}


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

long long n, sm;
vector<long long> d;

bool fav(long long x) { return (n % x == (n / x)); }

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

  cin >> n;
  if (n == 1)
    return cout << 0, 0;
  long long i;
  for (i = 1; 1LL * i * i < n; i++) {
    if (n % i == 0) {
      if (i > 1)
        if (fav(i - 1))
          sm += (i - 1);
      if (fav(1LL * n / i - 1))
        sm += (1LL * n / i - 1);
    }
  }
  if (i * i == n && fav(i - 1))
    sm += (i - 1);
  cout << sm;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  long n;
  cin >> n;
  long res = 0;
  for (long i = 1; i * i <= n; ++i) {
    if ((n - i) % i > 0)
      continue;
    auto m = (n - i) / i;
    if (n / m == n % m)
      res += m;
  }
  cout << res << endl;
}


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

int main() {
  long n;
  cin >> n;
  long res = 0;
  for (long i = 1; i * i <= n; ++i) {
    if ((n - i) % i > 0)
      continue;
    auto m = (n - i) / i;
    if (m > 0 and n / m == n % m)
      res += m;
  }
  cout << res << endl;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define ALL(v) v.begin(), v.end()
#define pb push_back
#define mp make_pair
#define F first
#define S second

#define UNIQUE(v)                                                              \
  do {                                                                         \
    sort(v.begin(), v.end());                                                  \
    v.erase(unique(v.begin(), v.end()), v.end());                              \
  } while (0)

#define y0 y3487465
#define y1 y8687969

#define m0(x) memset(x, 0, sizeof(x))
#define m1(x) memset(x, 63, sizeof(x))

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

double EPS = 1e-9;
int INFi = 1000000005;
long long INFll = 1000000000000000005ll;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
ll MOD = 1000000007;

ll N;

int main() {
  cin >> N;

  ll ans = 0;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      ll m = N / i - 1;
      if (N / m == i) {
        ans += m;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define ALL(v) v.begin(), v.end()
#define pb push_back
#define mp make_pair
#define F first
#define S second

#define UNIQUE(v)                                                              \
  do {                                                                         \
    sort(v.begin(), v.end());                                                  \
    v.erase(unique(v.begin(), v.end()), v.end());                              \
  } while (0)

#define y0 y3487465
#define y1 y8687969

#define m0(x) memset(x, 0, sizeof(x))
#define m1(x) memset(x, 63, sizeof(x))

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

double EPS = 1e-9;
int INFi = 1000000005;
long long INFll = 1000000000000000005ll;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
ll MOD = 1000000007;

ll N;

int main() {
  cin >> N;

  ll ans = 0;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      ll m = N / i - 1;
      if (m != 0 && N / m == i) {
        ans += m;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;

int main(void) {
  ll n, m, ans = 0;
  cin >> n;
  for (ll k = 1; k < sqrt(n) + 1; k++) {
    if (n % k == 0) {
      m = n / k - 1;
      if (n % m == k)
        ans += m;
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main(void) {
  ll n, m, ans = 0;
  cin >> n;
  for (ll k = 1; k < sqrt(n) + 1; k++) {
    if (n % k == 0) {
      m = n / k - 1;
      if (m > 0 && n % m == k)
        ans += m;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
using Graph = vector<vector<int>>;

signed main() {

  int N;
  cin >> N;

  int ans = 0;
  for (int i = 1; i <= sqrt(N); i++) {
    if (N % i == 0) {
      int A = N / i - 1;
      int B = N / A;
      if (B == N % A)
        ans += A;
      if (i == 1 || i == N / i)
        continue;
      A = i - 1;
      B = N / A;
      if (B == N % A)
        ans += A;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
using Graph = vector<vector<int>>;

signed main() {

  int N;
  cin >> N;

  int ans = 0;
  for (int i = 1; i <= sqrt(N); i++) {
    if (N % i == 0) {
      int A = N / i - 1;
      if (A == 0)
        continue;
      int B = N / A;
      if (B == N % A)
        ans += A;
      if (i == 1 || i == N / i)
        continue;
      A = i - 1;
      B = N / A;
      if (B == N % A)
        ans += A;
    }
  }
  cout << ans << endl;
}
Problem p03050 (C++)

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

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define el endl
#define INF 1e8
typedef long long ll;
typedef long double ld;

int main() {
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll k = 1; k * k <= n; k++) {
    if (n % k == 0) {
      ll m = (n / k) - 1LL;
      if (n / m == n % m)
        ans += m;
      if (k > 1) {
        m = k - 1;
        if (n / m == n % m)
          ans += m;
      }
    }
  }
  cout << ans << endl;
}


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

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define el endl
#define INF 1e8
typedef long long ll;
typedef long double ld;

int main() {
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll k = 1; k * k < n; k++) {
    if (n % k == 0) {
      ll m = (n / k) - 1LL;
      if (n / m == n % m)
        ans += m;
      if (k > 1) {
        m = k - 1;
        if (n / m == n % m)
          ans += m;
      }
    }
  }
  cout << ans << endl;
}
Problem p03050 (C++)

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

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(0);
  ll N;
  cin >> N;
  ll odp = 0;
  for (ll d1 = 1; d1 * d1 <= N; ++d1) {
    if (N % d1 == 0) {
      ll d2 = N / d1 - 1;
      if (N / d2 == N % d2)
        odp += d2;
      if (d1 * d1 != N)
        if (d1 != 1 && N / (d1 - 1) == N % (d1 - 1))
          odp += d1 - 1;
    }
  }
  cout << odp;
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(0);
  ll N;
  cin >> N;
  ll odp = 0;
  for (ll d1 = 1; d1 * d1 <= N; ++d1) {
    if (N % d1 == 0) {
      ll d2 = N / d1 - 1;
      if (d2 != 0 && N / d2 == N % d2)
        odp += d2;
      if (d1 * d1 != N)
        if (d1 != 1 && N / (d1 - 1) == N % (d1 - 1))
          odp += d1 - 1;
    }
  }
  cout << odp;
  return 0;
}
Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long ones[2000001];
int now;
void it(long long x) {
  for (long long i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      if (i * i != x)
        ones[now++] = i, ones[now++] = x / i;
      else
        ones[now++] = i;
    }
  }
}
long long cnt;
int main() {
  long long n;
  cin >> n;
  it(n);
  ones[now++] = n;
  for (int i = 0; i < now; i++)
    if (n % (ones[i] - 1) == n / (ones[i] - 1))
      cnt += ones[i] - 1;
  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long ones[2000001];
int now;
void it(long long x) {
  for (long long i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      if (i * i != x)
        ones[now++] = i, ones[now++] = x / i;
      else
        ones[now++] = i;
    }
  }
}
long long cnt;
int main() {
  long long n;
  cin >> n;
  it(n);
  ones[now++] = n;
  for (int i = 0; i < now; i++)
    if (ones[i] - 1 != 0 && n % (ones[i] - 1) == n / (ones[i] - 1))
      cnt += ones[i] - 1;
  cout << cnt << endl;
}
Problem p03050 (Python)

Buggy submission (status: Runtime Error):
import math

N = int(input())
ans = 0
for i in range(1, math.ceil(math.sqrt(N)) + 1):
    if N % i:
        continue
    n = N // i - 1
    if N % n == N // n:
        ans += n
print(ans)


Fixed submission:
import math

N = int(input())
ans = 0
for i in range(1, math.ceil(math.sqrt(N)) + 1):
    if N % i:
        continue
    n = N // i - 1
    if n and N % n == N // n:
        ans += n
print(ans)
Problem p03051 (C++)

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

using namespace std;

// #define FILE_IO

const int VMAX = (1 << 20) + 5;
const int NMAX = 5e2 + 5;
const int mod = 1e9 + 7;

int N;
int v[NMAX], sum[NMAX], dp[NMAX], cnt0[NMAX];
int sumdp[VMAX], sumdpcnt[VMAX];
vector<int> pos[VMAX];

int power(int x, int y) {
  if (x == 0)
    return 0;
  if (y <= 0)
    return 1;
  int ans = power((1LL * x * x) % mod, y >> 1);
  if (y & 1)
    ans = (1LL * ans * x) % mod;
  return ans;
}

int main() {
#ifdef FILE_IO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> v[i];
    sum[i] = sum[i - 1] ^ v[i];
  }

  int sumall = sum[N];
  if (sumall != 0) {
    sumdp[0] = 1;
    dp[0] = 1;
    for (int i = 1; i <= N; i++) {
      int needval = sum[i] ^ sumall;
      dp[i] = sumdp[needval];
      sumdp[sum[i]] = (sumdp[sum[i]] + dp[i]) % mod;
    }
    printf("%d\n", dp[N]);
    exit(0);
  }

  cnt0[0] = 1;
  for (int i = 1; i <= N; i++) {
    cnt0[i] = cnt0[i - 1] + (sum[i] == 0);
    pos[sum[i]].push_back(i);
  }

  for (int i = 1; i <= N; i++) {
    if (sum[i] == 0)
      continue;
    int s = sum[i];
    dp[i] = ((1LL * cnt0[i] * sumdp[s]) % mod - sumdpcnt[s] + mod + 1) % mod;
    sumdp[s] = (sumdp[s] + dp[i]) % mod;
    sumdpcnt[s] = (sumdpcnt[s] + (1LL * dp[i] * cnt0[i]) % mod) % mod;
  }

  int ans = power(2, (int)pos[0].size() - 1);
  for (int i = 1; i <= N; i++)
    if (sum[i] != 0)
      ans = (ans + dp[i]) % mod;
  printf("%d\n", ans);

  return 0;
}


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

using namespace std;

// #define FILE_IO

const int VMAX = (1 << 20) + 5;
const int NMAX = 5e5 + 5;
const int mod = 1e9 + 7;

int N;
int v[NMAX], sum[NMAX], dp[NMAX], cnt0[NMAX];
int sumdp[VMAX], sumdpcnt[VMAX];
vector<int> pos[VMAX];

int power(int x, int y) {
  if (x == 0)
    return 0;
  if (y <= 0)
    return 1;
  int ans = power((1LL * x * x) % mod, y >> 1);
  if (y & 1)
    ans = (1LL * ans * x) % mod;
  return ans;
}

int main() {
#ifdef FILE_IO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> v[i];
    sum[i] = sum[i - 1] ^ v[i];
  }

  int sumall = sum[N];
  if (sumall != 0) {
    sumdp[0] = 1;
    dp[0] = 1;
    for (int i = 1; i <= N; i++) {
      int needval = sum[i] ^ sumall;
      dp[i] = sumdp[needval];
      sumdp[sum[i]] = (sumdp[sum[i]] + dp[i]) % mod;
    }
    printf("%d\n", dp[N]);
    exit(0);
  }

  cnt0[0] = 1;
  for (int i = 1; i <= N; i++) {
    cnt0[i] = cnt0[i - 1] + (sum[i] == 0);
    pos[sum[i]].push_back(i);
  }

  for (int i = 1; i <= N; i++) {
    if (sum[i] == 0)
      continue;
    int s = sum[i];
    dp[i] = ((1LL * cnt0[i] * sumdp[s]) % mod - sumdpcnt[s] + mod + 1) % mod;
    sumdp[s] = (sumdp[s] + dp[i]) % mod;
    sumdpcnt[s] = (sumdpcnt[s] + (1LL * dp[i] * cnt0[i]) % mod) % mod;
  }

  int ans = power(2, (int)pos[0].size() - 1);
  for (int i = 1; i <= N; i++)
    if (sum[i] != 0)
      ans = (ans + dp[i]) % mod;
  printf("%d\n", ans);

  return 0;
}
Problem p03051 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

int a[500005];
ll dp1[2100000];
ll dp2[2100000];
ll s[500005];
ll last[500005];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll x = 0;
  for (int i = 0; i < 2100000; i++)
    dp2[i] = 1;
  for (int i = 0; i < n; i++) {
    x ^= a[i];
    s[i + 1] = s[i];
    if (x) {
      dp2[x] = (dp2[x] + dp1[x] * (s[i + 1] - s[last[x]]) % MOD) % MOD;
      dp1[x] = (dp1[x] + dp2[x]) % MOD;
      last[x] = i + 1;
    } else {
      s[i + 1]++;
    }
  }
  ll ans = 0;
  if (x == 0) {
    for (int i = 0; i < 2100000; i++)
      ans = (ans + dp1[i]) % MOD;
    ll c = 0;
    for (int i = 0; i < n; i++) {
      x ^= a[i];
      if (x == 0)
        c++;
    }
    ll k = 1;
    for (int i = 0; i < c - 1; i++)
      k = k * 2 % MOD;
    ans = (ans + k) % MOD;
  } else {
    ans = dp2[x];
  }
  cout << ans << endl;
}


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

const ll MOD = 1000000007;

int a[500005];
ll dp1[2100000];
ll dp2[2100000];
ll s[500005];
ll last[2100000];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll x = 0;
  for (int i = 0; i < 2100000; i++)
    dp2[i] = 1;
  for (int i = 0; i < n; i++) {
    x ^= a[i];
    s[i + 1] = s[i];
    if (x) {
      dp2[x] = (dp2[x] + dp1[x] * (s[i + 1] - s[last[x]]) % MOD) % MOD;
      dp1[x] = (dp1[x] + dp2[x]) % MOD;
      last[x] = i + 1;
    } else {
      s[i + 1]++;
    }
  }
  ll ans = 0;
  if (x == 0) {
    for (int i = 0; i < 2100000; i++)
      ans = (ans + dp1[i]) % MOD;
    ll c = 0;
    for (int i = 0; i < n; i++) {
      x ^= a[i];
      if (x == 0)
        c++;
    }
    ll k = 1;
    for (int i = 0; i < c - 1; i++)
      k = k * 2 % MOD;
    ans = (ans + k) % MOD;
  } else {
    ans = dp2[x];
  }
  cout << ans << endl;
}
Problem p03051 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
int mul(int x, int y) { return (x * 1LL * y) % mod; }

int n;
int arr[500005];
int sum[500005];

pair<int, int> tree[500005];
int doneUpdates[500005];
int updates;

void propagate(int ind) {
  int toDo = updates - doneUpdates[ind];
  doneUpdates[ind] = updates;
  tree[ind].first = add(tree[ind].first, mul(tree[ind].second, toDo));
}

void update(int ind, int val) {
  propagate(ind);
  tree[ind].second = add(tree[ind].second, val);
}

void apply() { updates++; }

int get(int ind) {
  propagate(ind);
  return tree[ind].first;
}

int main() {
  ios::sync_with_stdio(false);

  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", arr + i), sum[i] = arr[i];
  for (int i = 1; i < n; i++)
    sum[i] ^= sum[i - 1];

  int ans = 0;
  int z = 0;
  for (int i = n - 1; i >= 0; i--) {
    int val = sum[i];
    if (val == 0) {
      apply();
      if (sum[n - 1] == 0) {
        int tmp = z;
        if (i == n - 1)
          tmp = 1;
        z = add(tmp, z);
        ans = add(ans, tmp);
      }
    } else {
      int tmp = get(val);
      if (sum[n - 1] == 0)
        tmp = add(tmp, 1);
      else if (i == n - 1)
        tmp = 1;
      ans = add(ans, tmp);
      update(val, tmp);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
int mul(int x, int y) { return (x * 1LL * y) % mod; }

int n;
int arr[500005];
int sum[500005];

pair<int, int> tree[1 << 21];
int doneUpdates[1 << 21];
int updates;

void propagate(int ind) {
  int toDo = updates - doneUpdates[ind];
  doneUpdates[ind] = updates;
  tree[ind].first = add(tree[ind].first, mul(tree[ind].second, toDo));
}

void update(int ind, int val) {
  propagate(ind);
  tree[ind].second = add(tree[ind].second, val);
}

void apply() { updates++; }

int get(int ind) {
  propagate(ind);
  return tree[ind].first;
}

int main() {
  ios::sync_with_stdio(false);

  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", arr + i), sum[i] = arr[i];
  for (int i = 1; i < n; i++)
    sum[i] ^= sum[i - 1];

  int ans = 0;
  int z = 0;
  for (int i = n - 1; i >= 0; i--) {
    int val = sum[i];
    if (val == 0) {
      apply();
      if (sum[n - 1] == 0) {
        int tmp = z;
        if (i == n - 1)
          tmp = 1;
        z = add(tmp, z);
        ans = add(ans, tmp);
      }
    } else {
      int tmp = get(val);
      if (sum[n - 1] == 0)
        tmp = add(tmp, 1);
      else if (i == n - 1)
        tmp = 1;
      ans = add(ans, tmp);
      update(val, tmp);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <climits>
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

class Point {
public:
  int x;
  int y;
  int v;
  Point(int _x, int _y, int _v) {
    x = _x;
    y = _y;
    v = _v;
  }
  Point(const Point &p) {
    x = p.x;
    y = p.y;
    v = p.v;
  }
};

int main() {
  int h, w;
  scanf("%d %d\n", &h, &w);
  vector<vector<int>> board(h, vector<int>(w, INT_MAX));
  queue<Point> q;
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < h; y++) {
      char ch = getchar();
      assert(ch == '#' || ch == '.');
      if (ch == '#') {
        q.push(Point(x, y, 0));
      }
    }
    getchar(); // discard LF
  }
  int res = 0;
  while (!q.empty()) {
    Point p(q.front());
    q.pop();
    if (p.x < 0 || h <= p.x || p.y < 0 || w <= p.y) {
      continue;
    }
    if (board.at(p.x).at(p.y) <= p.v) {
      continue;
    }
    board.at(p.x).at(p.y) = p.v;
    res = max(res, p.v);
    q.push(Point(p.x - 1, p.y, p.v + 1));
    q.push(Point(p.x + 1, p.y, p.v + 1));
    q.push(Point(p.x, p.y - 1, p.v + 1));
    q.push(Point(p.x, p.y + 1, p.v + 1));
  }
  printf("%d\n", res);
  return 0;
}


Fixed submission:
#include <cassert>
#include <climits>
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

class Point {
public:
  int x;
  int y;
  int v;
  Point(int _x, int _y, int _v) {
    x = _x;
    y = _y;
    v = _v;
  }
  Point(const Point &p) {
    x = p.x;
    y = p.y;
    v = p.v;
  }
};

int main() {
  int h, w;
  scanf("%d %d\n", &h, &w);
  vector<vector<int>> board(h, vector<int>(w, INT_MAX));
  queue<Point> q;
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      char ch = getchar();
      assert(ch == '#' || ch == '.');
      if (ch == '#') {
        q.push(Point(x, y, 0));
      }
    }
    getchar(); // discard LF
  }
  int res = 0;
  while (!q.empty()) {
    Point p(q.front());
    q.pop();
    if (p.x < 0 || h <= p.x || p.y < 0 || w <= p.y) {
      continue;
    }
    if (board.at(p.x).at(p.y) <= p.v) {
      continue;
    }
    board.at(p.x).at(p.y) = p.v;
    res = max(res, p.v);
    q.push(Point(p.x - 1, p.y, p.v + 1));
    q.push(Point(p.x + 1, p.y, p.v + 1));
    q.push(Point(p.x, p.y - 1, p.v + 1));
    q.push(Point(p.x, p.y + 1, p.v + 1));
  }
  printf("%d\n", res);
  return 0;
}
Problem p03053 (C++)

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> data;
  queue<pair<int, int>> que;

  vector<pair<int, int>> diff;
  diff.emplace_back(0, 1);
  diff.emplace_back(1, 0);
  diff.emplace_back(0, -1);
  diff.emplace_back(-1, 0);

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char temp;
      cin >> temp;
      if (temp == '#') {
        // 黒マスは初期値: 0
        data[i][j] = 0;
        que.emplace(i, j);
      } else {
        // 白マスは-1
        data[i][j] = -1;
      }
    }
  }

  while (!que.empty()) {
    auto cur = que.front();
    que.pop();

    // 現在のカーソル位置の上下左右を新たにカーソル位置とする
    for (auto it = diff.begin(); it != diff.end(); it++) {
      // 境界値チェック
      int nx = cur.first + it->first;
      int ny = cur.second + it->second;
      if (nx < 0 || nx >= H || ny < 0 || ny >= W)
        continue;

      if (data[nx][ny] == -1) {
        data[nx][ny] = data[cur.first][cur.second] + 1;
        que.emplace(nx, ny);
      }
    }
  }

  int max = 0;
  for (auto it_H : data) {
    for (auto it : it_H) {
      if (max < it) {
        max = it;
      }
    }
  }

  cout << max << endl;

  return 0;
}

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> data(H, vector<int>(W, -1));

  queue<pair<int, int>> que;

  vector<pair<int, int>> diff;
  diff.emplace_back(0, 1);
  diff.emplace_back(1, 0);
  diff.emplace_back(0, -1);
  diff.emplace_back(-1, 0);

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char temp;
      cin >> temp;
      if (temp == '#') {
        // 黒マスは初期値: 0
        data[i][j] = 0;
        que.emplace(i, j);
      } else {
        // 白マスは-1
        data[i][j] = -1;
      }
    }
  }

  while (!que.empty()) {
    auto cur = que.front();
    que.pop();

    // 現在のカーソル位置の上下左右を新たにカーソル位置とする
    for (auto it = diff.begin(); it != diff.end(); it++) {
      // 境界値チェック
      int nx = cur.first + it->first;
      int ny = cur.second + it->second;
      if (nx < 0 || nx >= H || ny < 0 || ny >= W)
        continue;

      if (data[nx][ny] == -1) {
        data[nx][ny] = data[cur.first][cur.second] + 1;
        que.emplace(nx, ny);
      }
    }
  }

  int max = 0;
  for (auto it_H : data) {
    for (auto it : it_H) {
      if (max < it) {
        max = it;
      }
    }
  }

  cout << max << endl;

  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

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

using namespace std;

typedef long long int ll;

const ll INF = (ll)1e18;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;

  vector<vector<char>> a(h);
  vector<vector<int>> d(h, vector<int>(w, -1));
  queue<pair<int, int>> q;

  REP(i, h) {
    REP(j, w) {
      char c;
      cin >> c;
      a[i].push_back(c);
      if (c == '#') {
        q.push(make_pair(i, j));
        d[i][j] = 0;
      }
    }
  }
  int ans = 0;
  while (!q.empty()) {
    pair<int, int> pos = q.front();
    q.pop();

    REP(i, 4) {
      int x = pos.first + dx[i];
      int y = pos.second + dy[i];
      if (x < 0 || x >= w || y < 0 || y >= h)
        continue;

      if (a[x][y] == '#')
        continue;

      a[x][y] = '#';
      d[x][y] = d[pos.first][pos.second] + 1;
      ans = max(ans, d[x][y]);
      q.push(make_pair(x, y));
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

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

using namespace std;

typedef long long int ll;

const ll INF = (ll)1e18;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;

  vector<vector<char>> a(h);
  vector<vector<int>> d(h, vector<int>(w, -1));
  queue<pair<int, int>> q;

  REP(i, h) {
    REP(j, w) {
      char c;
      cin >> c;
      a[i].push_back(c);
      if (c == '#') {
        q.push(make_pair(i, j));
        d[i][j] = 0;
      }
    }
  }
  int ans = 0;
  while (!q.empty()) {
    pair<int, int> pos = q.front();
    q.pop();

    REP(i, 4) {
      int x = pos.first + dx[i];
      int y = pos.second + dy[i];
      if (x < 0 || x >= h || y < 0 || y >= w)
        continue;

      if (a[x][y] == '#')
        continue;

      a[x][y] = '#';
      d[x][y] = d[pos.first][pos.second] + 1;
      ans = max(ans, d[x][y]);
      q.push(make_pair(x, y));
    }
  }

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

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

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> a(h);
  for (auto &e : a)
    cin >> e;

  vector<vector<int>> d(h, vector<int>(w, 1e9));
  queue<pair<int, int>> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == '#') {
        q.push({i, j});
        d[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int y = p.first + dy[i], x = p.second + dx[i];
      if (!(0 <= y && y < h && 0 <= x && x < w))
        continue;
      if (d[p.first][p.second] + 1 > d[y][x])
        continue;
      d[y][x] = d[p.first][p.second] + 1;
      q.push({y, x});
    }
  }

  int r = 0;
  for (const auto &row : d) {
    for (auto e : row) {
      r = max(r, e);
    }
  }
  cout << r << endl;
}


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

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> a(h);
  for (auto &e : a)
    cin >> e;

  vector<vector<int>> d(h, vector<int>(w, 1e9));
  queue<pair<int, int>> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == '#') {
        q.push({i, j});
        d[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int y = p.first + dy[i], x = p.second + dx[i];
      if (!(0 <= y && y < h && 0 <= x && x < w))
        continue;
      if (d[p.first][p.second] + 1 >= d[y][x])
        continue;
      d[y][x] = d[p.first][p.second] + 1;
      q.push({y, x});
    }
  }

  int r = 0;
  for (const auto &row : d) {
    for (auto e : row) {
      r = max(r, e);
    }
  }
  cout << r << endl;
}
Problem p03053 (C++)

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

int main() {
  int H, W;
  queue<pair<int, int>> search;
  cin >> H >> W;
  int a[1010][1010]; // 距離
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char tmp;
      cin >> tmp;
      if (tmp == '#') {
        a[i][j] = 0;
        search.push({i, j});
      } else
        a[i][j] = 10e6;
    }
  }
  int dr[] = {-1, 1, 0, 0};
  int dc[] = {0, 0, -1, 1};
  while (!search.empty()) {
    int r = search.front().first, c = search.front().second;
    search.pop();
    for (int i = 0; i < 4; i++) {
      int newr = r + dr[i], newc = c + dc[i];
      if (newr >= 0 && newr < H && newc >= 0 && newc < W &&
          a[newr][newc] > a[r][c]) {
        a[newr][newc] = a[r][c] + 1;
        search.push({newr, newc});
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      ans = max(ans, a[i][j]);
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int H, W;
  queue<pair<int, int>> search;
  cin >> H >> W;
  int a[1010][1010]; // 距離
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char tmp;
      cin >> tmp;
      if (tmp == '#') {
        a[i][j] = 0;
        search.push({i, j});
      } else
        a[i][j] = 10e6;
    }
  }
  int dr[] = {-1, 1, 0, 0};
  int dc[] = {0, 0, -1, 1};
  while (!search.empty()) {
    int r = search.front().first, c = search.front().second;
    search.pop();
    for (int i = 0; i < 4; i++) {
      int newr = r + dr[i], newc = c + dc[i];
      if (newr >= 0 && newr < H && newc >= 0 && newc < W &&
          a[newr][newc] > a[r][c] + 1) {
        a[newr][newc] = a[r][c] + 1;
        search.push({newr, newc});
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      ans = max(ans, a[i][j]);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  int ans = 0;
  cin >> h >> w;
  char a[h][w];
  queue<pair<int, int>> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#') {
        q.push(pair<int, int>(i, j));
      }
    }
  }
  vector<vector<int>> distance(h, vector<int>(w, 0));
  while (q.size()) {
    pair<int, int> start = q.front();
    q.pop();
    int x = start.first;
    int y = start.second;
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      int cx = x + dx[i];
      int cy = y + dy[i];
      if (0 <= cx && cx < h && 0 <= cy && cy < w) {
        distance[cx][cy] = distance[x][y] + 1;
        q.push(pair<int, int>(cx, cy));
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      ans = max(ans, distance[i][j]);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  int ans = 0;
  cin >> h >> w;
  char a[h][w];
  queue<pair<int, int>> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#') {
        q.push(pair<int, int>(i, j));
      }
    }
  }
  vector<vector<int>> distance(h, vector<int>(w, 0));
  while (q.size()) {
    pair<int, int> start = q.front();
    q.pop();
    int x = start.first;
    int y = start.second;
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      int cx = x + dx[i];
      int cy = y + dy[i];
      if (0 <= cx && cx < h && 0 <= cy && cy < w && a[cx][cy] == '.' &&
          distance[cx][cy] == 0) {
        distance[cx][cy] = distance[x][y] + 1;
        q.push(pair<int, int>(cx, cy));
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      ans = max(ans, distance[i][j]);
    }
  }
  cout << ans << endl;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define INF 10000000
#define MOD 1000000007
typedef long long ll;
typedef pair<int, pair<int, int>> P;

int H, W, ans = 0;
char C[60][60];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
queue<P> que;

void bfs() {

  while (que.size()) {
    P p = que.front();
    que.pop();
    ans = max(ans, p.first);
    // cout<<p.second.first<<" "<<p.second.second<<endl;
    for (int i = 0; i < 4; i++) {
      int ny = p.second.first + dy[i], nx = p.second.second + dx[i];
      // cout<<ny<<" "<<nx<<" "<<"nyとnx"<<endl;
      if (0 <= nx && nx < W && ny >= 0 && ny < H && C[ny][nx] == '.') {
        // cout<<"unk"<<endl;
        C[ny][nx] = '#';
        que.push(make_pair(ans + 1, make_pair(ny, nx)));
      }
    }
  }
}

int main() {
  int count = 0;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> C[i][j];
      if (C[i][j] == '#')
        que.push(make_pair(0, make_pair(i, j)));
    }
  }

  bfs();
  // cout<<C[0][0]<<endl;
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define INF 10000000
#define MOD 1000000007
typedef long long ll;
typedef pair<int, pair<int, int>> P;

int H, W, ans = 0;
char C[1100][1100];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
queue<P> que;

void bfs() {

  while (que.size()) {
    P p = que.front();
    que.pop();
    ans = max(ans, p.first);
    // cout<<p.second.first<<" "<<p.second.second<<endl;
    for (int i = 0; i < 4; i++) {
      int ny = p.second.first + dy[i], nx = p.second.second + dx[i];
      // cout<<ny<<" "<<nx<<" "<<"nyとnx"<<endl;
      if (0 <= nx && nx < W && ny >= 0 && ny < H && C[ny][nx] == '.') {
        // cout<<"unk"<<endl;
        C[ny][nx] = '#';
        que.push(make_pair(ans + 1, make_pair(ny, nx)));
      }
    }
  }
}

int main() {
  int count = 0;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> C[i][j];
      if (C[i][j] == '#')
        que.push(make_pair(0, make_pair(i, j)));
    }
  }

  bfs();
  // cout<<C[0][0]<<endl;
  cout << ans << endl;
}
Problem p03053 (Python)

Buggy submission (status: Runtime Error):
from collections import deque


H, W = map(int, input().split())
A = [list("#" * (W + 2))]
q = deque()
A_append = A.append
q_append = q.append
for _ in range(H):
    a = input()
    A_append(list("#" + a + "#"))
A.append(list("#" * (W + 2)))
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if A[i][j] == "#":
            q_append([i, j, 0])

ans = 0

while len(q) != 0:
    p = q.popleft()
    for k in range(4):
        y = p[1] + dy[k]
        x = p[0] + dx[k]
        c = p[2]
        ans = max(ans, c)
        if A[y][x] == ".":
            A[y][x] = "#"
            q_append([y, x, c + 1])

print(ans)


Fixed submission:
from collections import deque


H, W = map(int, input().split())
A = [list("#" * (W + 2))]
q = deque()
A_append = A.append
q_append = q.append
for _ in range(H):
    a = input()
    A_append(list("#" + a + "#"))
A.append(list("#" * (W + 2)))
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if A[i][j] == "#":
            q_append([i, j, 0])

ans = 0

while len(q) != 0:
    p = q.popleft()
    for k in range(4):
        y = p[0] + dy[k]
        x = p[1] + dx[k]
        c = p[2]
        ans = max(ans, c)
        if A[y][x] == ".":
            A[y][x] = "#"
            q_append([y, x, c + 1])

print(ans)
Problem p03053 (C++)

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

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {

  int h, w;
  cin >> h >> w;
  vector<string> a(h);
  for (int i = 0; i < h; i++)
    cin >> a[i];

  // 多点スタート
  // スタートから0距離で到達できる場合には距離配列に0を格納
  vector<vector<int>> dist(h, vector<int>(w, -1));
  queue<pair<int, int>> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == '#') {
        dist[i][j] = 0;
        que.push(pair<int, int>(i, j));
      }
    }
  }

  while (!que.empty()) {
    auto cur = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = cur.first + dx[i];
      int ny = cur.second + dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (dist[nx][ny] == -1) {
        que.push(pair<int, int>(nx, ny));
        dist[nx][ny] = dist[cur.first][cur.second] + 1;
      }
    }
  }

  int res = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      res = max(res, dist[i][j]);
    }
  }

  cout << res << endl;

  return 0;
}

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

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {

  int h, w;
  cin >> h >> w;
  vector<string> a(h);
  for (int i = 0; i < h; i++)
    cin >> a[i];

  // 多点スタート
  // スタートから0距離で到達できる場合には距離配列に0を格納
  vector<vector<int>> dist(h, vector<int>(w, -1));
  queue<pair<int, int>> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == '#') {
        dist[i][j] = 0;
        que.push(pair<int, int>(i, j));
      }
    }
  }

  while (!que.empty()) {
    auto cur = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = cur.first + dx[i];
      int ny = cur.second + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (dist[nx][ny] == -1) {
        que.push(pair<int, int>(nx, ny));
        dist[nx][ny] = dist[cur.first][cur.second] + 1;
      }
    }
  }

  int res = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      res = max(res, dist[i][j]);
    }
  }

  cout << res << endl;

  return 0;
}