prithivMLmods/Coder-Stat huggingface dataset:
Problem p02550 (Python)

Buggy submission (status: Runtime Error):
import sys

sys.setrecursionlimit(10**8)

input = sys.stdin.readline


def main():
    N, X, M = [int(x) for x in input().split()]

    x = {}

    ruiseki = [0] * (M + 1)

    cnt = 1
    ans = X
    c = X
    ruiseki[1] = ans
    while cnt < N:
        next = c**2 % M
        if next in x.keys():
            y = x[next]
            # print(ruiseki)
            # print(cnt, y)
            if N == cnt:
                print(ans + next)
                return
            loop = ruiseki[cnt] - ruiseki[y - 1]
            loopcnt = cnt - y + 1
            ans += ((N - cnt) // loopcnt) * loop
            # print(ans)
            amari = (N - cnt) % loopcnt
            # print(amari, loop, loopcnt)
            if amari != 0:
                ans += ruiseki[y - 1 + amari] - ruiseki[y - 1]
                # print(ans)
            break

        ans += next
        cnt += 1
        ruiseki[cnt] = next
        ruiseki[cnt] += ruiseki[cnt - 1]
        c = next
        x[next] = cnt

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys

sys.setrecursionlimit(10**8)

input = sys.stdin.readline


def main():
    N, X, M = [int(x) for x in input().split()]

    x = {}

    ruiseki = [0] * (M + 1)

    cnt = 1
    ans = X
    c = X
    ruiseki[1] = ans
    while cnt < N:
        if c == 0:
            print(ans)
            return
        next = c**2 % M
        if next in x.keys():
            y = x[next]
            # print(ruiseki)
            # print(cnt, y)
            if N == cnt:
                print(ans + next)
                return
            loop = ruiseki[cnt] - ruiseki[y - 1]
            loopcnt = cnt - y + 1
            ans += ((N - cnt) // loopcnt) * loop
            # print(ans)
            amari = (N - cnt) % loopcnt
            # print(amari, loop, loopcnt)
            if amari != 0:
                ans += ruiseki[y - 1 + amari] - ruiseki[y - 1]
                # print(ans)
            break

        ans += next
        cnt += 1
        ruiseki[cnt] = next
        ruiseki[cnt] += ruiseki[cnt - 1]
        c = next
        x[next] = cnt

    print(ans)


if __name__ == "__main__":
    main()
Problem p02550 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<long long>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<long, long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back

vll rem(1e5 + 5, 0), vec(1e5 + 5, 0);

int main() {
  ll n, x, m, first = 0, period = 0;
  cin >> n >> x >> m;
  ll sum = 0, ans = 0;

  for (ll i = 1; i <= m - 1; i++) {
    rem[i] = x;
    if (vec[x] != 0) {
      first = vec[x];
      period = i - first;
    }
    vec[x] = i;
    x *= x;
    x %= m;
  }
  if (first != 0) {
    for (ll i = first; i <= first + period - 1; i++)
      sum += rem[i];
  } else {
    for (ll i = 1; i <= m - 1; i++)
      sum += rem[i];
    ans += sum * (n / (m - 1));
    n %= (m - 1);
    // for(ll i=1;i<=n;i++) ans+=rem[i];
    cout << ans << endl;
    return 0;
  }

  if (n <= first) {
    for (ll i = 1; i <= n; i++)
      ans += rem[i];
  } else {
    for (ll i = 1; i <= first - 1; i++)
      ans += rem[i];
    n -= first - 1;
    ans += sum * (n / period);
    n %= period;
    for (ll i = first; i <= first + n - 1; i++)
      ans += rem[i];
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<long long>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<long, long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back

vll rem(1e5 + 5, 0), vec(1e5 + 5, 0);

int main() {
  ll n, x, m, first = 0, period = 0;
  cin >> n >> x >> m;
  ll sum = 0, ans = 0;

  for (ll i = 1; i <= m - 1; i++) {
    rem[i] = x;
    if (vec[x] != 0) {
      first = vec[x];
      period = i - first;
    }
    vec[x] = i;
    x *= x;
    x %= m;
  }
  if (first != 0) {
    for (ll i = first; i <= first + period - 1; i++)
      sum += rem[i];
  } else {
    if (m == 1) {
      ans = 0;
    } else {
      for (ll i = 1; i <= m - 1; i++)
        sum += rem[i];
      ans += sum * (n / (m - 1));
      n %= (m - 1);
      for (ll i = 1; i <= n; i++)
        ans += rem[i];
    }
    cout << ans << endl;
    return 0;
  }

  if (n <= first) {
    for (ll i = 1; i <= n; i++)
      ans += rem[i];
  } else {
    for (ll i = 1; i <= first - 1; i++)
      ans += rem[i];
    n -= first - 1;
    ans += sum * (n / period);
    n %= period;
    for (ll i = first; i <= first + n - 1; i++)
      ans += rem[i];
  }

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

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

int main(void) {
  long n, x, m, ans = 0;
  cin >> n >> x >> m;
  vector<long> k, s(m);
  map<long, long> mp;
  long cur = x, d = 0, e = 0;
  k.push_back(cur);
  mp[cur] = 1;
  for (int i = 2; i <= m; i++) {
    cur = (cur * cur) % m;
    k.push_back(cur);
    if (1 <= mp[cur]) {
      d = mp[cur];
      e = i;
      break;
    }
    mp[cur] = i;
  }
  // cout << d << e << endl;
  long fsum = 0, cirsum = 0, resum = 0;
  for (int i = 0; i < d - 1; i++)
    fsum += k[i];
  for (int i = d - 1; i < e - 1; i++)
    cirsum += k[i];
  for (int i = 0; i <= (n - d) % (e - d); i++)
    resum += k[d - 1 + i];
  // cout << fsum << ' ' << cirsum <<' '<<resum << endl;
  cout << fsum + (n - d) / (e - d) * cirsum + resum << endl;
}

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

int main(void) {
  long n, x, m, ans = 0;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<long> k, s(m);
  map<long, long> mp;
  long cur = x, d = 0, e = 0;
  k.push_back(cur);
  mp[cur] = 1;
  for (int i = 2; i <= m; i++) {
    cur = (cur * cur) % m;
    k.push_back(cur);
    if (1 <= mp[cur]) {
      d = mp[cur];
      e = i;
      break;
    }
    mp[cur] = i;
  }
  // cout << d << e << endl;
  long fsum = 0, cirsum = 0, resum = 0;
  for (int i = 0; i < d - 1; i++)
    fsum += k[i];
  for (int i = d - 1; i < e - 1; i++)
    cirsum += k[i];
  for (int i = 0; i <= (n - d) % (e - d); i++)
    resum += k[d - 1 + i];
  // cout << fsum << ' ' << cirsum <<' '<<resum << endl;
  cout << fsum + (n - d) / (e - d) * cirsum + resum << endl;
}
Problem p02550 (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)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<ll, ll>;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
struct edge {
  ll to, cost;
};

int main() {
  ll N;
  cin >> N;
  ll X, M;
  cin >> X >> M;
  ll S = 0;          // 最終的に答えとなる和
  vector<P> memo(M); // その値Xが { 何番目に出現したか, それまでの計算結果(和) }
  for (int i = 0; i < N; ++i) {
    if (memo[X].first) { // もしその値がすでに配列にあれば
      // 今の順番と、かつてXが出てきた番号の差。これがループ1周分の長さになる
      const auto t = i - memo[X].first;
      // ループ1周分の残りの長さ
      const auto c = (N - i - 1) / t;
      i += t * c; // ループをスキップ?
      S += (S - memo[X].second) * c;
    } else {             // もし配列にまだXが無ければ
      memo[X] = P(i, S); // 配列のX番目にpairを格納する。
    }
    // 必ずSとXを更新する
    S += X;
    (X *= X) %= M;
  }
  cout << S << endl;
}

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<ll, ll>;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
struct edge {
  ll to, cost;
};

int main() {
  ll N;
  cin >> N;
  ll X, M;
  cin >> X >> M;
  ll S = 0;          // 最終的に答えとなる和
  vector<P> memo(M); // その値Xが { 何番目に出現したか, それまでの計算結果(和) }
  for (ll i = 0; i < N; ++i) {
    if (memo[X].first) { // もしその値がすでに配列にあれば
      // 今の順番と、かつてXが出てきた番号の差。これがループ1周分の長さになる
      const auto t = i - memo[X].first;
      // ループ1周分の残りの長さ
      const auto c = (N - i - 1) / t;
      i += t * c; // ループをスキップ?
      S += (S - memo[X].second) * c;
    } else {             // もし配列にまだXが無ければ
      memo[X] = P(i, S); // 配列のX番目にpairを格納する。
    }
    // 必ずSとXを更新する
    S += X;
    (X *= X) %= M;
  }
  cout << S << endl;
}
Problem p02550 (C++)

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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

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

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, X, M;
  cin >> N >> X >> M;
  ll logk{0};
  while ((1ll << logk) < N)
    ++logk;

  vector<vector<ll>> ding(logk, vector<ll>(M));
  vector<vector<ll>> ding2(logk, vector<ll>(M));
  REP(i, M)
  ding[0][i] = (i * i) % M;
  REP(k, logk - 1) {
    REP(i, M) { ding[k + 1][i] = ding[k][ding[k][i]]; }
  }

  REP(i, M)
  ding2[0][i] = i;
  REP(k, logk - 1) {
    REP(i, M) { ding2[k + 1][i] = ding2[k][ding[k][i]] + ding2[k][i]; }
  }

  ll now{X};
  ll res{0};
  --N;
  for (ll bit = 0;; bit++) {
    if (N & 1) {
      res += ding2[bit][now];
      now = ding[bit][now];
    }
    N = N >> 1;
    if (N == 0) {
      res += now;
      break;
    }
  }

  std::cout << res << std::endl;
  return 0;
}


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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

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

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, X, M;
  cin >> N >> X >> M;

  if (N == 1) {
    std::cout << X << std::endl;
    return 0;
  }

  ll logk{0};
  while ((1ll << logk) < N)
    ++logk;

  vector<vector<ll>> ding(logk, vector<ll>(M));
  vector<vector<ll>> ding2(logk, vector<ll>(M));
  REP(i, M)
  ding[0][i] = (i * i) % M;
  REP(k, logk - 1) {
    REP(i, M) { ding[k + 1][i] = ding[k][ding[k][i]]; }
  }

  REP(i, M)
  ding2[0][i] = i;
  REP(k, logk - 1) {
    REP(i, M) { ding2[k + 1][i] = ding2[k][ding[k][i]] + ding2[k][i]; }
  }

  ll now{X};
  ll res{0};
  --N;
  for (ll bit = 0;; bit++) {
    if (N & 1) {
      res += ding2[bit][now];
      now = ding[bit][now];
    }
    N = N >> 1;
    if (N == 0) {
      res += now;
      break;
    }
  }

  std::cout << res << std::endl;
  return 0;
}
Problem p02550 (C++)

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

#define llong long long
#define rep(i, l, n) for (llong(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _abs(a) ((a) > 0 ? (a) : (-(a)))

using namespace std;

int main() {
  llong N, X, M;
  cin >> N >> X >> M;
  int first[M];
  llong first_sum[M];
  rep(i, 0, M) {
    first[i] = -1;
    first_sum[i] = 0;
  }

  llong ans = 0;
  if (M < N) {
    int roop;
    llong roop_ans;
    rep(i, 0, M) {
      if (first[X] >= 0) {
        roop = i - first[X];
        roop_ans = ans - first_sum[X];
        break;
      } else {
        first[X] = i;
        first_sum[X] = ans;
        ans += X;
      }
      X = (X * X) % M;
    }
    ans += roop_ans * ((N - first[X]) / roop - 1);
    N = (N - first[X]) % roop;
  }
  rep(i, 0, N) {
    ans += X;
    X = (X * X) % M;
  }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <vector>

#define llong long long
#define rep(i, l, n) for (llong(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _abs(a) ((a) > 0 ? (a) : (-(a)))

using namespace std;

int main() {
  llong N, X, M;
  cin >> N >> X >> M;
  int first[M];
  llong first_sum[M];
  rep(i, 0, M) {
    first[i] = -1;
    first_sum[i] = 0;
  }

  llong ans = 0;
  if (M < N) {
    int roop;
    llong roop_ans;
    rep(i, 0, N) {
      if (first[X] >= 0) {
        roop = i - first[X];
        roop_ans = ans - first_sum[X];
        break;
      } else {
        first[X] = i;
        first_sum[X] = ans;
        ans += X;
      }
      X = (X * X) % M;
    }
    ans += roop_ans * ((N - first[X]) / roop - 1);
    N = (N - first[X]) % roop;
  }
  rep(i, 0, N) {
    ans += X;
    X = (X * X) % M;
  }
  cout << ans;
}
Problem p02550 (C++)

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

int main() {

  long long n, x, m;
  cin >> n >> x >> m;
  vector<long long> id(m, -1);
  vector<long long> ans;
  long long res;
  long long j = 0;
  long long len = 0;
  while (id[x] == -1) {
    id[x] = len;
    ans[j] = x;
    x = (x * x) % m;
    len++;
    j++;
  }
  long long loop;
  loop = len - id[x];
  long long count;
  count = (n - id[x]) / loop;
  long long value = 0;
  for (long long i = id[x]; i < len; i++) {
    value += ans[i];
  }

  ////////////////ここまではできてるはず

  res = 0;
  if (n < len) {
    res = 0;
    for (long long i = 0; i < n; i++) {
      res += ans[i];
    }

  } else {
    for (long long i = 0; i < id[x]; i++) {
      res += ans[i];
    }
    res = res + count * value;
    long long amari = (n - id[x]) % loop + id[x];
    for (long long i = id[x]; i < amari; i++) {
      res += ans[i];
    }
  }

  cout << res << endl;

  return 0;
}

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

int main() {

  long long n, x, m;
  cin >> n >> x >> m;
  vector<long long> id(m, -1);
  vector<long long> ans;
  long long res;
  long long j = 0;
  long long len = 0;
  while (id[x] == -1) {
    id[x] = len;
    ans.push_back(x);
    x = (x * x) % m;
    len++;
    j++;
  }
  long long loop;
  loop = len - id[x];
  long long count;
  count = (n - id[x]) / loop;
  long long value = 0;
  for (long long i = id[x]; i < len; i++) {
    value += ans[i];
  }

  ////////////////ここまではできてるはず

  res = 0;
  if (n < len) {
    res = 0;
    for (long long i = 0; i < n; i++) {
      res += ans[i];
    }

  } else {
    for (long long i = 0; i < id[x]; i++) {
      res += ans[i];
    }
    res = res + count * value;
    long long amari = (n - id[x]) % loop + id[x];
    for (long long i = id[x]; i < amari; i++) {
      res += ans[i];
    }
  }

  cout << res << endl;

  return 0;
}
Problem p02550 (C++)

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

int main() {

  long long n, x, m;
  cin >> n >> x >> m;
  vector<long long> id(m, -1);
  vector<long long> ans(n, 0);
  long long res;
  long long j = 0;
  long long len = 0;
  while (id[x] == -1) {
    id[x] = len;
    ans.at(j) = x;
    x = (x * x) % m;
    len++;
    j++;
  }

  long long loop;
  loop = len - id[x];
  long long count;
  count = (n - id[x]) / loop;
  long long value = 0;
  for (long long i = id[x]; i < len; i++) {
    value += ans[i];
  }

  ////////////////ここまではできてるはず

  res = 0;
  if (n < len) {
    res = 0;
    for (long long i = 0; i < n; i++) {
      res += ans[i];
    }

  } else {
    for (long long i = 0; i < id[x]; i++) {
      res += ans[i];
    }
    res = res + count * value;
    long long amari = (n - id[x]) % loop + id[x];
    for (long long i = id[x]; i < amari; i++) {
      res += ans[i];
    }
  }

  cout << res << endl;

  return 0;
}

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

int main() {

  long long n, x, m;
  cin >> n >> x >> m;
  vector<long long> id(m, -1);
  vector<long long> ans(m, 0);
  long long res;
  long long j = 0;
  long long len = 0;
  while (id[x] == -1) {
    id[x] = len;
    ans.at(j) = x;
    x = (x * x) % m;
    len++;
    j++;
  }

  long long loop;
  loop = len - id[x];
  long long count;
  count = (n - id[x]) / loop;
  long long value = 0;
  for (long long i = id[x]; i < len; i++) {
    value += ans[i];
  }

  ////////////////ここまではできてるはず

  res = 0;
  if (n < len) {
    res = 0;
    for (long long i = 0; i < n; i++) {
      res += ans[i];
    }

  } else {
    for (long long i = 0; i < id[x]; i++) {
      res += ans[i];
    }
    res = res + count * value;
    long long amari = (n - id[x]) % loop + id[x];
    for (long long i = id[x]; i < amari; i++) {
      res += ans[i];
    }
  }

  cout << res << endl;

  return 0;
}
Problem p02550 (C++)

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

using namespace std;

#define ll long long int
#define ld long double
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout << #x << '=' << x << endl
#define MOD 1000000007
const int N = 1e5 + 1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, x, m;
  cin >> n >> x >> m;
  ll ans = 0;
  if (x == 1) {
    cout << n << "\n";
    return 0;
  } else if (x == 0) {
    cout << "0\n";
    return 0;
  }
  set<ll> s;
  vector<ll> rem;
  ll sum = 0;
  ll same = -1;
  while (1) {
    ans += x;
    s.insert(x);
    rem.pb(x);
    n--;
    if (n == 0) {
      break;
    }
    x *= x;
    x %= m;
    if (s.find(x) != s.end()) {
      same = x;
      break;
    }
  }
  ll l = 0;
  bool ok = 0;
  for (auto it : rem) {
    if (it == same) {
      ok = 1;
    }
    if (ok) {
      sum += it;
      l++;
    }
  }
  ans += (n / l) * sum;
  n -= ((n / l) * l);
  ok = 0;
  for (auto it : rem) {
    if (it == same) {
      ok = 1;
    }
    if (ok) {
      if (n == 0) {
        break;
      }
      ans += it;
      n--;
    }
  }
  cout << ans << "\n";
  return 0;
}

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

using namespace std;

#define ll long long int
#define ld long double
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout << #x << '=' << x << endl
#define MOD 1000000007
const int N = 1e5 + 1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, x, m;
  cin >> n >> x >> m;
  ll ans = 0;
  if (x == 1) {
    cout << n << "\n";
    return 0;
  } else if (x == 0) {
    cout << "0\n";
    return 0;
  }
  set<ll> s;
  vector<ll> rem;
  ll sum = 0;
  ll same = -1;
  while (1) {
    ans += x;
    s.insert(x);
    rem.pb(x);
    n--;
    if (n == 0) {
      break;
    }
    x *= x;
    x %= m;
    if (s.find(x) != s.end()) {
      same = x;
      break;
    }
  }
  ll l = 0;
  bool ok = 0;
  for (auto it : rem) {
    if (it == same) {
      ok = 1;
    }
    if (ok) {
      sum += it;
      l++;
    }
  }
  if (l != 0) {
    ans += (n / l) * sum;
    n -= ((n / l) * l);
  }
  ok = 0;
  for (auto it : rem) {
    if (it == same) {
      ok = 1;
    }
    if (ok) {
      if (n == 0) {
        break;
      }
      ans += it;
      n--;
    }
  }
  cout << ans << "\n";
  return 0;
}
Problem p02550 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
LL n, x, m, a[100005], cnt, sum[100005], mpd[100005];
bool vis[100005];
int main() {
  scanf("%lld %lld %lld", &n, &x, &m);
  LL now = x;
  while (!vis[now]) {
    vis[now] = true;
    a[++cnt] = now;
    sum[cnt] = sum[cnt - 1] + now;
    mpd[now] = cnt;
    now *= now;
    now %= m;
  }
  if (n <= cnt)
    printf("%lld", sum[n]);
  else {
    LL ans = sum[cnt];
    LL beg = mpd[now];
    LL len = (cnt - beg + 1);
    n -= cnt;
    while (n >= len) {
      n -= len;
      ans += sum[cnt] - sum[beg - 1];
    }
    LL i = beg;
    while (n-- > 0) {
      ans += a[i];
      ++i;
    }
    printf("%lld", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
LL n, x, m, a[100005], cnt, sum[100005], mpd[100005];
bool vis[100005];
int main() {
  scanf("%lld %lld %lld", &n, &x, &m);
  LL now = x;
  while (!vis[now]) {
    vis[now] = true;
    a[++cnt] = now;
    sum[cnt] = sum[cnt - 1] + now;
    mpd[now] = cnt;
    now *= now;
    now %= m;
  }
  if (n <= cnt)
    printf("%lld", sum[n]);
  else {
    LL ans = sum[cnt];
    LL beg = mpd[now];
    LL len = (cnt - beg + 1);
    n -= cnt;
    LL dodo = n / len;
    ans += dodo * (sum[cnt] - sum[beg - 1]);
    n -= dodo * len;
    LL i = beg;
    while (n-- > 0) {
      ans += a[i];
      ++i;
    }
    printf("%lld", ans);
  }
  return 0;
}
Problem p02550 (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 per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

int main() {
  ll check[110000] = {};
  ll n, x, m;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  vll v;
  ll cnt = 0LL;
  ll a = x;
  rep(i, n) {
    v.push_back(a);
    cnt += a;
    check[a] = true;
    a *= a;
    a %= m;
    if (check[a])
      break;
    if (a == 0) {
      cout << cnt << endl;
      return 0;
    }
  }
  if (a == x) {
    ll s = v.size();
    ll ans = 0LL;
    ll c = n - n % s;
    ll b = c / s;
    ans += cnt * b;
    rep(i, n % s) ans += v[i];
    cout << ans << endl;
  } else {
    ll ans = 0LL;
    bool swi = true;
    vll vv;
    ll vs = v.size();
    rep(i, vs) {
      if (v[i] == a)
        swi = false;
      if (swi) {
        ans += v[i];
        n--;
      } else
        vv.push_back(v[i]);
    }
    ll s = vv.size();
    ll sum = 0LL;
    rep(i, s) sum += vv[i];
    ll b = n / s;
    ans += sum * b;
    rep(i, n % s) ans += vv[i];
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

int main() {
  ll check[110000] = {};
  ll n, x, m;
  cin >> n >> x >> m;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  vll v;
  ll cnt = 0LL;
  ll a = x;
  rep(i, m + 10) {
    v.push_back(a);
    cnt += a;
    check[a] = true;
    a *= a;
    a %= m;
    if (check[a])
      break;
    if (a == 0) {
      cout << cnt << endl;
      return 0;
    }
  }
  if (a == x) {
    ll s = v.size();
    ll ans = 0LL;
    ll c = n - n % s;
    ll b = c / s;
    ans += cnt * b;
    rep(i, n % s) ans += v[i];
    cout << ans << endl;
  } else {
    ll ans = 0LL;
    bool swi = true;
    vll vv;
    ll vs = v.size();
    rep(i, vs) {
      if (v[i] == a)
        swi = false;
      if (swi) {
        ans += v[i];
        n--;
      } else
        vv.push_back(v[i]);
    }
    ll s = vv.size();
    ll sum = 0LL;
    rep(i, s) sum += vv[i];
    ll b = n / s;
    ans += sum * b;
    rep(i, n % s) ans += vv[i];
    cout << ans << endl;
  }
  return 0;
}
Problem p02551 (C++)

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

int main() {
  ll n;
  int q;
  cin >> n >> q;
  set<ll> st1, st2;
  st1.insert(n);
  st2.insert(n);
  ll c1[100005]{0}, c2[100005]{0};
  ll ans = (n - 2) * (n - 2);
  while (q--) {
    int c;
    ll x;
    cin >> c >> x;
    if (c == 1) {
      auto itr = st1.lower_bound(x);
      if (itr == st1.begin()) {
        st1.insert(x);
        c1[x] = *st2.begin() - 2;
        ans -= c1[x];
      } else
        ans -= c1[*--itr];
    } else {
      auto itr = st2.lower_bound(x);
      if (itr == st2.begin()) {
        st2.insert(x);
        c2[x] = *st1.begin() - 2;
        ans -= c2[x];
      } else
        ans -= c2[*--itr];
    }
  }
  cout << ans << endl;
}


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

int main() {
  ll n;
  int q;
  cin >> n >> q;
  set<ll> st1, st2;
  st1.insert(n);
  st2.insert(n);
  ll c1[200005]{0}, c2[200005]{0};
  ll ans = (n - 2) * (n - 2);
  while (q--) {
    int c;
    ll x;
    cin >> c >> x;
    if (c == 1) {
      auto itr = st1.lower_bound(x);
      if (itr == st1.begin()) {
        st1.insert(x);
        c1[x] = *st2.begin() - 2;
        ans -= c1[x];
      } else
        ans -= c1[*--itr];
    } else {
      auto itr = st2.lower_bound(x);
      if (itr == st2.begin()) {
        st2.insert(x);
        c2[x] = *st1.begin() - 2;
        ans -= c2[x];
      } else
        ans -= c2[*--itr];
    }
  }
  cout << ans << endl;
}
Problem p02551 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 100005;

int n, q;
struct Node {
  int c[N];
  int sum(int x) {
    int res = n;
    while (x) {
      res = min(res, c[x]);
      x -= x & -x;
    }
    return res;
  }
  void add(int x, int d) {
    while (x <= n) {
      c[x] = min(c[x], d);
      x += x & -x;
    }
  }
} T1, T2;
int main() {
  cin >> n >> q;
  LL ans = (n - 2LL) * (n - 2);
  for (int i = 1; i <= n; i++)
    T1.c[i] = T2.c[i] = n;
  while (q--) {
    int op, x;
    cin >> op >> x;
    if (op == 1) {
      int z = T1.sum(n - x + 1);
      T2.add(n - z + 1, x);
      ans -= z - 2;
    } else {
      int z = T2.sum(n - x + 1);
      T1.add(n - z + 1, x);
      ans -= z - 2;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 200005;

int n, q;
struct Node {
  int c[N];
  int sum(int x) {
    int res = n;
    while (x) {
      res = min(res, c[x]);
      x -= x & -x;
    }
    return res;
  }
  void add(int x, int d) {
    while (x <= n) {
      c[x] = min(c[x], d);
      x += x & -x;
    }
  }
} T1, T2;
int main() {
  cin >> n >> q;
  LL ans = (n - 2LL) * (n - 2);
  for (int i = 1; i <= n; i++)
    T1.c[i] = T2.c[i] = n;
  while (q--) {
    int op, x;
    cin >> op >> x;
    if (op == 1) {
      int z = T1.sum(n - x + 1);
      T2.add(n - z + 1, x);
      ans -= z - 2;
    } else {
      int z = T2.sum(n - x + 1);
      T1.add(n - z + 1, x);
      ans -= z - 2;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02551 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

  ll ans = n - 2;
  ans *= ans;
  vector<int> v[] = {vector<int>(n + 1, n), vector<int>(n + 1, n)};
  int d[] = {n, n};
  rep(_, q) {
    int x, a;
    cin >> x >> a;
    --x;
    if (a < d[x]) {
      rep(i, d[x]) { v[x][i] = d[!x]; }
      d[x] = a;
    }
    ans -= v[x][a] - 2;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

  ll ans = n - 2;
  ans *= ans;
  vector<int> v[] = {vector<int>(n + 1, n), vector<int>(n + 1, n)};
  int d[] = {n, n};
  rep(_, q) {
    int x, a;
    cin >> x >> a;
    --x;
    if (a < d[x]) {
      for (int i = a; i < d[x]; ++i) {
        v[x][i] = d[!x];
      }
      d[x] = a;
    }
    ans -= v[x][a] - 2;
  }
  cout << ans << endl;
}
Problem p02552 (C++)

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

int main() {
  int N;
  cin >> N;
  int ans = 1 - N;
  cout << ans;
  for (long long i = 1; i < 10000000; i++) {
    cout << endl;
  }
}


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

int main() {
  int N;
  cin >> N;
  int ans = 1 - N;
  cout << ans;
  for (long long i = 1; i < 100000; i++) {
    cout << endl;
  }
}
Problem p02552 (C++)

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

using namespace std;

#define ar array
#define ll long long

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

void solve() {}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int tc;
  cin >> tc;
  cout << 1 - tc;
  return 1;
}


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

using namespace std;

#define ar array
#define ll long long

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

void solve() {}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int tc;
  cin >> tc;
  cout << 1 - tc;
  return 0;
}
Problem p02552 (C++)

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

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

  return !x;
}

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

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

  cout << (!x) << endl;
}
Problem p02552 (C++)

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

int main() {
  int x;
  cin >> x;
  return !x;
}

static int fastio = []() {
#define endl '\n'
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  return 0;
}();


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

int main() {
  int x;
  cin >> x;
  cout << !x;
}

static int fastio = []() {
#define endl '\n'
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  return 0;
}();
Problem p02552 (C++)

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

int main() {
  int a;
  cin >> a;
  if (a)
    return 0;
  return 1;
}

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

int main() {
  int a;
  cin >> a;
  if (a)
    cout << "0" << endl;
  else
    cout << "1" << endl;
  return 0;
}
Problem p02552 (C++)

Buggy submission (status: Time Limit Exceeded):
// Code by: A Gokul Karthik
#include <bits/stdc++.h>

#define ll long long int
#define mkp make_pair
#define pb push_back
#define ff first
#define ss second
#define fast std::ios_base::sync_with_stdio(false)
#define debug1(a) cout << a << endl;
#define debug2(a, b) cout << a << ' ' << b << endl;
#define debug3(a, b, c) cout << a << ' ' << b << ' ' << c << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repre(i, a, b) for (int i = a; i <= b; i++)
#define clr1(arr) memset(arr, -1, sizeof(arr));
#define clr0(arr) memset(arr, 0, sizeof(arr));
#define RANGE 255
#define po(a)                                                                  \
  if (a < 0) {                                                                 \
    a *= -1;                                                                   \
  }

using namespace std;

void swap(int *xp, int *yp) {
  int temp = *xp;
  *xp = *yp;
  *yp = temp;
}

void bubbleSort(int arr[], int n) {
  int i, j;
  for (i = 0; i < n - 1; i++)
    for (j = 0; j < n - i - 1; j++)
      if (arr[j] > arr[j + 1])
        swap(&arr[j], &arr[j + 1]);
}

int solve() {
  int x;
  cin >> x;
  if (x == 0) {
    cout << 1 << "\n";
  }
  if (x == 1) {
    cout << 0 << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  t = 1;
  for (int i = 0; i < t; i++) {
    solve();
  }
}


Fixed submission:
// Code by: A Gokul Karthik
#include <bits/stdc++.h>

#define ll long long int
#define mkp make_pair
#define pb push_back
#define ff first
#define ss second
#define fast std::ios_base::sync_with_stdio(false)
#define debug1(a) cout << a << endl;
#define debug2(a, b) cout << a << ' ' << b << endl;
#define debug3(a, b, c) cout << a << ' ' << b << ' ' << c << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repre(i, a, b) for (int i = a; i <= b; i++)
#define clr1(arr) memset(arr, -1, sizeof(arr));
#define clr0(arr) memset(arr, 0, sizeof(arr));
#define RANGE 255
#define po(a)                                                                  \
  if (a < 0) {                                                                 \
    a *= -1;                                                                   \
  }

using namespace std;

void swap(int *xp, int *yp) {
  int temp = *xp;
  *xp = *yp;
  *yp = temp;
}

void bubbleSort(int arr[], int n) {
  int i, j;
  for (i = 0; i < n - 1; i++)
    for (j = 0; j < n - i - 1; j++)
      if (arr[j] > arr[j + 1])
        swap(&arr[j], &arr[j + 1]);
}

int solve() {
  int x;
  cin >> x;
  if (x == 0) {
    cout << 1 << "\n";
  }
  if (x == 1) {
    cout << 0 << "\n";
  }
  return 0;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  t = 1;
  for (int i = 0; i < t; i++) {
    solve();
  }
}
Problem p02552 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    ll n;
    cin >> n;
    return n == 0;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;

int main() {
  int t = 1;
  // cin >> t;
  while (t--) {
    ll n;
    cin >> n;
    cout << (n == 0);
  }
}
Problem p02553 (C++)

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

int main() {
  // your code goes here
  ll a, b, c, d, p = -1000000000, i, j;
  cin >> a >> b >> c >> d;
  for (i = a; i <= b; i++) {
    for (j = c; j <= d; j++)
      p = max(p, i * j);
  }
  cout << p << endl;
  return 0;
}

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

int main() {
  // your code goes here
  ll a, b, c, d, p = -1000000000, i, j;
  cin >> a >> b >> c >> d;
  cout << max(max(a * c, a * d), max(b * c, b * d)) << endl;
  return 0;
}
Problem p02553 (C++)

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

int main() {
  long long a, b, c, d, i, j;
  cin >> a >> b >> c >> d;
  long long max;
  if (0 < a && 0 < c) {
    cout << b * d << endl;
    return 0;
  } else if (0 >= b && 0 >= d) {
    cout << a * c << endl;
    return 0;
  } else {

    max = a * c;
    i = a;
    while (i <= b) {
      j = c;
      while (j <= d) {
        if (i * j > max)
          max = i * j;
        j++;
      }
      i++;
    }
  }
  cout << max << endl;
  return 0;
}

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

int main() {
  long long a, b, c, d, i, j;
  cin >> a >> b >> c >> d;
  long long max, max1, max2, max3, max4;
  max1 = a * c;
  max2 = a * d;
  max3 = b * c;
  max4 = b * d;
  max = max1;
  if (max < max2)
    max = max2;
  if (max < max3)
    max = max3;
  if (max < max4)
    max = max4;
  cout << max << endl;
  return 0;
}
Problem p02553 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

typedef long long LL;
int main() {
  int l1, r1, l2, r2;
  cin >> l1 >> r1 >> l2 >> r2;
  LL res = -1e18 - 10;
  for (int i = l1; i <= r1; ++i)
    for (int j = l2; j <= r2; ++j) {
      res = max(res, (LL)i * j);
    }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

typedef long long LL;
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  LL ans1 = (LL)a * c;
  LL ans2 = (LL)a * d;
  LL ans3 = (LL)b * c;
  LL ans4 = (LL)b * d;
  // cout<<ans1<<" "<<ans2<<" "<<ans3<<" "<<ans4<<endl;
  LL ans = max(ans1, ans2);
  ans = max(ans, max(ans3, ans4));
  cout << ans << endl;
  return 0;
}
Problem p02553 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  long long ans = a * c;
  for (long long i = a; i <= b; i++) {
    for (long long j = c; j <= d; j++) {
      ans = max(ans, i * j);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  long long ans;
  ans = max(max(max(a * c, a * d), b * c), b * d);
  cout << ans << endl;
  return 0;
}
Problem p02553 (C++)

Buggy submission (status: Time Limit Exceeded):
/*******************************
 * BASMILLAHHIR RAHMANNIR RAHIM *
 * MD SHAHINUR RAHMAN           *
 * DATE: 12/sep/2020            *
 * DEPT. OF CSE                 *
 * LEADING UNIVERSITY, SYLHET   *
 *******************************/
#include <algorithm>
#include <bits/stdc++.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define shahin                                                                 \
  std::ios_base::sync_with_stdio(false);                                       \
  std::cin.tie(NULL);
#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
const int nax = 1000 * 1007;
const int mod = 1e9 + 7;

using namespace std;
char wcz[nax];

bool fu(int a, int b) { return (a > b); }

void fun() {
  ll a, b, c, d, sum = 0, S = -10000000000;
  cin >> a >> b >> c >> d;
  for (ll i = a; i <= b; i++) {

    for (ll j = c; j <= d; j++) {
      sum = (i * j);
      S = max(sum, S);
      //      cout << i << " "<< j << "="<<sum<< " "<< S<<endl;
    }
  }
  cout << S << endl;
}

int main() {
  shahin
  /*  ll tc,i=1;
    cin >> tc;
    while(tc--)
    {
    //cout << "Case " << i << ": ";
        fun();
    //i++;
    }*/
  fun();
}


Fixed submission:
/*******************************
 * BASMILLAHHIR RAHMANNIR RAHIM *
 * MD SHAHINUR RAHMAN           *
 * DATE: 12/sep/2020            *
 * DEPT. OF CSE                 *
 * LEADING UNIVERSITY, SYLHET   *
 *******************************/
#include <algorithm>
#include <bits/stdc++.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define shahin                                                                 \
  std::ios_base::sync_with_stdio(false);                                       \
  std::cin.tie(NULL);
#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
const int nax = 1000 * 1007;
const int mod = 1e9 + 7;

using namespace std;
char wcz[nax];

bool fu(int a, int b) { return (a > b); }

void fun() {
  ll a, b, c, d, sum = 0, S = -10000000000;
  cin >> a >> b >> c >> d;
  cout << max(a * c, max(a * d, max(b * c, b * d)));
}

int main() {
  shahin
  /*  ll tc,i=1;
    cin >> tc;
    while(tc--)
    {
    //cout << "Case " << i << ": ";
        fun();
    //i++;
    }*/
  fun();
}
Problem p02553 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long int a, b, c, d, p, i, j;
  cin >> a >> b >> c >> d;
  p = a * c;
  for (i = a; i <= b; i = i + (b - a)) {
    for (j = c; j <= d; j = j + (d - c)) {
      if (p < (i * j))
        p = (i * j);
      if (j == d)
        break;
    }
  }
  cout << p;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long int a, b, c, d, p, i, j;
  cin >> a >> b >> c >> d;
  p = a * c;
  for (i = a; i <= b; i = i + (b - a)) {
    for (j = c; j <= d; j = j + (d - c)) {
      if (p < (i * j))
        p = (i * j);
      if (j == d)
        break;
    }
    if (i == b)
      break;
  }
  cout << p;
  return 0;
}
Problem p02553 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  long long maxNum = -1000000000;
  for (long long x = a; x <= b; x++) {
    for (long long y = c; y <= d; y++) {
      maxNum = max(maxNum, x * y);
    }
  }
  cout << maxNum << endl;
}


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

using namespace std;

int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  cout << max(max(a * c, a * d), max(b * c, b * d)) << endl;
}
Problem p02553 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
long long max(long long a, long long b, long long c, long long d) {
  if (a > b) {
    if (a > c) {
      if (a > d) {
        return a;
      } else {
        return d;
      }
    } else {
      if (c > d) {
        return c;
      } else {
        return d;
      }
    }
  } else {
    if (b > c) {
      if (b > d) {
        return b;
      } else {
        return d;
      }
    } else {
      if (c > d) {
        return c;
      } else {
        return d;
      }
    }
  }
}

int main() {
  long long a, b, c, d;
  scanf("%lld %lld %lld %lld");

  printf("%lld", max(a * c, a * d, b * c, b * d));
  return 0;
}

Fixed submission:
#include <stdio.h>
long long max(long long a, long long b, long long c, long long d) {
  if (a > b) {
    if (a > c) {
      if (a > d) {
        return a;
      } else {
        return d;
      }
    } else {
      if (c > d) {
        return c;
      } else {
        return d;
      }
    }
  } else {
    if (b > c) {
      if (b > d) {
        return b;
      } else {
        return d;
      }
    } else {
      if (c > d) {
        return c;
      } else {
        return d;
      }
    }
  }
}

int main() {
  long long a, b, c, d;
  scanf("%lld %lld %lld %lld", &a, &b, &c, &d);

  printf("%lld", max(a * c, a * d, b * c, b * d));
  return 0;
}
Problem p02553 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz size()
#define ll long long
#define all(_v) _v.begin(), _v.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pvllvll pair<vector<ll>, vector<ll>>
#define ld long double
#define veci vector<int>
#define vecll vector<ll>

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;

void solve() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  if (a >= 0 && c >= 0) {
    cout << b * d;
    return;
  }
  if (b <= 0 && d <= 0) {
    cout << a * c;
    return;
  }
  if (a >= 0 && d <= 0) {
    cout << a * d;
    return;
  }
  if (b <= 0 && c >= 0) {
    cout << b * c;
    return;
  }
  assert(0);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  /// cin >> T;
  while (T--)
    solve(), cout << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz size()
#define ll long long
#define all(_v) _v.begin(), _v.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pvllvll pair<vector<ll>, vector<ll>>
#define ld long double
#define veci vector<int>
#define vecll vector<ll>

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;

void solve() {
  ll a[4];
  cin >> a[0] >> a[1] >> a[2] >> a[3];
  ll ans = -1e18;
  ans = max(ans, a[0] * a[2]);
  ans = max(ans, a[0] * a[3]);
  ans = max(ans, a[1] * a[2]);
  ans = max(ans, a[1] * a[3]);
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  /// cin >> T;
  while (T--)
    solve(), cout << '\n';
  return 0;
}
Problem p02553 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define FastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;

int main(void) {
  FastIO;

  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll p[3];
  p[0] = a * c;
  p[1] = a * d;
  p[2] = b * c;
  p[3] = b * d;
  sort(p, p + 4);
  cout << p[3];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define FastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;

int main(void) {
  FastIO;

  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  ll p[4];
  p[0] = a * c;
  p[1] = a * d;
  p[2] = b * c;
  p[3] = b * d;
  sort(p, p + 4);
  cout << p[3];
  return 0;
}
Problem p02553 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, c, d, i, j, p, m, s;
  cin >> a >> b >> c >> d;
  long long h = a * c;
  for (i = a; i <= b; i++) {
    for (j = c; j <= d; j++) {
      p = i * j;
      if (p > h) {
        h = p;
      }
    }
  }
  cout << h;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, c, d, i, j, p, m, s;
  cin >> a >> b >> c >> d;
  long long w, x, y, z;
  w = a * c;
  x = b * c;
  y = a * d;
  z = b * d;
  p = max(w, x);
  m = max(p, y);
  s = max(m, z);
  cout << s;
}
Problem p02553 (Python)

Buggy submission (status: Runtime Error):
if __name__ == "__main__":
    a = int(input())
    b = int(input())
    c = int(input())
    d = int(input())

    if b <= 0:
        if d <= 0:
            xy = a * c
        elif d > 0:
            if c > 0:
                xy = b * c
            elif c == 0:
                xy = 0
            elif c < 0:
                xy = a * c
    elif b > 0:
        if a >= 0:
            if d <= 0:
                xy = a * d
            elif d > 0:
                xy = b * d
        elif a < 0:
            if d <= 0:
                xy = a * c
            elif d > 0:
                if c >= 0:
                    xy = b * d
                elif c < 0:
                    if abs(a * c) > abs(b * d):
                        xy = a * c
                    else:
                        xy = b * d

    print(xy)


Fixed submission:
if __name__ == "__main__":
    a, b, c, d = (int(x) for x in input().split())

    if b <= 0:
        if d <= 0:
            xy = a * c
        elif d > 0:
            if c > 0:
                xy = b * c
            elif c == 0:
                xy = 0
            elif c < 0:
                xy = a * c
    elif b > 0:
        if a >= 0:
            if d <= 0:
                xy = a * d
            elif d > 0:
                xy = b * d
        elif a < 0:
            if d <= 0:
                xy = a * c
            elif d > 0:
                if c >= 0:
                    xy = b * d
                elif c < 0:
                    if abs(a * c) > abs(b * d):
                        xy = a * c
                    else:
                        xy = b * d

    print(xy)