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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007ll
#define pb push_back
#define vl vector<ll>
#define pll pair<ll, ll>
#define mp make_pair
using namespace std;
#define pi (long double)3.14159265358979323846
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
struct sch {
  ll s, e, t;
};
bool sortinrev(const struct sch &a, const struct sch &b) { return (a.s < b.s); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}
int main() {
  ll x;
  cin >> x;
  if (x % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  ll ans = 0;
  ll rem = 0;
  while (1) {
    rem *= 10;
    rem += 7;
    ans++;
    if (rem % x == 0)
      break;
    rem = rem % x;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007ll
#define pb push_back
#define vl vector<ll>
#define pll pair<ll, ll>
#define mp make_pair
using namespace std;
#define pi (long double)3.14159265358979323846
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
struct sch {
  ll s, e, t;
};
bool sortinrev(const struct sch &a, const struct sch &b) { return (a.s < b.s); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}
int main() {
  ll x;
  cin >> x;
  if (x % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  ll ans = 0;
  ll rem = 0;
  while (1) {
    rem *= 10;
    rem += 7;
    ans++;
    if (rem % x == 0)
      break;
    rem = rem % x;
    if (ans > 1000000) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
}
Problem p02596 (C++)

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

const int INF = 1001001001;
// const ll INF = 1000000000000000000;

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

  if (k % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  int ans;
  vector<int> s;
  s.push_back(0);
  int id = 0;
  ll term = 7 % k;
  while (true) {
    int &ref = s.back();
    if (id != 0 and ref == 0) {
      ans = id;
      break;
    }
    s.push_back((ref + term) % k);
    id++;
    term *= 10;
    term %= k;
  }
  cout << ans << endl;
  return 0;
}


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

const int INF = 1001001001;
// const ll INF = 1000000000000000000;

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

  if (k % 2 == 0 or k % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }
  int ans;
  vector<int> s;
  s.push_back(0);
  int id = 0;
  ll term = 7 % k;
  while (true) {
    int &ref = s.back();
    if (id != 0 and ref == 0) {
      ans = id;
      break;
    }
    s.push_back((ref + term) % k);
    id++;
    term *= 10;
    term %= k;
  }
  cout << ans << endl;
  return 0;
}
Problem p02596 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
int dp[MAXN];
int main() {
  int k;
  cin >> k;
  dp[1] = 7 % k;
  if (dp[1] == 0)
    return cout << 1 << "\n", 0;
  for (int i = 2; i <= MAXN; i++) {
    dp[i] = (dp[1] + (dp[i - 1] * (10 % k)) % k) % k;
    if (dp[i] == 0)
      return cout << i << "\n", 0;
  }
  return cout << -1 << "\n", 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
int dp[MAXN];
int main() {
  int k;
  cin >> k;
  dp[1] = 7 % k;
  if (dp[1] == 0)
    return cout << 1 << "\n", 0;
  for (int i = 2; i < MAXN; i++) {
    dp[i] = (dp[1] + (dp[i - 1] * (10 % k)) % k) % k;
    if (dp[i] == 0)
      return cout << i << "\n", 0;
  }
  return cout << -1 << "\n", 0;
}
Problem p02596 (C++)

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

int main() {
  int K;
  std::cin >> K;
  int m = 0;
  for (int i = 0; i < K; i++) {
    m = (m * 10 + 7 % K) % K;
    if (m == 0) {
      std::cout << i + 1 << std::endl;
      exit(1);
    }
  }
  std::cout << -1 << std::endl;
}

Fixed submission:
#include <iostream>

int main() {
  int K;
  std::cin >> K;
  int m = 0;
  for (int i = 0; i < K; i++) {
    m = (m * 10 + 7 % K) % K;
    if (m == 0) {
      std::cout << i + 1 << std::endl;
      exit(0);
    }
  }
  std::cout << -1 << std::endl;
}
Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<int, int> P;
int main() {
  int k;
  cin >> k;
  ll s = 7;
  ll ans = 1;
  while (1) {
    if (s % k == 0) {
      cout << ans << endl;
      return 0;
    } else {
      s %= k;
      s = s * 10 + 7;
      ans++;
    }
    if (k % 2 == 0) {
      break;
    }
    if (ans > INF) {
      break;
    }
  }
  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<int, int> P;
int main() {
  int k;
  cin >> k;
  ll s = 7;
  ll ans = 1;
  while (1) {
    if (s % k == 0) {
      cout << ans << endl;
      return 0;
    } else {
      s %= k;
      s = s * 10 + 7;
      ans++;
    }
    if (k % 2 == 0) {
      break;
    }
    if (ans > k + 1) {
      break;
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define INF LONG_LONG_MAX
#define int long long
#define ar array
#define db double
#define pi 3.14159265358979323846
#define pow pw
int pw(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans *= a;
    a *= a;
    b /= 2;
  }
  return (ans);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int k;
  cin >> k;
  int ans = 0, du = 0;
  do {
    ans++;
    du = (du * 10 + 7) % k;
  } while (du);
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF LONG_LONG_MAX
#define int long long
#define ar array
#define db double
#define pi 3.14159265358979323846
#define pow pw
int pw(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans *= a;
    a *= a;
    b /= 2;
  }
  return (ans);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int k;
  cin >> k;
  int ans = 0, du = 0;
  do {
    ans++;
    du = (du * 10 + 7) % k;
    if (ans > k) {
      cout << -1;
      return 0;
    }
  } while (du);
  cout << ans;
  return 0;
}
Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int k;
  cin >> k;
  if (k == 2 || k == 5) {
    cout << -1 << endl;
    return 0;
  }
  ll t = 7;
  int cnt = 1;
  while (t % k != 0) {
    t = (t * 10 + 7) % k;
    cnt++;
  }
  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int k;
  cin >> k;
  if (k % 2 == 0 || k % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }
  ll t = 7;
  int cnt = 1;
  while (t % k != 0) {
    t = (t * 10 + 7) % k;
    cnt++;
  }
  cout << cnt << endl;
}
Problem p02596 (C++)

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

using namespace std;

int main() {
  long long k;
  cin >> k;
  if (k % 2 == 0) {
    return cout << "-1\n", 0;
  }
  int cur = 7 % k;
  int ans = 1;
  while (cur != 0) {
    cur = cur * 10 + 7;
    cur %= k;
    ans++;
  }
  cout << ans << endl;
}


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

using namespace std;

int main() {
  long long k;
  cin >> k;
  if (k % 2 == 0 || k % 5 == 0) {
    return cout << "-1\n", 0;
  }
  int cur = 7 % k;
  int ans = 1;
  while (cur != 0) {
    cur = cur * 10 + 7;
    cur %= k;
    ans++;
  }
  cout << ans << endl;
}
Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int k;
  cin >> k;
  if (k % 2 == 0) {
    cout << "-1";
    return 0;
  }
  int p = 0, c = 0;
  while (1 == 1) {
    c++;
    p = (p * 10 + 7) % k;
    if (p == 0) {
      break;
    }
  }
  cout << c;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int k;
  cin >> k;
  if (k % 2 == 0) {
    cout << "-1";
    return 0;
  }
  if (k % 5 == 0) {
    cout << "-1";
    return 0;
  }
  int p = 0, c = 0;
  while (1 == 1) {
    c++;
    p = (p * 10 + 7) % k;
    if (p == 0) {
      break;
    }
  }
  cout << c;
  return 0;
}
Problem p02596 (C++)

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

int main() {
  int K;
  cin >> K;
  int cnt = 0, now = 0;
  auto start = chrono::system_clock::now();
  while (++cnt) {
    now = (now * 10 + 7) % K;
    if (now == 0)
      return cout << cnt << "\n", 0;
    if (static_cast<double>(chrono::duration_cast<chrono::microseconds>(
                                chrono::system_clock::now() - start)
                                .count() /
                            1000.0) > 1999)
      return cout << -1 << "\n", 0;
  }
}

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

int main() {
  int K;
  cin >> K;
  int cnt = 0, now = 0;
  auto start = chrono::system_clock::now();
  while (++cnt) {
    now = (now * 10 + 7) % K;
    if (now == 0)
      return cout << cnt << "\n", 0;
    if (static_cast<double>(chrono::duration_cast<chrono::microseconds>(
                                chrono::system_clock::now() - start)
                                .count() /
                            1000.0) > 1990)
      return cout << -1 << "\n", 0;
  }
}
Problem p02596 (C++)

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

int main() {
  int K;
  cin >> K;
  auto start = system_clock::now();
  for (int cnt = 1, now = 0;
       static_cast<double>(
           duration_cast<milliseconds>(system_clock::now() - start).count()) <
       1999;
       cnt++) {
    now = (now * 10 + 7) % K;
    if (now == 0)
      return cout << cnt << "\n", 0;
  }
  cout << -1 << "\n";
}

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

int main() {
  int K;
  cin >> K;
  auto start = system_clock::now();
  for (int cnt = 1, now = 0;
       static_cast<double>(
           duration_cast<milliseconds>(system_clock::now() - start).count()) <
       1990;
       cnt++) {
    now = (now * 10 + 7) % K;
    if (now == 0)
      return cout << cnt << "\n", 0;
  }
  cout << -1 << "\n";
}
Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll k;
  ll num = 7;
  cin >> k;
  ll sum = 7LL % k;
  ll fac = 10LL % k;
  if (k % 2 != 0 && k != 5) {
    ll cnt = 1;
    while (sum % k) {
      sum += (num % k * fac) % k;
      num = (num % k * fac);
      cnt++;
    }
    cout << cnt << endl;
  } else {
    cout << -1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll k;
  ll num = 7;
  cin >> k;
  ll sum = 7LL % k;
  ll fac = 10LL % k;
  ll cnt = 1;
  while (sum % k && cnt <= 10000000) {
    sum += (num % k * fac) % k;
    num = num % k * fac;
    cnt++;
  }
  if (sum % k == 0) {
    cout << cnt << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p02596 (C++)

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

int main() {
  int K;
  cin >> K;
  if ((K % 2 == 0) || (K % 5 == 0)) {
    cout << -1 << endl;
  } else {
    if (K % 7 == 0) {
      K /= 7;
    }
    int mod = 9 * K;
    int x = 1 % 9 * K;
    for (int i = 1;; i++) {
      x = (x * (10 % mod)) % mod;
      if (x == 1) {
        cout << i << endl;
        return 0;
      }
    }
  }
  return 0;
}


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

int main() {
  int K;
  cin >> K;
  if ((K % 2 == 0) || (K % 5 == 0)) {
    cout << -1 << endl;
  } else {
    if (K % 7 == 0) {
      K /= 7;
    }
    int mod = 9 * K;
    int x = 1 % (9 * K);
    for (int i = 1;; i++) {
      x = (x * (10 % mod)) % mod;
      if (x == 1) {
        cout << i << endl;
        return 0;
      }
    }
  }
  return 0;
}
Problem p02596 (C++)

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

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"

int main() {
  string s;
  cin >> s;
  int K = stoi(s);
  if (K % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }

  int count = s.length();

  int seven = 7;
  for (int j = 1; j < count; ++j) {
    seven = seven * 10 + 7;
  }
  if (seven < K) {
    seven = seven * 10 + 7;
    ++count;
  }

  long long preAmari = seven % K;
  for (int i = 0;; ++i) {
    if (preAmari % K == 0) {
      cout << count + i << endl;
      return 0;
    }
    preAmari = (preAmari * 10 + 7) % K;
  }

  return 0;
}

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

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"

int main() {
  string s;
  cin >> s;
  int K = stoi(s);
  if (K % 2 == 0 || K % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }

  int count = s.length();

  int seven = 7;
  for (int j = 1; j < count; ++j) {
    seven = seven * 10 + 7;
  }
  if (seven < K) {
    seven = seven * 10 + 7;
    ++count;
  }

  long long preAmari = seven % K;
  for (int i = 0;; ++i) {
    if (preAmari % K == 0) {
      cout << count + i << endl;
      return 0;
    }
    preAmari = (preAmari * 10 + 7) % K;
  }

  return 0;
}
Problem p02596 (C++)

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

int64_t MOD = 1000000007;
double PI = 3.141592653589793;

int main() {
  int K;
  cin >> K;
  if (K % 2 == 0 || K % 2 == 5)
    cout << -1;
  else {
    int tmp = 7, ans = 1;
    while (true) {
      if (tmp % K == 0) {
        cout << ans;
        break;
      } else {
        ans++;
        tmp = (tmp * 10 + 7) % K;
      }
    }
  }
}

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

int64_t MOD = 1000000007;
double PI = 3.141592653589793;

int main() {
  int K;
  cin >> K;
  if (K % 2 == 0 || K % 5 == 0)
    cout << -1;
  else {
    int tmp = 7, ans = 1;
    while (true) {
      if (tmp % K == 0) {
        cout << ans;
        break;
      } else {
        ans++;
        tmp = (tmp * 10 + 7) % K;
      }
    }
  }
}
Problem p02596 (C++)

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

int main() {
  long long int K;
  cin >> K;
  long long int N = 7;
  long long int i = 1;
  long long int output = -1;

  if (K % 2 == 0) {
    cout << output << '\n';
    return 0;
  }

  while (i < LLONG_MAX) {
    if (N % K == 0) {
      output = i;
      break;
    }
    N %= K;
    N = 10 * N + 7;

    i += 1;
  }
  cout << output << '\n';
}

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

int main() {
  long long int K;
  cin >> K;
  long long int N = 7;
  long long int i = 1;
  long long int output = -1;

  if (K % 2 == 0) {
    cout << output << '\n';
    return 0;
  }

  while (i < 7 * K + 1) {
    if (N % K == 0) {
      output = i;
      break;
    }
    N %= K;
    N = 10 * N + 7;

    i += 1;
  }
  cout << output << '\n';
}
Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h> //线段树维护区间和
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + 10;

int main() {
  int k;
  scanf("%d", &k);
  ll n = 7;
  int ans = 1;
  if (k % 2 == 0 || k == 5) {
    printf("-1");
    return 0;
  }
  while (1) {
    if (n % k == 0) {
      printf("%d", ans);
      break;
    }
    ans++;
    n = n * 10 + 7;
    n %= k;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h> //线段树维护区间和
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + 10;

int main() {
  int k;
  scanf("%d", &k);
  ll n = 7;
  int ans = 1;
  if (k % 2 == 0 || k % 5 == 0) {
    printf("-1");
    return 0;
  }
  while (1) {
    if (n % k == 0) {
      printf("%d", ans);
      break;
    }
    ans++;
    n = n * 10 + 7;
    n %= k;
  }

  return 0;
}
Problem p02596 (C++)

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

int main() {
  int A = 7;
  int K, ans;
  cin >> K;
  if (K % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  long long amari = 0;
  for (int i = 1;; i++) {
    amari += A % K;
    if (amari % K == 0) {
      ans = i;
      break;
    } else {
      A *= 10;
      A %= K;
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int A = 7;
  int K, ans;
  cin >> K;
  if (K % 2 == 0 || K % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }
  long long amari = 0;
  for (int i = 1;; i++) {
    amari += A % K;
    if (amari % K == 0) {
      ans = i;
      break;
    } else {
      A *= 10;
      A %= K;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02596 (C++)

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

using namespace std;

int main() {
  int K, sum = 0, count = 1;
  cin >> K;

  if (K % 2 == 0 || K % 5 == 0) {
    cout << "-1";
  } else {
    while (1) {
      sum = 7 + sum * 10;
      if (sum % K == 0) {
        cout << count;
        break;
      }
      count++;
    }
  }
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int K, sum = 0, count = 1;
  cin >> K;

  if (K % 2 == 0 || K % 5 == 0) {
    cout << "-1";
  } else {
    while (1) {
      sum = (7 + sum * 10) % K;
      if (sum % K == 0) {
        cout << count;
        break;
      }
      count++;
    }
  }
}
Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
#define int long long int
#define vi vector<int>
#define vvi vector<vector<int>>
using namespace std;

int32_t main() {
  // your code goes here
  int k, count = 0, sum = 0, i, j;
  cin >> k;
  if (k % 2 == 0) {
    cout << "-1\n";
  } else {
    while (1) {
      sum = 10 * sum + 7;
      count++;
      sum %= k;
      if (!sum) {
        cout << count << "\n";
        break;
      }
    }
  }
  return 0;
}


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

int32_t main() {
  // your code goes here
  int k, count = 0, sum = 0, i, j;
  cin >> k;
  if (k % 2 == 0) {
    cout << "-1\n";
  } else {
    while (1) {
      if (count > k) {
        cout << "-1\n";
        break;
      }
      sum = 10 * sum + 7;
      count++;
      sum %= k;
      if (!sum) {
        cout << count << "\n";
        break;
      }
    }
  }
  return 0;
}
Problem p02596 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using st = string;
using P = pair<int, int>;
using bl = bool;
using ch = char;
#define mrep(i, m, n) for (int i = m; i < (n); i++)
#define rep(i, n) mrep(i, 0, n)
#define chmax(x, y)                                                            \
  { x = max(x, y); }
#define chmin(x, y)                                                            \
  { x = min(x, y); }
#define INF 1e10

vector<int> a(INF);

int main() {
  int k;
  cin >> k;
  a.at(0) = 7 % k;
  mrep(i, 1, k) { a.at(i) = (a.at(i - 1) * 10 + 7) % k; }
  rep(i, k) {
    if (a.at(i) == 0) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  /*while(true){
      if(k % 2 == 0 || k % 5 == 0){
          ans = -1;
          break;
      }
      if(seven % k == 0){
          break;
      }
      seven = seven * 10 + 7;
      ans++;
  }*/
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using st = string;
using P = pair<int, int>;
using bl = bool;
using ch = char;
#define mrep(i, m, n) for (int i = m; i < (n); i++)
#define rep(i, n) mrep(i, 0, n)
#define chmax(x, y)                                                            \
  { x = max(x, y); }
#define chmin(x, y)                                                            \
  { x = min(x, y); }
#define INF 1e7

vector<int> a(INF);

int main() {
  int k;
  cin >> k;
  a.at(0) = 7 % k;
  mrep(i, 1, k) { a.at(i) = (a.at(i - 1) * 10 + 7) % k; }
  rep(i, k) {
    if (a.at(i) == 0) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  /*while(true){
      if(k % 2 == 0 || k % 5 == 0){
          ans = -1;
          break;
      }
      if(seven % k == 0){
          break;
      }
      seven = seven * 10 + 7;
      ans++;
  }*/
}
Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int k, c = 1;

  cin >> k;
  // vector<int> x(n), y(n);
  int s = 7;
  if (k % 2 == 0 && k % 5 == 5) {
    cout << -1 << endl;
    return 0;
  }
  while (s % k != 0) {
    s = s % k * 10 + 7;
    c = c + 1;
  }

  cout << c << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int k, c = 1;

  cin >> k;
  // vector<int> x(n), y(n);
  int s = 7;
  if (k % 2 == 0 || k % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }
  while (s % k != 0) {
    s = s % k * 10 + 7;
    c = c + 1;
  }

  cout << c << endl;

  return 0;
}
Problem p02596 (C++)

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

int main() {
  ll K, count = 1;
  cin >> K;
  ll mod = 7 % K;
  if (K % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  while (mod > 0) {
    mod = (mod * 10 + 7) % K;
    count++;
  }
  cout << count << endl;
}

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

int main() {
  ll K, count = 1;
  cin >> K;
  ll mod = 7 % K;
  if (K % 2 == 0 || K % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }
  while (mod > 0) {
    mod = (mod * 10 + 7) % K;
    count++;
  }
  cout << count << endl;
}
Problem p02596 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

using namespace std;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = 1000000000 + 7;
const double PI = acos(-1);

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

  if (K % 2 == 0) {
    cout << -1;
    return 0;
  }

  int num = 0;
  int k = 1;
  int count = 0;
  while (true) {
    ++count;
    num += 7 * k;
    num %= K;
    if (num == 0)
      break;
    k *= 10;
    k %= K;
  }
  cout << count;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

using namespace std;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = 1000000000 + 7;
const double PI = acos(-1);

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

  if (K % 2 == 0 || K % 5 == 0) {
    cout << -1;
    return 0;
  }

  int num = 0;
  int k = 1;
  int count = 0;
  while (true) {
    ++count;
    num += 7 * k;
    num %= K;
    if (num == 0)
      break;
    k *= 10;
    k %= K;
  }
  cout << count;
}
Problem p02596 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  ll K;
  cin >> K;
  if (K == 2 || K == 5) {
    cout << -1 << endl;
    return 0;
  }
  ll ten[3000000];
  ten[0] = 7;
  for (int i = 1; i < 3000000; i++) {
    ten[i] = (ten[i - 1] * 10) % K;
  }
  ll now = 0;
  ll ans = 0;
  while (0 == 0) {
    now += ten[ans];
    now %= K;
    ans++;
    if (now == 0) {
      cout << ans << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  ll K;
  cin >> K;
  if (K == 2 || K == 5) {
    cout << -1 << endl;
    return 0;
  }
  ll ten[3000000];
  ten[0] = 7;
  for (int i = 1; i < 3000000; i++) {
    ten[i] = (ten[i - 1] * 10) % K;
  }
  ll now = 0;
  ll ans = 0;
  for (ans = 0; ans < 3000000;) {
    now += ten[ans];
    now %= K;
    ans++;
    if (now == 0) {
      cout << ans << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p02596 (C++)

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

int main() {
  int k;
  cin >> k;
  if (k % 7 == 0)
    k /= 7;
  int ans = 0;
  int ini = 1;
  if (k % 2 == 0)
    ans = -1;
  else
    while (ini) {
      ans++;
      // cout<<ini<<" ";
      ini = ini % k;
      // cout<<ini<<" ";
      if (ini == 0)
        break;
      ini *= 10;
      ini++;
    }
  cout << ans;
  return 0;
}


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

int main() {
  int k;
  cin >> k;
  if (k % 7 == 0)
    k /= 7;
  int ans = 0;
  int ini = 1;
  if (k % 2 == 0 || k % 5 == 0)
    ans = -1;
  else
    while (ini) {
      ans++;
      // cout<<ini<<" ";
      ini = ini % k;
      // cout<<ini<<" ";
      if (ini == 0)
        break;
      ini *= 10;
      ini++;
    }
  cout << ans;
  return 0;
}
Problem p02596 (C++)

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

#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = n - 1; i <= 0; --i)
#define loop while (true)
typedef long long ll;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
const ll MAX = 10000000;
const ll MIN = 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;
}
using pint = pair<int, int>;
using pstring = pair<int, string>;

int main() {
  ll N;
  cin >> N;
  if (N % 2 == 0 || N % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  ll ans = 1;
  ll mod = 7 % N;

  while (mod % N) {
    mod *= 10;
    mod += 7;
    mod %= N;
    ans++;
  }

  cout << ans << endl;
  return 0;
}


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

#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = n - 1; i <= 0; --i)
#define loop while (true)
typedef long long ll;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
const ll MAX = 10000000;
const ll MIN = 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;
}
using pint = pair<int, int>;
using pstring = pair<int, string>;

int main() {
  ll N;
  cin >> N;
  if (N % 2 == 0 || N % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }
  ll ans = 1;
  ll mod = 7 % N;

  while (mod % N) {
    mod *= 10;
    mod += 7;
    mod %= N;
    ans++;
  }

  cout << ans << endl;
  return 0;
}
Problem p02596 (C++)

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

int main() {
  ll K;
  cin >> K;

  if (K % 7 == 0) {
    K /= 7;
  }
  ll L = 9 * K;

  ll am = 1;
  ll ans;
  bool a = false;
  for (ll i = 1; i < 1000000000; i++) {
    am *= 10;
    if (am % L == 1) {
      a = true;
      ans = i;
      break;
    }
    am %= L;
  }

  if (a) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}


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

int main() {
  ll K;
  cin >> K;

  if (K % 7 == 0) {
    K /= 7;
  }
  ll L = 9 * K;
  if (L % 2 == 0 || L % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }

  ll am = 1;
  ll ans;
  bool a = false;
  for (ll i = 1; i < 1000000000; i++) {
    am *= 10;
    if (am % L == 1) {
      a = true;
      ans = i;
      break;
    }
    am %= L;
  }

  if (a) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p02597 (C++)

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

int main() {
  int n, ans = 0;
  string c;
  cin >> n >> c;
  priority_queue<int> r;
  vector<int> w;
  for (auto i = 0; i < n; i++) {
    if (c[i] == 'R')
      r.push(i);
    else
      w.push_back(i);
  }
  for (auto i = 0; i < (int)w.size(); i++) {
    if (r.top() > w[i]) {
      auto tmp = r.top();
      r.pop();
      c[w[i]] = 'R';
      c[tmp] = 'W';
      r.push(w[i]);
      w[i] = tmp;
      ans++;
    }
    //        cout << c << endl;
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n, ans = 0;
  string c;
  cin >> n >> c;
  priority_queue<int> r;
  vector<int> w;
  for (auto i = 0; i < n; i++) {
    if (c[i] == 'R')
      r.push(i);
    else
      w.push_back(i);
  }
  if (r.empty() or w.empty()) {
    cout << 0 << endl;
    return 0;
  }
  for (auto i = 0; i < (int)w.size(); i++) {
    if (r.top() > w[i]) {
      auto tmp = r.top();
      r.pop();
      c[w[i]] = 'R';
      c[tmp] = 'W';
      r.push(w[i]);
      w[i] = tmp;
      ans++;
    }
    //        cout << c << endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
c = input()

d = list(c)

i = 0
j = N - 1
result = 0
while i != j:
    if d[i] == "W":
        while d[j] != "R" and i != j:
            j -= 1
        d[i] = "R"
        d[j] = "W"
        result += 1
    i += 1
print(result)


Fixed submission:
N = int(input())
c = input()

d = list(c)

i = 0
j = N - 1
result = 0
while True:
    while i < N and d[i] != "W":
        i += 1
    while j > 0 and d[j] != "R":
        j -= 1
    if i == N or j == -1 or i >= j:
        break
    d[i] = "R"
    d[j] = "W"
    result += 1
print(result)
Problem p02597 (C++)

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

int main(void) {
  ll n, r, w;
  cin >> n;
  vector<char> vec(n);
  rep(i, n) {
    cin >> vec.at(i);
    if (vec.at(i) == 'R')
      r++;
    else if (vec.at(i) == 'W')
      w++;
  }
  ll ans1 = 0, ans2 = 0;
  rep(i, r) {
    if (vec.at(i) == 'W')
      ans1++;
  }
  for (int i = r; i < n; i++) {
    if (vec.at(i) == 'R')
      ans2++;
  }
  ll ans = max(ans1, ans2);
  cout << ans << endl;

  return 0;
}


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

int main(void) {
  ll n, r = 0, w = 0;
  cin >> n;
  vector<char> vec(n);
  rep(i, n) {
    cin >> vec.at(i);
    if (vec.at(i) == 'R')
      r++;
    else if (vec.at(i) == 'W')
      w++;
  }
  ll ans1 = 0, ans2 = 0;
  rep(i, r) {
    if (vec.at(i) == 'W')
      ans1++;
  }
  for (int i = r; i < n; i++) {
    if (vec.at(i) == 'R')
      ans2++;
  }
  ll ans = max(ans1, ans2);
  cout << ans << endl;

  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF long long int(1e18 + 1)
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<char> a(n);
  vector<int> w, r;
  rep(i, 0, n) {
    cin >> a[i];
    if (a[i] == 'W')
      w.push_back(i);
    else
      r.push_back(i);
  }
  reverse(r.begin(), r.end());
  int ans = 0;
  int W, R;
  W = w.size();
  R = r.size();
  if (W && R) {
    for (int i = 0; i < W || i < R; i++) {
      if (w[i] < r[i])
        ans++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF long long int(1e18 + 1)
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<char> a(n);
  vector<int> w, r;
  rep(i, 0, n) {
    cin >> a[i];
    if (a[i] == 'W')
      w.push_back(i);
    else
      r.push_back(i);
  }
  reverse(r.begin(), r.end());
  int ans = 0;
  int W, R;
  W = w.size();
  R = r.size();
  if (W && R) {
    for (int i = 0; i < W && i < R; i++) {
      if (w[i] < r[i])
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

int64_t solve(const int N, string c) {
  char RED = 'R';
  char WHITE = 'W';
  int r = N - 1;
  int count = 0;

  REP(l, N) {
    if (c.at(l) == RED)
      continue;

    while (c.at(r) == WHITE && r > -1)
      --r;

    if (l >= r)
      break;

    c.at(l) = RED;
    c.at(r) = WHITE;
    ++count;
  }

  return count;
}

// generated by online-judge-template-generator v4.4.0
// (https://github.com/kmyk/online-judge-template-generator)
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  constexpr char endl = '\n';
  int N;
  cin >> N;
  string c;
  cin >> c;
  auto ans = solve(N, c);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

int64_t solve(const int N, string c) {
  char RED = 'R';
  char WHITE = 'W';
  int r = N - 1;
  int count = 0;

  REP(l, N) {
    if (c.at(l) == RED)
      continue;

    while (c.at(r) == WHITE && r > 0)
      --r;

    if (l >= r)
      break;

    c.at(l) = RED;
    c.at(r) = WHITE;
    ++count;
  }

  return count;
}

// generated by online-judge-template-generator v4.4.0
// (https://github.com/kmyk/online-judge-template-generator)
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  constexpr char endl = '\n';
  int N;
  cin >> N;
  string c;
  cin >> c;
  auto ans = solve(N, c);
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define eps 0.000001;
using namespace std;
typedef long long ll;
const int inf = 0xfff;
const int maxn = 200005;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
struct node {
  int x, y, d;
  node() { x = y = d = 0; };
};
int main() {
  int n;
  cin >> n;
  string maze;
  cin >> maze;
  int i = 0, j = n - 1;
  int cnt = 0;
  while (i < j) {
    while (maze[i] != 'W')
      i++;
    while (maze[j] != 'R')
      j--;
    swap(maze[i], maze[j]);
    i++;
    if (i > j)
      cnt++;
  }
  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define eps 0.000001;
using namespace std;
typedef long long ll;
const int inf = 0xfff;
const int maxn = 200005;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
struct node {
  int x, y, d;
  node() { x = y = d = 0; };
};
int main() {
  int n;
  cin >> n;
  string maze;
  cin >> maze;
  int i = 0, j = n - 1;
  int cnt = 0;
  for (int l = 0, r = n - 1; l < r;) {
    if (maze[l] == 'W') {
      if (maze[r] == 'R') {
        swap(maze[l], maze[r]);
        cnt++;
        l++;
      }
      r--;
    } else {
      l++;
    }
  }
  cout << cnt << endl;
}
Problem p02597 (C++)

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0, j = n - 1, ans = 0;
  while (1) {
    while (i < j and s[i] == 'R')
      ++i;
    while (i < j and s[j] == 'W')
      ++j;
    if (i == j)
      break;
    swap(s[i], s[j]);
    ans++;
  }
  cout << ans << endl;
}

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0, j = n - 1, ans = 0;
  while (1) {
    while (i < j and s[i] == 'R')
      ++i;
    while (i < j and s[j] == 'W')
      --j;
    if (i == j)
      break;
    swap(s[i], s[j]);
    ans++;
  }
  cout << ans << endl;
}
Problem p02597 (C++)

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

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

  string c;
  cin >> c;

  vector<ll> rsumv(n, 0);
  if (c.at(0) == 'R')
    rsumv.at(0)++;

  for (int i = 1; i < n; i++) {
    if (c.at(i) == 'R') {
      rsumv.at(i) = rsumv.at(i - 1) + 1;
    } else
      rsumv.at(i) = rsumv.at(i - 1);
  }
  ll rsum = rsumv.at(n - 1);

  ll ans = rsum - rsumv.at(rsum - 1);

  cout << ans << endl;
}

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

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

  string c;
  cin >> c;

  vector<ll> rsumv(n, 0);
  if (c.at(0) == 'R')
    rsumv.at(0)++;

  for (int i = 1; i < n; i++) {
    if (c.at(i) == 'R') {
      rsumv.at(i) = rsumv.at(i - 1) + 1;
    } else
      rsumv.at(i) = rsumv.at(i - 1);
  }
  ll rsum = rsumv.at(n - 1);

  ll ans = 0;
  if (rsum) {
    ans = rsum - rsumv.at(rsum - 1);
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e9;
typedef pair<int, int> pi;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  cin >> n >> s;
  vector<int> a, b;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      a.push_back(i);
    else
      b.push_back(i);
  }
  reverse(b.begin(), b.end());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i > a.size() - 1 || i > b.size() - 1) {
      break;
    }
    if (a[i] > b[i])
      break;
    ;
    ans++;
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e9;
typedef pair<int, int> pi;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  cin >> n >> s;
  vector<int> a, b;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      a.push_back(i);
    else
      b.push_back(i);
  }
  reverse(b.begin(), b.end());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i > ((int)a.size() - 1) || i > ((int)b.size()) - 1) {
      break;
    }
    if (a[i] > b[i])
      break;
    ;
    ans++;
  }
  cout << ans;
}
Problem p02597 (C++)

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

// solution:
signed main() {
  int n, count = n, moves = 0;
  cin >> n;
  string s;
  cin >> s;
  for (int i = n - 1; ~i; --i)
    count -= (s[i] == 'W');
  for (int i = 0; i < count; ++i)
    moves += (s[i] == 'W');
  cout << moves << "\n";
  return 0;
} // farewell, until we meet again..


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

// solution:
signed main() {
  int n, count, moves = 0;
  cin >> n, count = n;
  string s;
  cin >> s;
  for (int i = n - 1; ~i; --i)
    count -= (s[i] == 'W');
  for (int i = 0; i < count; ++i)
    moves += (s[i] == 'W');
  cout << moves << "\n";
  return 0;
} // farewell, until we meet again..
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
char a[10010];
int main() {
  int n, rz = 0, r = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 'R')
      rz++;
  }
  for (int i = 1; i <= rz; i++)
    if (a[i] == 'R')
      r++;
  cout << rz - r;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
char a[200010];
int main() {
  int n, rz = 0, r = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 'R')
      rz++;
  }
  for (int i = 1; i <= rz; i++)
    if (a[i] == 'R')
      r++;
  cout << rz - r;
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define IO                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define int long long
#define endl "\n"
#define all(x) (x).begin(), (x).end()
const int MOD = 1e9 + 7;
const int mxN = 2e5 + 3;

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int st = 0;
  int en = n - 1;
  while (s[st] == 'R') {
    st++;
  }
  while (s[en] == 'W') {
    en--;
  }
  int ans = 0;
  while (st < en) {
    if (s[st] == 'W' and s[en] == 'R')
      ans++, st++, en--;
    else if (s[st] == 'R' and s[en] == 'R')
      st++;
    else if (s[st] == 'W' and s[en] == 'W')
      en--;
  }
  cout << ans << endl;
}

int32_t main() {
  IO;

  int nT = 1;
  // cin >> nT;
  for (int i = 1; i <= nT; ++i)
    solve();
  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define IO                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define int long long
#define endl "\n"
#define all(x) (x).begin(), (x).end()
const int MOD = 1e9 + 7;
const int mxN = 2e5 + 3;

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int st = 0;
  int en = n - 1;
  while (s[st] == 'R') {
    st++;
  }
  while (s[en] == 'W') {
    en--;
  }
  int ans = 0;
  while (st < en) {
    if (s[st] == 'W' and s[en] == 'R')
      ans++, st++, en--;
    else if (s[st] == 'R' and s[en] == 'R')
      st++;
    else if (s[st] == 'W' and s[en] == 'W')
      en--;
    else
      st++, en--;
  }
  cout << ans << endl;
}

int32_t main() {
  IO;

  int nT = 1;
  // cin >> nT;
  for (int i = 1; i <= nT; ++i)
    solve();
  return 0;
}
Problem p02597 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
s = input()
count = 0

while "WR" in s:
    w_i = s.find("W")
    r_i = s.rfind("R")
    s = s[:w_i] + "R" + s[w_i + 1 : r_i] + "W" + s[r_i + 1 :]
    count += 1

print(count)


Fixed submission:
N = int(input())
s = input()
r_count = s.count("R")
new_s = s[:r_count]
print(new_s.count("W"))