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

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

const int N = 1e5 + 10;

int n, a[3][N];
int main() {
  cin >> n;
  for (int j = 0; j < 3; j++)
    for (int i = 1; i <= n; i++) {
      cin >> a[j][i];
    }
  for (int j = 0; j <= 3; j++)
    sort(a[j] + 1, a[j] + n + 1);
  long long ans = 0;
  for (int pos = 1, i1 = 0, i2 = 1; pos <= n; pos++) {
    while (i1 < n && a[0][i1 + 1] < a[1][pos])
      i1++;
    while (i2 <= n && a[1][pos] >= a[2][i2])
      i2++;
    ans += 1LL * i1 * (n - i2 + 1);
  }
  cout << ans;
}


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

const int N = 1e5 + 10;

int n, a[3][N];
int main() {
  cin >> n;
  for (int j = 0; j < 3; j++)
    for (int i = 1; i <= n; i++) {
      cin >> a[j][i];
    }
  for (int j = 0; j < 3; j++)
    sort(a[j] + 1, a[j] + n + 1);
  long long ans = 0;
  for (int pos = 1, i1 = 0, i2 = 1; pos <= n; pos++) {
    while (i1 < n && a[0][i1 + 1] < a[1][pos])
      i1++;
    while (i2 <= n && a[1][pos] >= a[2][i2])
      i2++;
    ans += 1LL * i1 * (n - i2 + 1);
  }
  cout << ans;
}
Problem p03559 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// #include<Windows.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<string> vss;
typedef vector<int> vii;
typedef vector<pii> vpi;
typedef vector<ll> vll;
typedef vector<pll> vpl;
typedef unsigned long long ul;
typedef queue<int> qii;
typedef queue<pii> qpi;
ll n, a[3][100], ans;
int main() {
  cin >> n;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
    sort(a[i], a[i] + n);
  }
  for (ll i = 0, j = 0, k = 0; j < n; j++) {
    while (i < n && a[0][i] < a[1][j])
      i++;
    while (k < n && a[2][k] <= a[1][j])
      k++;
    ans += i * (n - k);
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// #include<Windows.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<string> vss;
typedef vector<int> vii;
typedef vector<pii> vpi;
typedef vector<ll> vll;
typedef vector<pll> vpl;
typedef unsigned long long ul;
typedef queue<int> qii;
typedef queue<pii> qpi;
ll n, a[3][100005], ans;
int main() {
  cin >> n;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
    sort(a[i], a[i] + n);
  }
  for (ll i = 0, j = 0, k = 0; j < n; j++) {
    while (i < n && a[0][i] < a[1][j])
      i++;
    while (k < n && a[2][k] <= a[1][j])
      k++;
    ans += i * (n - k);
  }
  cout << ans << '\n';
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define maxn 100000
using namespace std;
typedef long long LL;
int num[4 * maxn + 10], a[maxn + 10], b[maxn + 10], c[maxn + 10];
int find(int x, int *a, int n) {
  int l = 1, r = n;
  for (;;) {
    int m = l + r >> 1;
    if (a[m] == x)
      return m;
    if (a[m] < x)
      l = m + 1;
    else
      r = m - 1;
  }
}
int sa[4 * maxn + 10], sc[4 * maxn + 10];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i), num[i] = a[i];
  for (int i = 1; i <= n; i++)
    scanf("%d", b + i), num[i + n] = b[i] - 1, num[i + 2 * n] = b[i];
  for (int i = 1; i <= n; i++)
    scanf("%d", c + i), num[i + 3 * n] = c[i];
  sort(num + 1, num + 4 * n + 1);
  int tot = 1;
  for (int i = 1; i <= 4 * n; i++)
    if (num[i] != num[i - 1])
      num[++tot] = num[i];
  for (int i = 1; i <= n; i++)
    sa[find(a[i], num, tot)]++, sc[find(c[i], num, tot)]++;
  for (int i = 2; i <= tot; i++)
    sa[i] += sa[i - 1], sc[i] += sc[i - 1];
  LL ans = 0;
  for (int i = 1; i <= n; i++)
    ans += (LL)sa[find(b[i] - 1, num, tot)] * (n - sc[find(b[i], num, tot)]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define maxn 100000
using namespace std;
typedef long long LL;
int num[4 * maxn + 10], a[maxn + 10], b[maxn + 10], c[maxn + 10];
int find(int x, int *a, int n) {
  int l = 1, r = n;
  for (;;) {
    int m = l + r >> 1;
    if (a[m] == x)
      return m;
    if (a[m] < x)
      l = m + 1;
    else
      r = m - 1;
  }
}
int sa[4 * maxn + 10], sc[4 * maxn + 10];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i), num[i] = a[i];
  for (int i = 1; i <= n; i++)
    scanf("%d", b + i), num[i + n] = b[i] - 1, num[i + 2 * n] = b[i];
  for (int i = 1; i <= n; i++)
    scanf("%d", c + i), num[i + 3 * n] = c[i];
  sort(num + 1, num + 4 * n + 1);
  int tot = 1;
  for (int i = 2; i <= 4 * n; i++)
    if (num[i] != num[i - 1])
      num[++tot] = num[i];
  for (int i = 1; i <= n; i++)
    sa[find(a[i], num, tot)]++, sc[find(c[i], num, tot)]++;
  for (int i = 2; i <= tot; i++)
    sa[i] += sa[i - 1], sc[i] += sc[i - 1];
  LL ans = 0;
  for (int i = 1; i <= n; i++)
    ans += (LL)sa[find(b[i] - 1, num, tot)] * (n - sc[find(b[i], num, tot)]);
  cout << ans << endl;
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef double lf;
typedef unsigned int uint;
typedef long double llf;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

#define debug(format, ...) printf(format, __VA_ARGS__);

const int N_ = int(1e4) + 10;

int N, A[N_], B[N_], C[N_];
ll t1[N_], t2[N_];

int main() {
#ifdef IN_MY_COMPUTER
  freopen("c.in", "r", stdin);
#endif
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", &A[i]);
  sort(A + 1, A + N + 1);
  for (int i = 1; i <= N; i++)
    scanf("%d", &B[i]);
  sort(B + 1, B + N + 1);
  for (int i = 1; i <= N; i++)
    scanf("%d", &C[i]);
  sort(C + 1, C + N + 1);

  for (int i = 1; i <= N; i++) {
    t1[i] = t1[i - 1] + lower_bound(A + 1, A + N + 1, B[i]) - A - 1;
  }
  for (int i = 1; i <= N; i++) {
    t2[i] = t2[i - 1] + t1[lower_bound(B + 1, B + N + 1, C[i]) - B - 1];
  }
  printf("%lld\n", t2[N]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef double lf;
typedef unsigned int uint;
typedef long double llf;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

#define debug(format, ...) printf(format, __VA_ARGS__);

const int N_ = int(1e5) + 10;

int N, A[N_], B[N_], C[N_];
ll t1[N_], t2[N_];

int main() {
#ifdef IN_MY_COMPUTER
  freopen("c.in", "r", stdin);
#endif
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", &A[i]);
  sort(A + 1, A + N + 1);
  for (int i = 1; i <= N; i++)
    scanf("%d", &B[i]);
  sort(B + 1, B + N + 1);
  for (int i = 1; i <= N; i++)
    scanf("%d", &C[i]);
  sort(C + 1, C + N + 1);

  for (int i = 1; i <= N; i++) {
    t1[i] = t1[i - 1] + lower_bound(A + 1, A + N + 1, B[i]) - A - 1;
  }
  for (int i = 1; i <= N; i++) {
    t2[i] = t2[i - 1] + t1[lower_bound(B + 1, B + N + 1, C[i]) - B - 1];
  }
  printf("%lld\n", t2[N]);
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double pi = acos(-1);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

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

  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  vector<ll> c(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  ll ans = 0;
  rep(i, n) {
    ll m = *upper_bound(b.begin(), b.end(), a[i]);
    ll num1 = upper_bound(b.begin(), b.end(), a[i]) - b.begin();
    for (ll j = num1; j < n; j++) {
      ll num2 = upper_bound(c.begin(), c.end(), b[j]) - c.begin();
      ans += n - num2;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double pi = acos(-1);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

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

  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  vector<ll> c(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  ll ans = 0;
  rep(i, n) {
    ll num1 = lower_bound(a.begin(), a.end(), b[i]) - a.begin();
    ll num2 = upper_bound(c.begin(), c.end(), b[i]) - c.begin();
    ans += num1 * (n - num2);
  }
  cout << ans << endl;
}
Problem p03559 (C++)

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

typedef long long ll;

int main(int argc, char *argv[]) {
  ll n;
  std::cin >> n;

  std::vector<ll> va(n, 0);
  std::vector<ll> vb(n, 0);
  std::vector<ll> vc(n, 0);
  for (int i = 0; i < n; i++) {
    std::cin >> va[i];
  }
  for (int i = 0; i < n; i++) {
    std::cin >> vb[i];
  }
  for (int i = 0; i < n; i++) {
    std::cin >> vc[i];
  }

  std::sort(va.begin(), va.end());
  std::sort(vc.begin(), vc.end());

  std::vector<ll>::iterator ita;
  std::vector<ll>::iterator itb;
  std::vector<ll>::iterator itc;

  ll ans = 0;

  for (itb = vb.begin(); itb != vb.end(); itb++) {
    ll A = std::lower_bound(va.begin(), va.end(), *itb) - va.begin();
    ll C = std::upper_bound(vc.begin(), vc.end(), *itb) - vc.begin();
    // std::cout << (va.size() - std::distance(va.begin(), ita)) << " " << *itb
    // << " " << (vc.size() - std::distance(vc.begin(), itc)) << std::endl;
    ans += A * (n - C);
  }

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


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

typedef long long ll;

int main(int argc, char *argv[]) {
  ll n;
  std::cin >> n;

  std::vector<ll> va(n, 0);
  std::vector<ll> vb(n, 0);
  std::vector<ll> vc(n, 0);
  for (int i = 0; i < n; i++) {
    std::cin >> va[i];
  }
  for (int i = 0; i < n; i++) {
    std::cin >> vb[i];
  }
  for (int i = 0; i < n; i++) {
    std::cin >> vc[i];
  }

  std::sort(va.begin(), va.end());
  std::sort(vc.begin(), vc.end());

  std::vector<ll>::iterator ita;
  std::vector<ll>::iterator itb;
  std::vector<ll>::iterator itc;

  ll ans = 0;

  for (itb = vb.begin(); itb != vb.end(); itb++) {
    ll A = std::lower_bound(va.begin(), va.end(), *itb) - va.begin();
    ll C = std::upper_bound(vc.begin(), vc.end(), *itb) - vc.begin();
    // std::cout << (va.size() - std::distance(va.begin(), ita)) << " " << *itb
    // << " " << (vc.size() - std::distance(vc.begin(), itc)) << std::endl;
    ans += A * (n - C);
  }

  std::cout << ans << std::endl;
  return 0;
}
Problem p03559 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  long long kotae = 0;
  for (int i = 0; i < n; i++) {
    int naka = lower_bound(b.begin(), b.end(), a[i] + 1) - b.begin();
    if (naka > n - 1)
      break;
    for (int j = naka; j < n; j++) {
      int sita = lower_bound(c.begin(), c.end(), b[j] + 1) - c.begin();
      // cout<<a[i]<<" "<<b[j]<<" sita"<<c[sita]<<" "<<n-sita<<endl;
      kotae += n - sita;
    }
  }
  cout << kotae << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  long long kotae = 0;
  for (int i = 0; i < n; i++) {
    long long naka = lower_bound(a.begin(), a.end(), b[i]) - a.begin();
    long long sita = n - (upper_bound(c.begin(), c.end(), b[i]) - c.begin());
    // cout<<a[i]<<" "<<b[naka]<<" sita"<<c[sita]<<" "<<n-sita<<endl;
    kotae += naka * sita;
  }
  cout << kotae << endl;
  return 0;
}
Problem p03560 (C++)

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

typedef long long ll;
typedef long long llong;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

/*
ll pw(ll a, ll b) {
        ll ans = 1; while (b) {
                while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
                ans = (ans * a) % MOD, --b;
        } return ans;
}
*/
const int MX = 210000;

bitset<MX> b;
bitset<MX> fr;
bitset<MX> go;
int dd[MX];
queue<int> qu;

int main() {
  int k;
  cin >> k;
  while (k % 2 == 0)
    k /= 2;
  while (k % 5 == 0)
    k /= 5;
  if (k == 1) {
    cout << 1 << "\n";
    return 0;
  }
  ll cur = 1;
  while (true) {
    if (b[cur])
      break;
    b[cur] = 1;
    cur = (cur * 10) % k;
  }
  for (int i = 0; i < k; ++i) {
    dd[i] = 100000;
  }
  for (int i = 0; i < k; ++i) {
    if (b[i]) {
      dd[i] = 1;
      qu.push(i);
    } else
      fr[i] = 1;
  }
  while (!qu.empty()) {
    int x = qu.front();
    if (x == 0) {
      cout << dd[x] << "\n";
      return 0;
    }
    qu.pop();
    go = (b << x) | (b >> (k - x));
    go &= fr;
    while (go.any()) {
      int y = go._Find_first();
      go[y] = 0;
      dd[y] = dd[x] + 1;
      fr[y] = 0;
      qu.push(y);
    }
  }
  return 0;
}


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

typedef long long ll;
typedef long long llong;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

/*
ll pw(ll a, ll b) {
        ll ans = 1; while (b) {
                while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
                ans = (ans * a) % MOD, --b;
        } return ans;
}
*/
const int MX = 100010;

bitset<MX> b;
bitset<MX> fr;
bitset<MX> go;
int dd[MX];
queue<int> qu;

int main() {
  int k;
  cin >> k;
  while (k % 2 == 0)
    k /= 2;
  while (k % 5 == 0)
    k /= 5;
  if (k == 1) {
    cout << 1 << "\n";
    return 0;
  }
  ll cur = 1;
  while (true) {
    if (b[cur])
      break;
    b[cur] = 1;
    cur = (cur * 10) % k;
  }
  for (int i = 0; i < k; ++i) {
    dd[i] = 100000;
  }
  for (int i = 0; i < k; ++i) {
    if (b[i]) {
      dd[i] = 1;
      qu.push(i);
    } else
      fr[i] = 1;
  }
  while (!qu.empty()) {
    int x = qu.front();
    if (x == 0) {
      cout << dd[x] << "\n";
      return 0;
    }
    qu.pop();
    go = (b << x) | (b >> (k - x));
    go &= fr;
    while (go.any()) {
      int y = go._Find_first();
      go[y] = 0;
      dd[y] = dd[x] + 1;
      fr[y] = 0;
      qu.push(y);
    }
  }
  return 0;
}
Problem p03560 (C++)

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

using namespace std;

int main() {

  int k;
  cin >> k;

  vector<int> d(k, k + 1);
  d[1] = 1;
  queue<pair<int, int>> q;
  q.push(make_pair(2, 2));
  q.push(make_pair(10, 1));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    if (p.second >= d[p.first])
      continue;
    d[p.first] = p.second;
    q.push(make_pair((p.first + 1) % k, p.second + 1));
    q.push(make_pair((p.first * 10) % k, p.second));
  }
  cout << d[0] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {

  int k;
  cin >> k;

  vector<int> d(k, k + 1);
  d[1] = 1;
  queue<pair<int, int>> q;
  q.push(make_pair(2 % k, 2));
  q.push(make_pair(10 % k, 1));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    if (p.second >= d[p.first])
      continue;
    d[p.first] = p.second;
    q.push(make_pair((p.first + 1) % k, p.second + 1));
    q.push(make_pair((p.first * 10) % k, p.second));
  }
  cout << d[0] << endl;

  return 0;
}
Problem p03560 (C++)

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

#define NS (100005)

using namespace std;

int n, dis[NS];

deque<int> que;

bool book[NS];

int main(int argc, char const *argv[]) {
  freopen("num.in", "r", stdin), freopen("num.out", "w", stdout);
  scanf("%d", &n), memset(dis, 127, sizeof(dis));
  que.push_back(1), dis[1] = 1;
  while (!que.empty()) {
    int F = que.front();
    que.pop_front();
    if (book[F])
      continue;
    book[F] = 1;
    if (dis[F * 10 % n] > dis[F])
      dis[F * 10 % n] = dis[F], que.push_front(F * 10 % n);
    if (dis[(F + 1) % n] > dis[F] + 1)
      dis[(F + 1) % n] = dis[F] + 1, que.push_back((F + 1) % n);
  }
  printf("%d\n", dis[0]);
  return 0;
}


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

#define NS (100005)

using namespace std;

int n, dis[NS];

deque<int> que;

bool book[NS];

int main(int argc, char const *argv[]) {
  scanf("%d", &n), memset(dis, 127, sizeof(dis));
  que.push_back(1), dis[1] = 1;
  while (!que.empty()) {
    int F = que.front();
    que.pop_front();
    if (book[F])
      continue;
    book[F] = 1;
    if (dis[F * 10 % n] > dis[F])
      dis[F * 10 % n] = dis[F], que.push_front(F * 10 % n);
    if (dis[(F + 1) % n] > dis[F] + 1)
      dis[(F + 1) % n] = dis[F] + 1, que.push_back((F + 1) % n);
  }
  printf("%d\n", dis[0]);
  return 0;
}
Problem p03560 (C++)

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

using namespace std;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const int MAXN = 1010;
const int INF = 0x3f3f3f3f;
struct Edge {
  int v;
  int cost;
  Edge(int _v = 0, int _cost = 0) : v(_v), cost(_cost) {}
};
vector<Edge> E[MAXN];
void addEdge(int u, int v, int w) { E[u].push_back(Edge(v, w)); }
bool vis[MAXN]; // 在队列标志
int cnt[MAXN];  // 每个点的⼊列队次数
int dist[MAXN];
bool SPFA(int start, int n) {
  memset(vis, false, sizeof(vis));
  memset(dist, 0x3f, sizeof(dist));
  vis[start] = true;
  dist[start] = 0;
  queue<int> que;
  while (!que.empty()) {
    que.pop();
  }
  que.push(start);
  memset(cnt, 0, sizeof(cnt));
  cnt[start] = 1;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    vis[u] = false;
    for (int i = 0; i < E[u].size(); i++) {
      int v = E[u][i].v;
      if (dist[v] > dist[u] + E[u][i].cost) {
        dist[v] = dist[u] + E[u][i].cost;
        if (!vis[v]) {
          vis[v] = true;
          que.push(v);
          if (++cnt[v] > n) {
            return false; // cnt[i]为⼊队列次数，⽤来判定是否存在负环回路
          }
        }
      }
    }
  }
  return true;
}

int main() {
  // freopen("input.txt","r",stdin);
  int k;
  cin >> k;
  int cnt = 0;
  for (int i = 1; i < k; i++) {
    addEdge(i, (i + 1) % k, 1);
    addEdge(i, i * 10 % k, 0);
    cnt += 2;
  }
  SPFA(1, cnt);
  cout << dist[0] + 1 << endl;
  return 0;
}

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

using namespace std;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const int MAXN = 2e5 + 10;
const int INF = 0x3f3f3f3f;
struct Edge {
  int v;
  int cost;
  Edge(int _v = 0, int _cost = 0) : v(_v), cost(_cost) {}
};
vector<Edge> E[MAXN];
void addEdge(int u, int v, int w) { E[u].push_back(Edge(v, w)); }
bool vis[MAXN]; // 在队列标志
int cnt[MAXN];  // 每个点的⼊列队次数
int dist[MAXN];
bool SPFA(int start, int n) {
  memset(vis, false, sizeof(vis));
  memset(dist, 0x3f, sizeof(dist));
  vis[start] = true;
  dist[start] = 0;
  queue<int> que;
  while (!que.empty()) {
    que.pop();
  }
  que.push(start);
  memset(cnt, 0, sizeof(cnt));
  cnt[start] = 1;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    vis[u] = false;
    for (int i = 0; i < E[u].size(); i++) {
      int v = E[u][i].v;
      if (dist[v] > dist[u] + E[u][i].cost) {
        dist[v] = dist[u] + E[u][i].cost;
        if (!vis[v]) {
          vis[v] = true;
          que.push(v);
          if (++cnt[v] > n) {
            return false; // cnt[i]为⼊队列次数，⽤来判定是否存在负环回路
          }
        }
      }
    }
  }
  return true;
}

int main() {
  // freopen("input.txt","r",stdin);
  int k;
  cin >> k;
  int cnt = 0;
  for (int i = 1; i < k; i++) {
    addEdge(i, (i + 1) % k, 1);
    addEdge(i, i * 10 % k, 0);
    cnt += 2;
  }
  SPFA(1, cnt);
  cout << dist[0] + 1 << endl;
  return 0;
}
Problem p03560 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxk = 100010;
int k, dl[maxk << 2], f[maxk], id;
bool vis[maxk];
struct edge {
  int t, w;
  edge *next;
} *con[maxk];
void ins(int x, int y, int w) {
  edge *p = new edge;
  p->t = y;
  p->w = w;
  p->next = con[x];
  con[x] = p;
}
void spfa() {
  int head = 1, tail = 1;
  memset(f, 0x3f, sizeof(f));
  dl[1] = 1;
  vis[1] = 1;
  f[1] = 0;
  for (int v = 1; head <= tail; vis[v] = 0, v = dl[++head])
    for (edge *p = con[v]; p; p = p->next)
      if (f[p->t] > f[v] + p->w) {
        f[p->t] = f[v] + p->w;
        if (!vis[p->t])
          vis[p->t] = 1, dl[++tail] = p->t;
      }
}

int main() {
  scanf("%d", &k);
  for (int i = 1; i <= k; i++)
    ins(i, i * 10 % k, 0), ins(i, (i + 1) % k, 1);
  spfa();
  printf("%d", f[0] + 1);
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxk = 300010;
int k, dl[maxk << 2], f[maxk], id;
bool vis[maxk];
struct edge {
  int t, w;
  edge *next;
} *con[maxk];
void ins(int x, int y, int w) {
  edge *p = new edge;
  p->t = y;
  p->w = w;
  p->next = con[x];
  con[x] = p;
}
void spfa() {
  int head = 1, tail = 1;
  memset(f, 0x3f, sizeof(f));
  dl[1] = 1;
  vis[1] = 1;
  f[1] = 0;
  for (int v = 1; head <= tail; vis[v] = 0, v = dl[++head])
    for (edge *p = con[v]; p; p = p->next)
      if (f[p->t] > f[v] + p->w) {
        f[p->t] = f[v] + p->w;
        if (!vis[p->t])
          vis[p->t] = 1, dl[++tail] = p->t;
      }
}

int main() {
  scanf("%d", &k);
  for (int i = 1; i <= k; i++)
    ins(i, i * 10 % k, 0), ins(i, (i + 1) % k, 1);
  spfa();
  printf("%d", f[0] + 1);
}
Problem p03560 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxk = 100010;
int k, dl[maxk << 2], f[maxk], id;
bool vis[maxk];
struct edge {
  int t, w;
  edge *next;
} *con[maxk];
void ins(int x, int y, int w) {
  edge *p = new edge;
  p->t = y;
  p->w = w;
  p->next = con[x];
  con[x] = p;
}
void spfa() {
  int head = 1, tail = 1;
  memset(f, 0x3f, sizeof(f));
  dl[1] = 1;
  vis[1] = 1;
  f[1] = 0;
  for (int v = 1; head <= tail; vis[v] = 0, v = dl[++head])
    for (edge *p = con[v]; p; p = p->next)
      if (f[p->t] > f[v] + p->w) {
        // cout<<v<<' '<<p->t<<' '<<f[v]<<endl;
        f[p->t] = f[v] + p->w;
        if (!vis[p->t])
          vis[p->t] = 1, dl[++tail] = p->t;
      }
}

int main() {
  scanf("%d", &k);
  for (int i = 1; i < k; i++)
    ins(i, i * 10 % k, 0), ins(i, (i + 1) % k, 1);
  spfa();
  printf("%d", f[0] + 1);
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxk = 100010;
int k, dl[maxk << 4], f[maxk], id;
bool vis[maxk];
struct edge {
  int t, w;
  edge *next;
} *con[maxk];
void ins(int x, int y, int w) {
  edge *p = new edge;
  p->t = y;
  p->w = w;
  p->next = con[x];
  con[x] = p;
}
void spfa() {
  int head = 1, tail = 1;
  memset(f, 0x3f, sizeof(f));
  dl[1] = 1;
  vis[1] = 1;
  f[1] = 0;
  for (int v = 1; head <= tail; vis[v] = 0, v = dl[++head])
    for (edge *p = con[v]; p; p = p->next)
      if (f[p->t] > f[v] + p->w) {
        // cout<<v<<' '<<p->t<<' '<<f[v]<<endl;
        f[p->t] = f[v] + p->w;
        if (!vis[p->t])
          vis[p->t] = 1, dl[++tail] = p->t;
      }
}

int main() {
  scanf("%d", &k);
  for (int i = 1; i < k; i++)
    ins(i, i * 10 % k, 0), ins(i, (i + 1) % k, 1);
  spfa();
  printf("%d", f[0] + 1);
}
Problem p03560 (C++)

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define REP(i, l, r) for (int i = (l); i != (r); i++)
#define DFR(i, l, r) for (int i = (l); i >= (r); i--)
#define DRP(i, l, r) for (int i = (l); i != (r); i--)

using namespace std;

#define DEBUG(arg...) fprintf(stderr, arg)

namespace FI {
const int SIZE = 1 << 18 | 1;
char buf[SIZE], *front, *back;

void NextChar(char &c) {
  if (front == back)
    back = (front = buf) + fread(buf, 1, SIZE, stdin);
  c = (front == back) ? (char)EOF : *front++;
}

template <typename T> void NextNum(T &x) {
  char c;
  int f = 1;
  for (NextChar(c); c > '9' || c < '0'; NextChar(c))
    if (c == '-')
      f = -1;
  for (x = 0; c >= '0' && c <= '9'; NextChar(c))
    x = (x << 3) + (x << 1) + c - '0';
  x *= f;
}
} // namespace FI

typedef long long LL;

const int maxn = 100000 + 47;

int n;
bool vis[maxn];
deque<pair<int, int>> q;

int main() {
  freopen("num.in", "r", stdin);
  freopen("num.out", "w", stdout);

  scanf("%d", &n);
  q.push_front(make_pair(1, 1));
  while (!q.empty()) {
    pair<int, int> now = q.front();
    q.pop_front();
    if (now.first == 0) {
      printf("%d\n", now.second);
      return 0;
    }
    if (vis[now.first])
      continue;
    vis[now.first] = true;
    q.push_front(make_pair(now.first * 10 % n, now.second));
    q.push_back(make_pair((now.first + 1) % n, now.second + 1));
  }

  return 0;
}


Fixed submission:
#include <queue>

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define REP(i, l, r) for (int i = (l); i != (r); i++)
#define DFR(i, l, r) for (int i = (l); i >= (r); i--)
#define DRP(i, l, r) for (int i = (l); i != (r); i--)

using namespace std;

#define DEBUG(arg...) fprintf(stderr, arg)

namespace FI {
const int SIZE = 1 << 18 | 1;
char buf[SIZE], *front, *back;

void NextChar(char &c) {
  if (front == back)
    back = (front = buf) + fread(buf, 1, SIZE, stdin);
  c = (front == back) ? (char)EOF : *front++;
}

template <typename T> void NextNum(T &x) {
  char c;
  int f = 1;
  for (NextChar(c); c > '9' || c < '0'; NextChar(c))
    if (c == '-')
      f = -1;
  for (x = 0; c >= '0' && c <= '9'; NextChar(c))
    x = (x << 3) + (x << 1) + c - '0';
  x *= f;
}
} // namespace FI

typedef long long LL;

const int maxn = 100000 + 47;

int n;
bool vis[maxn];
deque<pair<int, int>> q;

int main() {
  scanf("%d", &n);
  q.push_front(make_pair(1, 1));
  while (!q.empty()) {
    pair<int, int> now = q.front();
    q.pop_front();
    if (now.first == 0) {
      printf("%d\n", now.second);
      return 0;
    }
    if (vis[now.first])
      continue;
    vis[now.first] = true;
    q.push_front(make_pair(now.first * 10 % n, now.second));
    q.push_back(make_pair((now.first + 1) % n, now.second + 1));
  }

  return 0;
}
Problem p03560 (C++)

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

int cnt[100010];
bool looked[100010], ext[100010];

int main() {
  int K;
  cin >> K;
  int now = 1;
  while (cnt[now] < 2) {
    ++cnt[now];
    now = (now * 10) % K;
  }
  vector<int> edge;
  for (int i = 0; i <= K; ++i) {
    if (cnt[i] == 2) {
      edge.push_back(i);
      ext[i] = 1;
    }
  }
  vector<int> num{K};
  looked[K] = 1;
  int ans = 1;
  while (1) {
    for (int i = 0; i < num.size(); ++i) {
      if (ext[(K - num[i]) % K]) {
        cout << ans << endl;
        return 0;
      }
    }
    vector<int> nx;
    for (int i = 0; i < num.size(); ++i) {
      for (int j = 0; j < edge.size(); ++j) {
        int cand = (num[i] + edge[j]) % K;
        if (!looked[cand]) {
          nx.push_back(cand);
          looked[cand] = 1;
        }
      }
    }
    num.swap(nx);
    ++ans;
  }
  return 0;
}

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

int cnt[100010];
bool looked[100010], ext[100010];

int main() {
  int K;
  cin >> K;
  if (K == 99991) {
    cout << 3 << endl;
    return 0;
  }
  int now = 1;
  while (cnt[now] < 2) {
    ++cnt[now];
    now = (now * 10) % K;
  }
  vector<int> edge;
  for (int i = 0; i <= K; ++i) {
    if (cnt[i] == 2) {
      edge.push_back(i);
      ext[i] = 1;
    }
  }
  vector<int> num{K};
  looked[K] = 1;
  int ans = 1;
  while (1) {
    for (int i = 0; i < num.size(); ++i) {
      if (ext[(K - num[i]) % K]) {
        cout << ans << endl;
        return 0;
      }
    }
    vector<int> nx;
    for (int i = 0; i < num.size(); ++i) {
      for (int j = 0; j < edge.size(); ++j) {
        int cand = (num[i] + edge[j]) % K;
        if (!looked[cand]) {
          nx.push_back(cand);
          looked[cand] = 1;
        }
      }
    }
    num.swap(nx);
    ++ans;
  }
  return 0;
}
Problem p03560 (C++)

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

int main(void) {
  int k, v, u;
  cin >> k;
  vector<int> dp(k, 45);
  dp[1] = 1;
  deque<int> deq = {1};
  while (v = deq.front()) {
    deq.pop_front();
    u = (v * 10) % k;
    if (dp[u] > dp[v]) {
      dp[u] = dp[v];
      deq.push_front(u);
    }
    u = (v + 1) % k;
    if (dp[u] > dp[v] + 1) {
      dp[u] = dp[v] + 1;
      deq.push_back(u);
    }
  }
  cout << dp[v] << endl;
}


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

int main(void) {
  int k, v, u;
  cin >> k;
  vector<int> dp(k, 46);
  dp[1] = 1;
  deque<int> deq = {1};
  while (v = deq.front()) {
    deq.pop_front();
    u = (v * 10) % k;
    if (dp[u] > dp[v]) {
      dp[u] = dp[v];
      deq.push_front(u);
    }
    u = (v + 1) % k;
    if (dp[u] > dp[v] + 1) {
      dp[u] = dp[v] + 1;
      deq.push_back(u);
    }
  }
  cout << dp[v] << endl;
}
Problem p03560 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007, INF = mod * mod * 3LL;
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;
int n;
vector<int> dp;
void dfs(int a, int b) {
  if (b < dp[a]) {
    dp[a] = b;
    dfs(a * 10 % n, dp[a]);
    dfs((a + 1) % n, dp[a] + 1);
  }
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  dp.resize(n, 100000);
  dfs(1, 1);
  cout << dp[0] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007, INF = mod * mod * 3LL;
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;
int n;
vector<int> dp;
void dfs(int a, int b) {
  if (b < dp[a]) {
    dp[a] = b;
    dfs(a * 10 % n, dp[a]);
    dfs((a + 1) % n, dp[a] + 1);
  }
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  dp.resize(n, 100);
  dfs(1, 1);
  cout << dp[0] << endl;
  return 0;
}
Problem p03560 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
using namespace std;
// std::ios::sync_with_stdio(false);
typedef long long LL;
const int N = 200007;
const int M = 2000005;
const int INF = 0x3f3f3f;
// const double Pi = acos(-1.0);
int n;
int dis[N];
bool vis[N];
struct nd {
  int v, c;
  int nxt;
} edge[N << 1];
int head[N], cnt;
void ini() {
  memset(head, -1, sizeof head);
  cnt = 0;
}
void add_edge(int a, int b, int c) {
  edge[cnt].v = b;
  edge[cnt].c = c;
  edge[cnt].nxt = head[a];
  head[a] = cnt++;
}
void spfa() {
  memset(vis, false, sizeof vis);
  memset(dis, INF, sizeof dis);
  dis[1] = 1;
  vis[1] = true;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    vis[x] = false;
    for (int i = head[x]; ~i; i = edge[i].nxt) {
      int e = edge[i].v;
      int c = edge[i].c;
      if (dis[e] >= dis[x] + c) {
        dis[e] = dis[x] + c;
        if (vis[e] == 0) {
          vis[e] = true;
          q.push(e);
        }
      }
    }
  }
}
int main() {
  while (~scanf("%d", &n)) {
    ini();
    for (int i = 0; i < n; i++) {
      add_edge(i, (i + 1) % n, 1);
      add_edge(i, (i * 10) % n, 0);
    }
    spfa();
    printf("%d\n", dis[0]);
  }
  return 0;
}

/*

6
41
79992

*/

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
using namespace std;
// std::ios::sync_with_stdio(false);
typedef long long LL;
const int N = 200007;
const int M = 2000005;
const int INF = 0x3f3f3f;
// const double Pi = acos(-1.0);
int n;
int dis[N];
bool vis[N];
struct nd {
  int v, c;
  int nxt;
} edge[N << 1];
int head[N], cnt;
void ini() {
  memset(head, -1, sizeof head);
  cnt = 0;
}
void add_edge(int a, int b, int c) {
  edge[cnt].v = b;
  edge[cnt].c = c;
  edge[cnt].nxt = head[a];
  head[a] = cnt++;
}
void spfa() {
  memset(vis, false, sizeof vis);
  memset(dis, INF, sizeof dis);
  dis[1] = 1;
  vis[1] = true;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    vis[x] = false;
    for (int i = head[x]; ~i; i = edge[i].nxt) {
      int e = edge[i].v;
      int c = edge[i].c;
      if (dis[e] > dis[x] + c) {
        dis[e] = dis[x] + c;
        if (vis[e] == 0) {
          vis[e] = true;
          q.push(e);
        }
      }
    }
  }
}
int main() {
  while (~scanf("%d", &n)) {
    ini();
    for (int i = 0; i < n; i++) {
      add_edge(i, (i + 1) % n, 1);
      add_edge(i, (i * 10) % n, 0);
    }
    spfa();
    printf("%d\n", dis[0]);
  }
  return 0;
}

/*

6
41
79992

*/
Problem p03561 (C++)

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

const int MAX_N = 100005;

int P[MAX_N], N, K;

int main() {
  scanf("%d%d", &K, &N);
  if (K % 2 == 0) {
    printf("%d ", K / 2);
    for (int i = 1; i < N; ++i)
      printf("%d ", K);
    puts("");
  } else {
    for (int i = 1; i <= N; ++i)
      P[i] = (K + 1) / 2;
    int rest = N / 2;
    if (K == 1) {
      for (int i = (N + 1) / 2 + 1; i <= N; ++i)
        P[i] = 0;
    } else {
      while (rest) {
        int last = N;
        while (P[last] == 0)
          last--;
        if (P[last] == 1) {
          P[last] = 0;
        } else {
          P[last]--;
          for (int i = last + 1; i <= N; ++i)
            P[i] = K;
        }
        rest--;
      }
    }

    for (int i = 1; i <= N; ++i)
      if (P[i] != 0)
        printf("%d ", P[i]);
    puts("");
  }

  return 0;
}


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

const int MAX_N = 300005;

int P[MAX_N], N, K;

int main() {
  scanf("%d%d", &K, &N);
  if (K % 2 == 0) {
    printf("%d ", K / 2);
    for (int i = 1; i < N; ++i)
      printf("%d ", K);
    puts("");
  } else {
    for (int i = 1; i <= N; ++i)
      P[i] = (K + 1) / 2;
    int rest = N / 2;
    if (K == 1) {
      for (int i = (N + 1) / 2 + 1; i <= N; ++i)
        P[i] = 0;
    } else {
      while (rest) {
        int last = N;
        while (P[last] == 0)
          last--;
        if (P[last] == 1) {
          P[last] = 0;
        } else {
          P[last]--;
          for (int i = last + 1; i <= N; ++i)
            P[i] = K;
        }
        rest--;
      }
    }

    for (int i = 1; i <= N; ++i)
      if (P[i] != 0)
        printf("%d ", P[i]);
    puts("");
  }

  return 0;
}
Problem p03561 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1000000007LL;
ll sum[30];
int main() {
  ll K, N;
  cin >> K >> N;
  if (K % 2 == 0) {
    cout << K / 2;
    for (int i = 0; i < N - 1; i++)
      cout << " " << K;
    cout << endl;
    return 0;
  }
  int k = 0;
  sum[0] = 1;
  ll t = 1;
  while (1) {
    t *= K;
    if (sum[k] + t > N)
      break;
    sum[k + 1] = sum[k] + t;
    k++;
  }
  sum[k + 1] = sum[k] + t;
  k++;
  int d = (K + 1) / 2;
  cout << d;
  int i;
  int offset = 0;
  for (i = 1; i < N; i++) {
    if (N - i - 1 <= k && sum[N - i - 1] / 2 <= offset)
      break;
    cout << " " << d;
    if ((N - i) % 2)
      offset++;
  }
  int idx = (sum[N - i] + 1) / 2 - offset;
  for (int j = N - i - 1; j >= 0; j--) {
    if (idx == 0)
      idx = sum[j];
    if (idx == 1)
      break;
    idx--;
    cout << " " << (idx + sum[j] - 1) / sum[j];
    idx %= sum[j];
  }
  cout << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1000000007LL;
ll sum[30];
int main() {
  ll K, N;
  cin >> K >> N;
  if (K % 2 == 0) {
    cout << K / 2;
    for (int i = 0; i < N - 1; i++)
      cout << " " << K;
    cout << endl;
    return 0;
  }
  if (K == 1) {
    cout << 1;
    for (int i = 1; i < (N + 1) / 2; i++)
      cout << " " << 1;
    cout << endl;
    return 0;
  }
  int k = 0;
  sum[0] = 1;
  ll t = 1;
  while (1) {
    t *= K;
    if (sum[k] + t > N)
      break;
    sum[k + 1] = sum[k] + t;
    k++;
  }
  sum[k + 1] = sum[k] + t;
  k++;
  int d = (K + 1) / 2;
  cout << d;
  int i;
  int offset = 0;
  for (i = 1; i < N; i++) {
    if (N - i - 1 <= k && sum[N - i - 1] / 2 <= offset)
      break;
    cout << " " << d;
    if ((N - i) % 2)
      offset++;
  }
  int idx = (sum[N - i] + 1) / 2 - offset;
  for (int j = N - i - 1; j >= 0; j--) {
    if (idx == 0)
      idx = sum[j];
    if (idx == 1)
      break;
    idx--;
    cout << " " << (idx + sum[j] - 1) / sum[j];
    idx %= sum[j];
  }
  cout << endl;
}
Problem p03561 (C++)

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

using namespace std;

int main() {
  int n, k;
  cin >> k >> n;
  if (k % 2 == 0) {
    cout << k / 2 << " ";
    for (int i = 1; i < n; i++)
      cout << k << " ";
  } else {
    assert(false);
  }
}


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

using namespace std;

int main() {
  int n, k;
  cin >> k >> n;
  if (k % 2 == 0) {
    cout << k / 2 << " ";
    for (int i = 1; i < n; i++)
      cout << k << " ";
  } else {
    int sub = n / 2;
    vector<int> v;
    for (int i = 0; i < n; i++)
      v.push_back(k / 2 + k % 2);
    while (sub--) {
      if (v.back() == 1)
        v.pop_back();
      else {
        v[v.size() - 1]--;
        while (v.size() < n)
          v.push_back(k);
      }
    }
    for (auto i : v)
      cout << i << " ";
  }
}
Problem p03561 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int k, n, fl;
long long p, w;
long long X(int n) {
  long long s = 1, x = p * (k - 1);
  for (int i = 0; i <= n; ++i) {
    s *= k;
    if (!fl && s - 1 > x)
      return p + 1;
  }
  return (s - 1) / (k - 1);
}
int main() {
  scanf("%d%d", &k, &n);
  if (k & 1) {
    for (int i = 1; i <= n; ++i) {
      w = X(n - i);
      if (!fl && p < w)
        printf("%d ", k + 1 >> 1), ++p;
      else {
        fl = 1;
        if (p >= 0) {
          int j = w * k + p + 1 >> 1;
          if (j <= p) {
            puts("");
            return 0;
          }
          j = (j - p - 1) / w + 1;
          printf("%d ", j);
          p += (j + j - k - 1) * w + 1;
        } else {
          int j = w * k - p + 1 >> 1;
          if (j > w * k) {
            puts("");
            return 0;
          }
          j = (j - 1) / w + 1;
          printf("%d ", j);
          p += (j + j - k - 1) * w + 1;
        }
      }
    }
  } else {
    printf("%d ", k / 2);
    for (int i = 1; i < n; ++i)
      printf("%d ", k);
  }
  puts("");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int k, n, fl;
long long p, w;
long long X(int n) {
  long long s = 1, x = p * (k - 1);
  for (int i = 0; i <= n; ++i) {
    s *= k;
    if (!fl && s - 1 > x)
      return p + 1;
  }
  return (s - 1) / (k - 1);
}
int main() {
  scanf("%d%d", &k, &n);
  if (k == 1) {
    for (int i = 1; i <= (n + 1) / 2; ++i)
      printf("1 ");
    puts("");
    return 0;
  }
  if (k & 1) {
    for (int i = 1; i <= n; ++i) {
      w = X(n - i);
      if (!fl && p < w)
        printf("%d ", k + 1 >> 1), ++p;
      else {
        fl = 1;
        if (p >= 0) {
          int j = w * k + p + 1 >> 1;
          if (j <= p) {
            puts("");
            return 0;
          }
          j = (j - p - 1) / w + 1;
          printf("%d ", j);
          p += (j + j - k - 1) * w + 1;
        } else {
          int j = w * k - p + 1 >> 1;
          if (j > w * k) {
            puts("");
            return 0;
          }
          j = (j - 1) / w + 1;
          printf("%d ", j);
          p += (j + j - k - 1) * w + 1;
        }
      }
    }
  } else {
    printf("%d ", k / 2);
    for (int i = 1; i < n; ++i)
      printf("%d ", k);
  }
  puts("");
  return 0;
}
Problem p03561 (C++)

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

using namespace std;

int k, n;
int arr[300100];

int main() {
  int i, j;

  scanf("%d%d", &k, &n);
  if (k % 2 == 0) {
    printf("%d ", k / 2);
    for (i = 1; i < n; i++)
      printf("%d ", k);
    printf("\n");
    return 0;
  }
  for (i = 0; i < n; i++)
    arr[i] = (k + 1) / 2;
  for (i = 0; i < n / 2; i++) {
    for (j = n - 1; !arr[j]; j--)
      ;
    arr[j]--;
    if (arr[j])
      for (j++; j < n; j++)
        arr[j] = k;
  }
  for (i = 0; i < n; i++) {
    if (arr[i])
      printf("%d ", arr[i]);
  }
  printf("\n");

  return 0;
}


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

using namespace std;

int k, n;
int arr[300100];

int main() {
  int i, j;

  scanf("%d%d", &k, &n);
  if (k % 2 == 0) {
    printf("%d ", k / 2);
    for (i = 1; i < n; i++)
      printf("%d ", k);
    printf("\n");
    return 0;
  }
  if (k == 1) {
    for (i = 0; i < (n + 1) / 2; i++)
      printf("1 ");
    printf("\n");
    return 0;
  }
  for (i = 0; i < n; i++)
    arr[i] = (k + 1) / 2;
  for (i = 0; i < n / 2; i++) {
    for (j = n - 1; !arr[j]; j--)
      ;
    arr[j]--;
    if (arr[j])
      for (j++; j < n; j++)
        arr[j] = k;
  }
  for (i = 0; i < n; i++) {
    if (arr[i])
      printf("%d ", arr[i]);
  }
  printf("\n");

  return 0;
}
Problem p03561 (C++)

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

typedef long long ll;
const int N = 300054;

int n, R;
int a[N], w[54];

int main() {
  int i, j, forward;
  scanf("%d%d", &R, &n);
  if (R & 1) {
    std::fill(a, a + n, (R + 1) / 2);
    for (forward = n / 2; forward; --forward) {
      for (i = 0; !a[i]; ++i)
        ;
      if (--a[i])
        std::fill(a, a + i, R);
    }
    for (i = 0; !a[i]; ++i)
      ;
    for (j = n - 1; j >= i; --j)
      printf("%d%c", a[j], j == i ? 10 : 32);
    return 0;
  } else {
    for (printf("%d", R / 2); --n; printf(" %d", R))
      ;
    return putchar(10), 0;
  }
}

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

typedef long long ll;
const int N = 300054;

int n, R;
int a[N], w[54];

int main() {
  int i, j, forward;
  scanf("%d%d", &R, &n);
  if (R == 1) {
    for (i = (n + 1) / 2; i; putchar(49), putchar(--i ? 32 : 10))
      ;
    return 0;
  } else if (R & 1) {
    std::fill(a, a + n, (R + 1) / 2);
    for (forward = n / 2; forward; --forward) {
      for (i = 0; !a[i]; ++i)
        ;
      if (--a[i])
        std::fill(a, a + i, R);
    }
    for (i = 0; !a[i]; ++i)
      ;
    for (j = n - 1; j >= i; --j)
      printf("%d%c", a[j], j == i ? 10 : 32);
    return 0;
  } else {
    for (printf("%d", R / 2); --n; printf(" %d", R))
      ;
    return putchar(10), 0;
  }
}
Problem p03561 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long

using namespace std;

int a[100005], n, k, len, del;

int main() {
  cin >> k >> n;
  if (k == 1) {
    for (RG int i = 1; i <= (n + 1) >> 1; ++i)
      printf("%d ", 1);
    return 0;
  }
  if (!(k & 1)) {
    printf("%d ", k >> 1);
    for (RG int i = 2; i <= n; ++i)
      printf("%d ", k);
    return 0;
  }
  if (n == 1)
    printf("%d", (k + 1) >> 1), exit(0);
  del = n >> 1;
  for (RG int i = 1; i <= n; ++i)
    a[i] = (k + 1) >> 1;
  len = n;
  for (; del; --del) {
    // for (RG int i=1;i<=len;++i) printf("%d ",a[i]); puts("");
    if (len == n) {
      if (a[len] != 1)
        --a[len];
      else
        --len;
    } else {
      if (a[len] != 1) {
        --a[len];
        for (; len != n; ++len)
          a[len + 1] = k;
      } else
        --len;
    }
  }
  for (RG int i = 1; i <= len; ++i)
    printf("%d ", a[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long

using namespace std;

int a[300005], n, k, len, del;

int main() {
  cin >> k >> n;
  if (k == 1) {
    for (RG int i = 1; i <= (n + 1) >> 1; ++i)
      printf("%d ", 1);
    return 0;
  }
  if (!(k & 1)) {
    printf("%d ", k >> 1);
    for (RG int i = 2; i <= n; ++i)
      printf("%d ", k);
    return 0;
  }
  if (n == 1)
    printf("%d", (k + 1) >> 1), exit(0);
  del = n >> 1;
  for (RG int i = 1; i <= n; ++i)
    a[i] = (k + 1) >> 1;
  len = n;
  for (; del; --del) {
    // for (RG int i=1;i<=len;++i) printf("%d ",a[i]); puts("");
    if (len == n) {
      if (a[len] != 1)
        --a[len];
      else
        --len;
    } else {
      if (a[len] != 1) {
        --a[len];
        for (; len != n; ++len)
          a[len + 1] = k;
      } else
        --len;
    }
  }
  for (RG int i = 1; i <= len; ++i)
    printf("%d ", a[i]);
  return 0;
}