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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
#define int long long
typedef pair<int, int> P;
typedef pair<int, P> P2;
#define F first
#define S second

typedef unsigned long long ull;

struct RollingHash {
  vector<ull> hash, p;
  ull MOD = 1e9 + 7;
  ull B = 1777771;
  RollingHash() {}
  RollingHash(const string &s) {
    int n = s.size();
    hash.assign(n + 1, 0);
    p.assign(n + 1, 1);
    for (int i = 0; i < n; i++) {
      hash[i + 1] = (hash[i] * B + s[i]) % MOD;
      p[i + 1] = p[i] * B % MOD;
    }
  }
  // S[l, r)
  ull find(int l, int r) {
    ull res = hash[r] + MOD - hash[l] * p[r - l] % MOD;
    return res >= MOD ? res - MOD : res;
  }
};

signed main() {

  string s, t;
  cin >> s >> t;
  int B = t.size();
  while ((int)s.size() <= 1000009)
    s = s + s;
  while ((int)t.size() <= 1000009)
    t = t + t;

  RollingHash R(s);
  RollingHash T(t);

  int l = 0, r = 1000009 / B + 2;

  // cout<<t.substr(0,6)<<endl;

  while ((r - l) > 1) {
    int mid = (r + l) / 2;
    ull A = T.find(0, mid * B);
    int flag = 0;
    r(i, s.size() - mid * B - 1) {
      if (R.find(i, i + mid * B) == A) {
        flag = 1;
        break;
      }
    }
    if (!flag)
      r = mid;
    else
      l = mid;
    // cout<<l<<' '<<B<<endl;
  }

  if (r == 1000009 / B + 2)
    cout << -1 << endl;
  else
    cout << l << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
#define int long long
typedef pair<int, int> P;
typedef pair<int, P> P2;
#define F first
#define S second

typedef unsigned long long ull;

struct RollingHash {
  vector<ull> hash, p;
  ull MOD = 1e9 + 7;
  ull B = 1777771;
  RollingHash() {}
  RollingHash(const string &s) {
    int n = s.size();
    hash.assign(n + 1, 0);
    p.assign(n + 1, 1);
    for (int i = 0; i < n; i++) {
      hash[i + 1] = (hash[i] * B + s[i]) % MOD;
      p[i + 1] = p[i] * B % MOD;
    }
  }
  // S[l, r)
  ull find(int l, int r) {
    ull res = hash[r] + MOD - hash[l] * p[r - l] % MOD;
    return res >= MOD ? res - MOD : res;
  }
};

signed main() {

  string s, t;
  cin >> s >> t;
  int B = t.size();
  while ((int)s.size() <= 2000009)
    s = s + s;
  while ((int)t.size() <= 1000009)
    t = t + t;

  RollingHash R(s);
  RollingHash T(t);

  int l = 0, r = 1000009 / B + 2;

  // cout<<t.substr(0,6)<<endl;

  while ((r - l) > 1) {
    int mid = (r + l) / 2;
    ull A = T.find(0, mid * B);
    int flag = 0;
    r(i, s.size() - mid * B - 1) {
      if (R.find(i, i + mid * B) == A) {
        flag = 1;
        break;
      }
    }
    if (!flag)
      r = mid;
    else
      l = mid;
    // cout<<l<<' '<<B<<endl;
  }

  if (r == 1000009 / B + 2)
    cout << -1 << endl;
  else
    cout << l << endl;
}
Problem p02962 (C++)

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

vector<int> Z(string s) {
  vector<int> z(s.size());
  z[0] = s.size();
  int i = 1, j = 0;
  while (i < s.size()) {
    while (i + j < s.size() && s[j] == s[i + j])
      j++;
    if (j == 0) {
      z[i] = 0;
      i++;
      continue;
    }
    z[i] = j;
    int k = 1;
    while (i + k < s.size() && k + z[k] < z[i]) {
      z[i + k] = z[k];
      k++;
    }
    i += k;
    j -= k;
  }
  return z;
}

vector<int> E[2000000];
int deg[200000];
int d[200000];

int main() {
  string s, t;
  cin >> s >> t;
  while (s.size() < t.size())
    s += s;
  auto v = Z(t + s + s);
  for (int i = t.size(); i < t.size() + s.size(); i++) {
    if (v[i] >= t.size()) {
      int a = i - t.size(), b = (a + t.size()) % s.size();
      E[b].push_back(a);
      deg[a] = 1;
    }
  }
  memset(d, -1, sizeof(d));
  queue<int> que;
  rep(i, s.size()) {
    if (!deg[i]) {
      d[i] = 0;
      que.push(i);
    }
  }
  while (!que.empty()) {
    int p = que.front();
    que.pop();
    for (int u : E[p]) {
      if ((--deg[u]) == 0) {
        d[u] = d[p] + 1;
        que.push(u);
      }
    }
  }
  if (*min_element(d, d + s.size()) == -1)
    puts("-1");
  else
    cout << *max_element(d, d + s.size()) << endl;
}

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

vector<int> Z(string s) {
  vector<int> z(s.size());
  z[0] = s.size();
  int i = 1, j = 0;
  while (i < s.size()) {
    while (i + j < s.size() && s[j] == s[i + j])
      j++;
    if (j == 0) {
      z[i] = 0;
      i++;
      continue;
    }
    z[i] = j;
    int k = 1;
    while (i + k < s.size() && k + z[k] < z[i]) {
      z[i + k] = z[k];
      k++;
    }
    i += k;
    j -= k;
  }
  return z;
}

vector<int> E[2000000];
int deg[2000000];
int d[2000000];

int main() {
  string s, t;
  cin >> s >> t;
  while (s.size() < t.size())
    s += s;
  auto v = Z(t + s + s);
  for (int i = t.size(); i < t.size() + s.size(); i++) {
    if (v[i] >= t.size()) {
      int a = i - t.size(), b = (a + t.size()) % s.size();
      E[b].push_back(a);
      deg[a] = 1;
    }
  }
  memset(d, -1, sizeof(d));
  queue<int> que;
  rep(i, s.size()) {
    if (!deg[i]) {
      d[i] = 0;
      que.push(i);
    }
  }
  while (!que.empty()) {
    int p = que.front();
    que.pop();
    for (int u : E[p]) {
      if ((--deg[u]) == 0) {
        d[u] = d[p] + 1;
        que.push(u);
      }
    }
  }
  if (*min_element(d, d + s.size()) == -1)
    puts("-1");
  else
    cout << *max_element(d, d + s.size()) << endl;
}
Problem p02962 (C++)

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

#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

int m;

int get(const string &s) {
  int n = (int)s.size();
  vector<int> z(n);
  for (int i = 1, l = 0, r = 0; i < n; i++) {
    if (i <= r)
      z[i] = min(r - i + 1, z[i - l]);
    while (i + z[i] < n and s[z[i]] == s[i + z[i]])
      z[i]++;
    if (i + z[i] - 1 > r)
      l = i, r = i + z[i] - 1;
  }
  vector<bool> vis(n, false);
  int ans = 0;
  for (int i = m; i < n; i++) {
    if (vis[i])
      continue;
    vis[i] = true;
    if (z[i] >= m) {
      int cnt = 1;
      int j = i;
      while (j + m < n and z[j + m] >= m) {
        j += m;
        vis[j] = true;
        cnt++;
      }
      ans = max(ans, cnt);
    }
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  m = sz(t);
  string S = s;
  while (sz(S) < sz(t))
    S = S + s;
  string X = S + S;
  string Y = X + S;
  string Z = Y + S;
  //  int ans1 = get(t + X);
  double t1 = clock();
  int ans2 = get(t + Y);
  double t2 = clock();
  if ((t2 - t1) / CLOCKS_PER_SEC > 1.2) {
    cout << -1 << endl;
    return (0);
  }
  int ans3 = get(t + Z);
  if (ans2 != ans3) {
    cout << -1 << endl;
    return (0);
  } else {
    cout << ans2 << endl;
  }
  return (0);
}


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

#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

int m;

int get(const string &s) {
  int n = (int)s.size();
  vector<int> z(n);
  for (int i = 1, l = 0, r = 0; i < n; i++) {
    if (i <= r)
      z[i] = min(r - i + 1, z[i - l]);
    while (i + z[i] < n and s[z[i]] == s[i + z[i]])
      z[i]++;
    if (i + z[i] - 1 > r)
      l = i, r = i + z[i] - 1;
  }
  vector<bool> vis(n, false);
  int ans = 0;
  for (int i = m; i < n; i++) {
    if (vis[i])
      continue;
    vis[i] = true;
    if (z[i] >= m) {
      int cnt = 1;
      int j = i;
      while (j + m < n and z[j + m] >= m) {
        j += m;
        vis[j] = true;
        cnt++;
      }
      ans = max(ans, cnt);
    }
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  m = sz(t);
  string S = s;
  while (sz(S) < sz(t))
    S = S + S;
  string X = S + S;
  string Y = X + S;
  string Z = Y + S;
  //  int ans1 = get(t + X);
  double t1 = clock();
  int ans2 = get(t + Y);
  double t2 = clock();
  if ((t2 - t1) / CLOCKS_PER_SEC > 1.2) {
    cout << -1 << endl;
    return (0);
  }
  int ans3 = get(t + Z);
  if (ans2 != ans3) {
    cout << -1 << endl;
    return (0);
  } else {
    cout << ans2 << endl;
  }
  return (0);
}
Problem p02962 (C++)

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

using namespace std;

vector<int> Z(string s) {
  vector<int> z(s.size(), 0);
  int l = 0, r = 0;
  for (int i = 1; i < s.size(); i++) {
    if (i <= r)
      z[i] = min(z[i - l], r - i + 1);

    while (i + z[i] < s.size() && s[i + z[i]] == s[z[i]])
      z[i]++;

    if (i + z[i] - 1 > r)
      l = i, r = i + z[i] - 1;
  }

  return z;
}

const int N = 5e5 + 5;
vector<int> adj[N];
int vis[N];

bool cycle(int node) {
  if (vis[node])
    return vis[node] == 1;

  vis[node] = 1;
  for (int c : adj[node])
    if (cycle(c))
      return 1;
  vis[node] = 2;

  return 0;
}

int memo[N];
int solve(int node) {
  int &ret = memo[node];
  if (~ret)
    return ret;

  ret = 0;
  for (int c : adj[node])
    ret = max(ret, solve(c) + 1);

  return ret;
}

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

  string s, t;
  cin >> s >> t;

  while (s.size() < t.size())
    s += s;

  int n = s.size();
  int m = t.size();

  vector<int> z = Z(t + "$" + s + s);
  int st = m + 1;
  int en = st + n - 1;

  for (int i = st; i <= en; i++)
    if (z[i] >= m)
      adj[i - st].push_back((i - st + m) % n);

  for (int i = 0; i < s.size(); i++) {
    if (cycle(i)) {
      cout << -1;
      return 0;
    }
  }

  memset(memo, -1, sizeof memo);
  int mx = 0;
  for (int i = 0; i < s.size(); i++)
    mx = max(mx, solve(i));

  cout << mx;
}

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

using namespace std;

vector<int> Z(string s) {
  vector<int> z(s.size(), 0);
  int l = 0, r = 0;
  for (int i = 1; i < s.size(); i++) {
    if (i <= r)
      z[i] = min(z[i - l], r - i + 1);

    while (i + z[i] < s.size() && s[i + z[i]] == s[z[i]])
      z[i]++;

    if (i + z[i] - 1 > r)
      l = i, r = i + z[i] - 1;
  }

  return z;
}

const int N = 1e6 + 5;
vector<int> adj[N];
int vis[N];

bool cycle(int node) {
  if (vis[node])
    return vis[node] == 1;

  vis[node] = 1;
  for (int c : adj[node])
    if (cycle(c))
      return 1;
  vis[node] = 2;

  return 0;
}

int memo[N];
int solve(int node) {
  int &ret = memo[node];
  if (~ret)
    return ret;

  ret = 0;
  for (int c : adj[node])
    ret = max(ret, solve(c) + 1);

  return ret;
}

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

  string s, t;
  cin >> s >> t;

  while (s.size() < t.size())
    s += s;

  int n = s.size();
  int m = t.size();

  vector<int> z = Z(t + "$" + s + s);
  int st = m + 1;
  int en = st + n - 1;

  for (int i = st; i <= en; i++)
    if (z[i] >= m)
      adj[i - st].push_back((i - st + m) % n);

  for (int i = 0; i < s.size(); i++) {
    if (cycle(i)) {
      cout << -1;
      return 0;
    }
  }

  memset(memo, -1, sizeof memo);
  int mx = 0;
  for (int i = 0; i < s.size(); i++)
    mx = max(mx, solve(i));

  cout << mx;
}
Problem p02962 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10**6)


def rolling_hash(s, w, MOD):
    ret = []
    tmp = 0
    p = pow(26, w, MOD)
    ords = [ord(c) - 97 for c in s]
    for i, o in enumerate(ords):
        tmp = tmp * 26 + o
        if i >= w:
            tmp = tmp - ords[i - w] * p
        tmp %= MOD
        ret.append(tmp)
    return ret


def solve(s, t):
    MOD = 10**9 + 7
    ls, lt = len(s), len(t)
    k = (lt - 1) // ls + 1
    s *= k * 2
    ls *= k
    rs, rt = rolling_hash(s, lt, MOD), rolling_hash(t, lt, MOD)
    rs = rs[ls:]
    ht = rt[-1]

    checked = [-1] * ls

    def series(i, st):
        if i == st:
            return float("-inf")
        if checked[i] == -1:
            checked[i] = series((i + lt) % ls, st) + 1 if rs[i] == ht else 0
        return checked[i]

    for i, hs in enumerate(rs):
        if hs != ht:
            continue
        ret = series((i + lt) % ls, i)
        if ret == float("-inf"):
            return -1
        checked[i] = ret + 1

    return max(0, max(checked))


s = input()
t = input()
print(solve(s, t))


Fixed submission:
import sys

sys.setrecursionlimit(10**6)


def rolling_hash(s, w, MOD):
    ret = []
    tmp = 0
    p = pow(26, w, MOD)
    ords = [ord(c) - 97 for c in s]
    for i, o in enumerate(ords):
        tmp = tmp * 26 + o
        if i >= w:
            tmp = tmp - ords[i - w] * p
        tmp %= MOD
        ret.append(tmp)
    return ret


def solve(s, t):
    MOD = 10**9 + 7
    ls, lt = len(s), len(t)
    k = (lt - 1) // ls + 1
    s *= k * 2
    ls *= k
    rs, rt = rolling_hash(s, lt, MOD), rolling_hash(t, lt, MOD)
    rs = rs[ls:]
    ht = rt[-1]

    checked = [-1] * ls

    def series(i, st):
        if st <= i < st + lt:
            return float("-inf")
        if checked[i] == -1:
            checked[i] = series((i + lt) % ls, st) + 1 if rs[i] == ht else 0
        return checked[i]

    for i, hs in enumerate(rs):
        if hs != ht:
            continue
        ret = series((i + lt) % ls, i)
        if ret == float("-inf"):
            return -1
        checked[i] = ret + 1

    return max(0, max(checked))


s = input()
t = input()
print(solve(s, t))
Problem p02962 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
t = input()

u = ""
while len(u) < len(s) + len(t):
    u = s * (len(t) // len(s) + 1)

t_add = t + "."


def partial_match_table(word):
    table = [0] * (len(word) + 1)
    table[0] = -1
    i, j = 0, 1

    while j < len(word):
        matched = word[i] == word[j]

        if not matched and i > 0:
            i = table[i]
        else:
            if matched:
                i += 1
            j += 1
            table[j] = i

    return table


table_tx = partial_match_table(t_add)

match = [0] * len(s)

i = j = 0
while i < len(u) and j < len(t_add):
    if u[i] == t_add[j]:
        i += 1
        j += 1
    elif j == 0:
        i += 1
    else:
        j = table_tx[j]
    if j == len(t_add) - 1:
        match[(i - j) % len(s)] = 1


# search
searched = [0] * len(s)
longest = 0
infinity = False
for i in range(len(s)):
    if searched[i]:
        continue
    searched[i] = 1
    p = i
    right = 0
    while match[p]:
        right += 1
        p = (p + len(t)) % len(s)
        searched[p] = 1
        if p == i:
            infinity = True
            break
    left = 0
    p = i
    while match[(p - len(t)) % len(s)]:
        left += 1
        p = (p - len(t)) % len(s)
        searched[p] = 1
        if p == i:
            infinity = True
            break
    if left + right > longest:
        longest = left + right

if infinity:
    print(-1)
else:
    print(longest)


Fixed submission:
s = input()
t = input()

u = s * (len(t) // len(s) + 2)

t_add = t + "."


def partial_match_table(word):
    table = [0] * (len(word) + 1)
    table[0] = -1
    i, j = 0, 1

    while j < len(word):
        matched = word[i] == word[j]

        if not matched and i > 0:
            i = table[i]
        else:
            if matched:
                i += 1
            j += 1
            table[j] = i

    return table


table_tx = partial_match_table(t_add)

match = [0] * len(s)

i = j = 0
while i < len(u) and j < len(t_add):
    if u[i] == t_add[j]:
        i += 1
        j += 1
    elif j == 0:
        i += 1
    else:
        j = table_tx[j]
    if j == len(t_add) - 1:
        match[(i - j) % len(s)] = 1


# search
searched = [0] * len(s)
longest = 0
infinity = False
for i in range(len(s)):
    if searched[i]:
        continue
    searched[i] = 1
    p = i
    right = 0
    while match[p]:
        right += 1
        p = (p + len(t)) % len(s)
        searched[p] = 1
        if p == i:
            infinity = True
            break
    left = 0
    p = i
    while match[(p - len(t)) % len(s)]:
        left += 1
        p = (p - len(t)) % len(s)
        searched[p] = 1
        if p == i:
            infinity = True
            break
    if left + right > longest:
        longest = left + right

if infinity:
    print(-1)
else:
    print(longest)
Problem p02962 (C++)

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

auto calc(const std::string &s) {
  std::vector<int> a(s.size() + 1);
  a[0] = -1;
  for (int i = 0, j = -1; i < s.size(); i++) {
    while (j >= 0 && s[i] != s[j])
      j = a[j];
    j++;
    a[i + 1] = s[i + 1] == s[j] ? a[j] : j;
  }
  return a;
}

int main() {
  std::string s, t;
  std::cin >> s >> t;
  int n = s.size(), m = t.size();
  std::vector<int> e(s.size());
  auto ss = s;
  while (ss.size() < s.size() + t.size())
    ss += s;
  auto a = calc(t);

  for (int i = 0, j = 0; i < ss.size(); i++) {
    while (j >= 0 && ss[i] != t[j])
      j = a[j];
    j++;
    if (j == m) {
      e[(i - m + 1) % n] = 1;
      j = a[j];
    }
  }

  int ret = 0;
  std::vector<int> p(n);
  for (int i = 0; i < n; i++) {
    if (e[i] < 0 || p[i])
      continue;
    int j = i, tmp = 0;
    while (!p[j] && e[j]) {
      j = (j + m) % n;
      if (j == i) {
        std::cout << -1 << std::endl;
        return 0;
      }
      tmp++;
    }
    p[i] = tmp + p[j];
    ret = std::max(ret, p[i]);
  }

  std::cout << ret << std::endl;

  return 0;
}


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

auto calc(const std::string &s) {
  std::vector<int> a(s.size() + 1);
  a[0] = -1;
  for (int i = 0, j = -1; i < s.size(); i++) {
    while (j >= 0 && s[i] != s[j])
      j = a[j];
    j++;
    a[i + 1] = s[i + 1] == s[j] ? a[j] : j;
  }
  return a;
}

int main() {
  std::string s, t;
  std::cin >> s >> t;
  int n = s.size(), m = t.size();
  std::vector<int> e(s.size());
  auto ss = s;
  while (ss.size() < s.size() + t.size())
    ss += s;
  auto a = calc(t);

  for (int i = 0, j = 0; i < ss.size(); i++) {
    while (j >= 0 && ss[i] != t[j])
      j = a[j];
    j++;
    if (j == m) {
      e[(i - m + 1) % n] = 1;
      j = a[j];
    }
  }

  int ret = 0;
  std::vector<int> p(n);
  for (int i = 0; i < n; i++) {
    if (e[i] < 0 || p[i])
      continue;
    int j = i, tmp = 0;
    while (!p[j] && e[j]) {
      p[j] = 1;
      j = (j + m) % n;
      if (j == i) {
        std::cout << -1 << std::endl;
        return 0;
      }
      tmp++;
    }
    p[i] = tmp + p[j];
    ret = std::max(ret, p[i]);
  }

  std::cout << ret << std::endl;

  return 0;
}
Problem p02962 (C++)

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

vector<int> z_algorithm(const vector<int> &S) {
  int N = S.size(), c = 0;
  vector<int> ret(N, 0);
  for (int i = 1; i < N; i++) {
    if (i + ret[i - c] < c + ret[c]) {
      ret[i] = ret[i - c];
    } else {
      int j = max(0, c + ret[c] - i);
      while (i + j < N && S[j] == S[i + j])
        j++;
      ret[i] = j;
      c = i;
    }
  }
  ret[0] = N;
  return ret;
}

vector<int> z_algorithm(const string &S) {
  int N = S.size();
  vector<int> s(N);
  for (int i = 0; i < N; i++)
    s[i] = S[i];
  return z_algorithm(s);
}

vector<int> z_algorithm(string &A, string &B) {
  string S = A + '!' + B;
  auto z = z_algorithm(S);
  vector<int> ret(B.size());
  for (int i = 0; i < (int)B.size(); i++) {
    ret[i] = z[i + A.size() + 1];
  }
  return ret;
}

int main() {
  string s, T;
  cin >> s >> T;
  string S;
  while (S.size() < T.size())
    for (char c : s)
      S.push_back(c);
  int N = S.size(), M = T.size();
  S += S;
  auto z = z_algorithm(T, S);
  vector<int> len(N, -1);
  for (int s = 0; s < N; s++) {
    if (len[s] >= 0)
      continue;
    int pt = s, l = 0;
    while (z[pt] == M) {
      l++;
      pt = (pt + M) % N;
      if (pt == s) {
        cout << -1 << endl;
        return 0;
      }
      if (len[pt] >= 0) {
        l += len[pt];
        break;
      }
    }
    len[s] = l;
  }
  cout << *max_element(len.begin(), len.end()) << endl;
}


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

vector<int> z_algorithm(const vector<int> &S) {
  int N = S.size(), c = 0;
  vector<int> ret(N, 0);
  for (int i = 1; i < N; i++) {
    if (i + ret[i - c] < c + ret[c]) {
      ret[i] = ret[i - c];
    } else {
      int j = max(0, c + ret[c] - i);
      while (i + j < N && S[j] == S[i + j])
        j++;
      ret[i] = j;
      c = i;
    }
  }
  ret[0] = N;
  return ret;
}

vector<int> z_algorithm(const string &S) {
  int N = S.size();
  vector<int> s(N);
  for (int i = 0; i < N; i++)
    s[i] = S[i];
  return z_algorithm(s);
}

vector<int> z_algorithm(string &A, string &B) {
  string S = A + '!' + B;
  auto z = z_algorithm(S);
  vector<int> ret(B.size());
  for (int i = 0; i < (int)B.size(); i++) {
    ret[i] = z[i + A.size() + 1];
  }
  return ret;
}

int main() {
  string s, T;
  cin >> s >> T;
  string S;
  while (S.size() < T.size())
    for (char c : s)
      S.push_back(c);
  int N = S.size(), M = T.size();
  S += S;
  auto z = z_algorithm(T, S);
  vector<int> len(N, -1);
  for (int s = 0; s < N; s++) {
    if (len[s] >= 0)
      continue;
    int pt = s, l = 0;
    while (z[pt] == M) {
      l++;
      pt = (pt + M) % N;
      if (pt == s) {
        cout << -1 << endl;
        return 0;
      }
      if (len[pt] >= 0) {
        l += len[pt];
        break;
      }
      len[pt] = 0;
    }
    len[s] = l;
  }
  cout << *max_element(len.begin(), len.end()) << endl;
}
Problem p02962 (C++)

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

using namespace std;

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

  string s, t;
  cin >> s;
  cin >> t;

  int slen = s.length(), tlen = t.length();
  s = t + '#' + s;
  for (int i = 0; i < t.length(); i++) {
    s += s[tlen + 1 + i % s.length()];
  }

  int z[s.length()];
  z[0] = 0;
  int j0 = 0, j1 = 0;
  for (int i = 1; i < s.length(); i++) {
    if (i > j0) {
      j0 = j1 = i;
    }
    if (i + z[i - j0] < j1) {
      z[i] = z[i - j0];
    } else {
      j0 = i;
      while (j1 < s.length() && s[j1] == s[j1 - j0]) {
        j1++;
      }
      z[i] = j1 - j0;
    }
  }

  bool v[slen];
  memset(v, false, sizeof(v));
  int ans = 0;
  for (int i = 0; i < slen; i++) {
    if (!v[i] && z[tlen + 1 + i] == tlen) {
      int x = i;
      int cnt = 0;
      while (!v[x] && z[tlen + 1 + x] == tlen) {
        v[x] = true;
        cnt++;
        x = ((x - tlen) % slen + slen) % slen;
      }
      if (v[x]) {
        cout << -1 << endl;
        return 0;
      }

      x = (i + tlen) % slen;
      while (!v[x] && z[tlen + 1 + x] == tlen) {
        v[x] = true;
        cnt++;
        x = (x + tlen) % slen;
      }

      ans = max(ans, cnt);
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

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

  string s, t;
  cin >> s;
  cin >> t;

  int slen = s.length(), tlen = t.length();
  s = t + '#' + s;
  for (int i = 0; i < t.length(); i++) {
    s += s[tlen + 1 + i % s.length()];
  }

  int z[s.length()];
  z[0] = 0;
  int j0 = 0, j1 = 0;
  for (int i = 1; i < s.length(); i++) {
    if (i >= j1) {
      j0 = j1 = i;
    }
    if (i + z[i - j0] < j1) {
      z[i] = z[i - j0];
    } else {
      j0 = i;
      while (j1 < s.length() && s[j1] == s[j1 - j0]) {
        j1++;
      }
      z[i] = j1 - j0;
    }
  }

  bool v[slen];
  memset(v, false, sizeof(v));
  int ans = 0;
  for (int i = 0; i < slen; i++) {
    if (!v[i] && z[tlen + 1 + i] == tlen) {
      int x = i;
      int cnt = 0;
      while (!v[x] && z[tlen + 1 + x] == tlen) {
        v[x] = true;
        cnt++;
        x = ((x - tlen) % slen + slen) % slen;
      }
      if (v[x]) {
        cout << -1 << endl;
        return 0;
      }

      x = (i + tlen) % slen;
      while (!v[x] && z[tlen + 1 + x] == tlen) {
        v[x] = true;
        cnt++;
        x = (x + tlen) % slen;
      }

      ans = max(ans, cnt);
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(), V.end()
using ll = long long;
const ll MOD = 1000000007;

bool out[500010], in[500010], arr[500010];

void MP(vector<int> &V, string S) {
  V[0] = -1;
  int j = -1, ss = S.size();
  for (int i = 0; i < ss; i++) {
    while (j >= 0 && S[i] != S[j])
      j = V[j];
    j++;
    V[i + 1] = j;
  }
}

int main() {
  string S, SS, T;
  cin >> S >> T;
  SS = S;
  while (S.size() < T.size())
    S += SS;

  T += 'X';

  int sz = S.size(), tz = T.size();
  vector<int> mpt(tz + 1);
  MP(mpt, T);

  for (int i = 0, st = 0; st < sz;) {
    if (T[i] == S[(st + i) % sz]) {
      i++;
    } else {
      if (T[i] == 'X')
        out[st] = 1, in[(st + i) % sz] = 1;
      st = st + i - mpt[i];
      if (i > 0)
        i = mpt[i];
    }
  }

  int ans = -1;
  bool roop = 0;
  tz--;
  for (int i = 0; i < sz; i++) {
    if (arr[i])
      continue;
    if (out[i] && in[i])
      roop = 1;
    if (out[i] && !in[i]) {
      int f = i, c = 0;
      while (out[f]) {
        arr[f] = 1;
        f = (f + tz) % sz;
        c++;
      }
      ans = max(c, ans);
    }
  }
  if (roop) {
    cout << ans << endl;
  } else {
    cout << max(ans, 0) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(), V.end()
using ll = long long;
const ll MOD = 1000000007;

bool out[1000010], in[1000010], arr[1000010];

void MP(vector<int> &V, string S) {
  V[0] = -1;
  int j = -1, ss = S.size();
  for (int i = 0; i < ss; i++) {
    while (j >= 0 && S[i] != S[j])
      j = V[j];
    j++;
    V[i + 1] = j;
  }
}

int main() {
  string S, SS, T;
  cin >> S >> T;
  SS = S;
  while (S.size() < T.size())
    S += SS;

  T += 'X';

  int sz = S.size(), tz = T.size();
  vector<int> mpt(tz + 1);
  MP(mpt, T);

  for (int i = 0, st = 0; st < sz;) {
    if (T[i] == S[(st + i) % sz]) {
      i++;
    } else {
      if (T[i] == 'X')
        out[st] = 1, in[(st + i) % sz] = 1;
      st = st + i - mpt[i];
      if (i > 0)
        i = mpt[i];
    }
  }

  int ans = -1;
  bool roop = 0;
  tz--;
  for (int i = 0; i < sz; i++) {
    if (arr[i])
      continue;
    if (out[i] && in[i])
      roop = 1;
    if (out[i] && !in[i]) {
      int f = i, c = 0;
      while (out[f]) {
        arr[f] = 1;
        f = (f + tz) % sz;
        c++;
      }
      ans = max(c, ans);
    }
  }
  if (roop) {
    cout << ans << endl;
  } else {
    cout << max(ans, 0) << endl;
  }
}
Problem p02963 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
typedef set<int>::iterator sit;
typedef map<int, int>::iterator mit;
typedef vector<int>::iterator vit;

long long INF = numeric_limits<long long>::max();

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
  ll s;
  cin >> s;
  ll a, b, x, y;
  if (s <= 1e9) {
    a = 0;
    b = 0;
    x = 1;
    y = s;
  } else {
    bool ok = 0;
    for (ll i = sqrt(s); i >= 2; i--) {
      if (s / i > 1000000000 || i > 1000000000)
        break;
      if (s % i == 0) {
        a = 0;
        b = 0;
        x = i;
        y = s / i;
        ok = 1;
        break;
      }
    }
    ll add;
    if (!ok) {
      add = 1;
      ll curr = s + add;
      while (!ok) {
        bool t1 = 0;
        bool t2 = 0;
        for (ll i = sqrt(add); sqrt(add) - i < 100 && i >= 1; i--) {
          if (add % i == 0) {
            t1 = 1;
            a = i;
            b = add / i;
            break;
          }
        }
        if (t1) {
          for (ll i = sqrt(curr); sqrt(curr) - i < 100 && i >= 2; i--) {
            if (curr / i > 1000000000 || i > 1000000000)
              break;
            if (curr % i == 0) {
              t2 = 1;
              x = i;
              y = curr / i;
              break;
            }
          }
        }
        if (t1 & t2)
          ok = 1;
        add++;
      }
    }
    // 1000000000
  }
  cout << a << ' ' << b << ' ' << x << " 0 0 " << y << endl;
  // cout << (x-a)*(y-b)-a*b << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
typedef set<int>::iterator sit;
typedef map<int, int>::iterator mit;
typedef vector<int>::iterator vit;

long long INF = numeric_limits<long long>::max();

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
  ll s;
  cin >> s;
  ll x, y;
  ll v = 1000000000;
  x = (v - s % v) % v;
  y = (s + x) / v;
  cout << "0 0 1000000000 1 " << x << ' ' << y << endl;
}
Problem p02963 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long S;
  cin >> S;
  long long N = pow(10, 9);
  for (long long i = 1; i <= N; i++) {
    if (S % i == 0 && S / i <= N) {
      cout << 0 << " " << 0 << " " << i << " " << 0 << " " << 0 << " " << S / i
           << endl;
      break;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long S;
  cin >> S;
  long long N = pow(10, 9);
  if (S == pow(10, 18)) {
    cout << 0 << " " << 0 << " " << N << " " << 0 << " " << 0 << " " << N
         << endl;
  } else {
    cout << 0 << " " << 0 << " " << N << " " << 1 << " " << N - S % N << " "
         << S / N + 1 << endl;
  }
}
Problem p02963 (C++)

Buggy submission (status: Runtime Error):
// -----------------------------------
// Author     : MatsuTaku
// Affiliation: Tokushima University
// Country    : Japan
// Date       : 04/07/2020
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  ll s;
  cin >> s;
  if (s >= (ll)1e18 / 2) {
    ll d = (ll)1e18 - s;
    ll a;
    for (ll i = sqrt(d); i >= 1; i++) {
      if (d % i == 0)
        a = i;
    }
    cout << 0 << " " << 0 << " " << (ll)1e9 << " " << s / a << " " << a << " "
         << (ll)1e9 << endl;
  } else {
    ll a;
    for (ll i = sqrt(s); i >= 1; i++) {
      if (s % i == 0)
        a = i;
    }
    cout << 0 << " " << 0 << " " << a << " " << 0 << " " << 0 << " " << s / a
         << endl;
  }

  return 0;
}


Fixed submission:
// -----------------------------------
// Author     : MatsuTaku
// Affiliation: Tokushima University
// Country    : Japan
// Date       : 04/07/2020
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  ll s;
  cin >> s;
  ll a = (s - 1) / (ll)1e9 + 1;
  cout << 0 << " " << 0 << " " << (ll)1e9 << " " << 1 << " "
       << ((ll)1e9 * a - s) << " " << a << endl;

  return 0;
}
Problem p02963 (C++)

Buggy submission (status: Runtime Error):
/* Dsingh_24 */

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;

#define N 100005

int main() {
  ios ll s, i, j;
  cin >> s;
  ll hi = min(s, (long long)1e9), lo = 1, mid;
  while (hi - lo > 1) {
    mid = (hi + lo) / 2;
    ll y = (s + mid - 1) / mid;
    if (y > 1e9)
      lo = mid;
    else
      hi = mid;
  }
  ll x2, y2;
  if ((s + mid - 1) / mid < 1e9)
    x2 = mid, y2 = (s + mid - 1) / mid;
  else
    x2 = hi, y2 = (s + hi - 1) / hi;
  // ll x2=mid,y2=(s+mid-1)/mid;
  ll req = x2 * y2 - s;
  if (req == 0) {
    cout << "0 0 0 " << y2 << " " << x2 << " 0";
    return 0;
  }
  for (i = 1; i * i <= req; i++) {
    if (req % i == 0 && (req / i) < 1e9) {
      cout << "0 0 " << x2 << " " << i << " " << (req / i) << " " << y2;
      return 0;
    }
  }
  return 0;
}

Fixed submission:
/* Dsingh_24 */

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;

#define N 100005

int main() {
  ios ll s, i, j;
  cin >> s;
  if (s == 1) {
    cout << "0 0 0 1 1 0";
    return 0;
  }
  ll hi = min(s, (long long)1e9), lo = 1, mid;
  while (hi - lo > 1) {
    mid = (hi + lo) / 2;
    ll y = (s + mid - 1) / mid;
    if (y > 1e9)
      lo = mid;
    else
      hi = mid;
  }
  ll x2, y2;
  if ((s + mid - 1) / mid < 1e9)
    x2 = mid, y2 = (s + mid - 1) / mid;
  else
    x2 = hi, y2 = (s + hi - 1) / hi;
  // ll x2=mid,y2=(s+mid-1)/mid;
  ll req = x2 * y2 - s;
  if (req == 0) {
    cout << "0 0 0 " << y2 << " " << x2 << " 0";
    return 0;
  }
  for (i = 1; i * i <= req; i++) {
    if (req % i == 0 && (req / i) < 1e9) {
      cout << "0 0 " << x2 << " " << i << " " << (req / i) << " " << y2;
      return 0;
    }
  }
  return 0;
}
Problem p02963 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  ll S;
  cin >> S;
  int yaku;
  for (ll i = 1; i * i <= S; i++) {
    if (S % i == 0) {
      yaku = i;
    }
  }
  cout << "0 0 0 " << yaku << " " << S / yaku << " 0" << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  long long s;
  cin >> s;

  const int v = 1000000000;

  int x = (v - (s % v)) % v;
  int y = (s + x) / v;

  cout << "0 0 1000000000 1 " << x << " " << y << endl;
}
Problem p02963 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll infl = 10000000000000007LL;
constexpr int inf = 1000000007;

int main() {
  ll s;
  cin >> s;
  ll k = 0;
  while (true) {
    ll t = s + k;
    for (ll i = 1; i * i <= t; ++i) {
      if (t % i == 0) {
        ll u = t / i;
        if (u <= 1000000000LL) {
          cout << 0 << " ";
          cout << 0 << " ";
          cout << i << " ";
          cout << u << " ";
          cout << 1 << " ";
          cout << k << " ";
          return 0;
        }
      }
    }
    ++k;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll infl = 10000000000000007LL;
constexpr int inf = 1000000007;

int main() {
  ll s;
  cin >> s;
  if (s == 1000000000000000000LL) {
    cout << 0 << " ";
    cout << 0 << " ";
    cout << 0 << " ";
    cout << 1000000000 << " ";
    cout << 1000000000 << " ";
    cout << 0 << " ";
  } else {
    ll a = (ll)(sqrt(s)) + 1;
    ll b = a * a - s;
    cout << 0 << " ";
    cout << 0 << " ";
    cout << a << " ";
    cout << b << " ";
    cout << 1 << " ";
    cout << a << " ";
  }
  return 0;
}
Problem p02963 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static ll INF = 1e9;
using namespace std;

int main() {
  ll s;
  cin >> s;
  ll a, b;
  ll l = 0, r = s;
  ll mid = (l + r) / 2;
  ll xa, ya, xb, yb, xc, yc;
  bool judge = false;

  if (s < 1e9)
    for (ll i = 0; i <= INF; ++i) {
      if (judge)
        break;
      a = s + i;
      if (a <= INF * INF) {
        for (ll j = 1; j <= INF; ++j) {
          if ((a % j == 0) && (a / j <= 1e9)) {
            xb = j;
            yc = a / j;
            judge = true;
            break;
          }
        }
      }
    }
  else
    for (ll i = INF; i >= 0; --i) {
      if (judge)
        break;
      a = s + i;
      if (a <= INF * INF) {
        for (ll j = INF; j >= 1; --j) {
          if ((a % j == 0) && (a / j <= 1e9)) {
            xb = j;
            yc = a / j;
            judge = true;
            break;
          }
        }
      }
      if (a > s / 2)
        l = mid + 1;
      else
        r = mid;
      i = (l + r) / 2;
    }

  xa = 0;
  ya = 0;
  b = a - s;
  l = 0;
  r = b;
  mid = (r + l) / 2;
  for (ll j = 1e9; j >= 1; --j) {
    if (b % j == 0 && b / j <= 1e9) {
      xc = j;
      yb = b / j;
      break;
    }
    if (b > s / 2)
      l = mid + 1;
    else
      r = mid;
    j = (l + r) / 2;
  }
  cout << xa << " " << ya << " " << xb << " " << yb << " " << xc << " " << yc
       << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static ll INF = 1e9;
using namespace std;

int main() {
  ll s;
  cin >> s;
  ll xa = 0, ya = 0, xb = 1e9, yb, xc = 1, yc;
  // xb*yc-xc*yb=s
  yc = s / INF; // INFで割るため
  yb = INF * yc - s;
  if (yb < 0) {
    yc++;
    yb = INF * yc - s;
  }
  cout << xa << " " << ya << " " << xb << " " << yb << " " << xc << " " << yc
       << endl;
  return 0;
}
Problem p02963 (C++)

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

using namespace std;

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

typedef long long LL;
typedef vector<int> VI;

#define UB 1000000000

int main() {
  int64_t s;
  cin >> s;
  int i = 1;
  while (true) {
    if (s <= i * i) {
      break;
    }
    i++;
  }
  int64_t x2 = i;
  int64_t y3 = i;
  while (x2 * y3 - s > UB) {
    x2--;
  }
  int64_t x3 = x2 * y3 - s;
  int64_t y2 = 1;
  cout << 0 << " " << 0 << " " << x2 << " " << y2 << " " << x3 << " " << y3
       << endl;
  return 0;
}


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

using namespace std;

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

typedef long long LL;
typedef vector<int> VI;

#define UB 1000000000

int main() {
  int64_t s;
  cin >> s;
  int64_t i = (int64_t)ceil(sqrt(s));
  int64_t x2 = i;
  int64_t y3 = i;
  while (x2 * y3 - s > UB) {
    x2--;
  }
  int64_t x3 = x2 * y3 - s;
  int64_t y2 = 1;
  cout << 0 << " " << 0 << " " << x2 << " " << y2 << " " << x3 << " " << y3
       << endl;
  return 0;
}
Problem p02963 (C++)

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

int main() {
  long long int S;
  cin >> S;
  int xy[6];
  xy[0] = 0;
  xy[1] = 0;
  bool flag = 0;
  for (int i = S / 2; i <= 1000000000; i++) {
    for (int j = 0; j <= 1000000000; j++) {
      for (int k = 0; k <= 1000000000; k++) {
        for (int h = S / 2; h <= 1000000000; h++) {
          if (i * h - k * j == S) {
            xy[2] = i;
            xy[3] = j;
            xy[4] = k;
            xy[5] = h;
            flag = 1;
            break;
          }
          if (flag)
            break;
        }
        if (flag)
          break;
      }
      if (flag)
        break;
    }
    if (flag)
      break;
  }
  for (int i = 0; i < 5; i++) {
    cout << xy[i] << " ";
  }
  cout << xy[5] << endl;
}


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

int main() {
  long long int S;
  cin >> S;
  int xy[6];
  xy[0] = 0;
  xy[1] = 0;
  xy[2] = 1000000000;
  xy[3] = 1;
  xy[5] = S / 1000000000 + 1;
  xy[4] = 1000000000 - S % 1000000000;
  if (S == 1000000000000000000) {
    xy[3] = 0;
    xy[5] = 1000000000;
  }
  for (int i = 0; i < 5; i++) {
    cout << xy[i] << " ";
  }
  cout << xy[5] << endl;
}
Problem p02963 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define cs const
#define pb push_back
#define y1 y_1
using namespace std;
typedef long long ll;
int main() {
#ifdef FSYolanda
  freopen("1.in", "r", stdin);
#endif
  ll S;
  scanf("%lld", &S);
  ll t = sqrt(S);
  if (1ll * t * t == S) {
    cout << 0 << " " << 0 << " " << t << " " << 0 << " " << 0 << " " << t
         << '\n';
    return 0;
  }
  ++t;
  ll x2 = t, y1 = t;
  ll r = t * t - S;
  if (r & 1)
    ++x2, --y1, --r;
  ll x1 = 2, y2 = r >> 1;
  assert(x1 <= 1e9);
  assert(x2 <= 1e9);
  assert(y1 <= 1e9);
  assert(y2 <= 1e9);
  cout << 0 << " " << 0 << " " << x1 << " " << y1 << " " << x2 << " " << y2;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define cs const
#define pb push_back
#define y1 y_1
using namespace std;
typedef long long ll;
int main() {
#ifdef FSYolanda
  freopen("1.in", "r", stdin);
#endif
  ll S;
  scanf("%lld", &S);
  ll t = sqrt(S);
  if (1ll * t * t == S) {
    cout << 0 << " " << 0 << " " << t << " " << 0 << " " << 0 << " " << t
         << '\n';
    return 0;
  }
  ++t;
  ll x2 = t, y1 = t;
  ll r = t * t - S;
  ll x1 = 1, y2 = r;
  if (t < 1e9) {
    if (r & 1)
      ++x2, --y1, --r;
    x1 = 2, y2 = r >> 1;
    cout << 0 << " " << 0 << " " << x1 << " " << y1 << " " << x2 << " " << y2;
    return 0;
  } else {
    if (r <= 1e9) {
      cout << 0 << " " << 0 << " " << x1 << " " << y1 << " " << x2 << " " << y2;
      return 0;
    }
    y1 = t - 1;
    r = t * (t - 1) - S;
    y2 = r;
    cout << 0 << " " << 0 << " " << x1 << " " << y1 << " " << x2 << " " << y2;
  }
  return 0;
}
Problem p02963 (C++)

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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { std::cout << #a << " = " << a << "\n"; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}
template <class T, class V> istream &operator>>(istream &ist, pair<T, V> &p) {
  return ist >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &ost, pair<T, T> &p) {
  return ost << p.first << ", " << p.second;
}
template <class T> istream &operator>>(istream &ist, vector<T> &vs) {
  for (auto &e : vs)
    ist >> e;
  return ist;
}

typedef long long ll;
int const inf = INT_MAX / 2;

void output(int x1, int y1, int x2, int y2, int x3, int y3) {
  printf("%d %d %d %d %d %d\n", x1, y1, x2, y2, x3, y3);
}

int const ten_9 = 1e9;

int main() {
  ll S;
  cin >> S;
  ll X1, X2, X3, Y1, Y2, Y3;
  X1 = ten_9, Y1 = 1;
  X3 = 0, Y3 = 0;
  if (0 <= S && S <= ten_9) {
    X2 = S, Y2 = 0;
  } else {
    // (S + X2) / 10^9 = Y2 (ただし (S + X2) % 10^9 == 0)
    X2 = ten_9 - S % ten_9, Y2 = (S + X2) / ten_9;
  }
  assert(abs(X1 * Y2 - X2 * Y1) == S);
  assert(Y2 <= ten_9);
  output(X1, Y1, X2, Y2, X3, Y3);
}


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

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \
  { std::cout << #a << " = " << a << "\n"; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}
template <class T, class V> istream &operator>>(istream &ist, pair<T, V> &p) {
  return ist >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &ost, pair<T, T> &p) {
  return ost << p.first << ", " << p.second;
}
template <class T> istream &operator>>(istream &ist, vector<T> &vs) {
  for (auto &e : vs)
    ist >> e;
  return ist;
}

typedef long long ll;
int const inf = INT_MAX / 2;

void output(int x1, int y1, int x2, int y2, int x3, int y3) {
  printf("%d %d %d %d %d %d\n", x1, y1, x2, y2, x3, y3);
}

int const ten_9 = 1e9;

int main() {
  ll S;
  cin >> S;
  ll X1, X2, X3, Y1, Y2, Y3;
  X1 = ten_9, Y1 = 1;
  X3 = 0, Y3 = 0;
  if (0 <= S && S <= ten_9) {
    X2 = S, Y2 = 0;
  } else {
    // (S + X2) / 10^9 = Y2 (ただし (S + X2) % 10^9 == 0)
    X2 = ten_9 - S % ten_9, Y2 = (S + X2) / ten_9;
  }
  assert(abs(X1 * Y2 - X2 * Y1) == S);
  if (Y2 > ten_9) {
    output(0, 0, ten_9, 0, 0, ten_9);
  } else {
    output(X1, Y1, X2, Y2, X3, Y3);
  }
}