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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define endl '\n'
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define fast_io ios_base::sync_with_stdio(0);
#define show(x) cout << x << endl;
#define pret(x)                                                                \
  cout << x << endl;                                                           \
  return 0;
#define disp(x) cout << x << " ";
#define let(x, y) cout << x << " " << y << endl;
ll MOD = 1e9 + 7;
ll MAX = 9223372036854775807;

ll dp[101][9], fib[20];

int main() {
  fast_io;
  int h, w, k;
  cin >> h >> w >> k;
  fib[2] = 2, fib[3] = 3;
  fib[0] = 1, fib[1] = 1;
  fo(i, 4, 19) fib[i] = (fib[i - 1] + fib[i - 2]) % MOD;
  dp[h + 1][1] = 1;
  rf(i, h, 1) {
    // {
    // int j=2;
    fo(j, 1, w) {
      // show(dp[1][k]);
      dp[i][j] = (dp[i + 1][j] * ((fib[j - 1] * fib[w - j]) % MOD)) % MOD;
      // show(dp[1][k]);
      if (j + 1 <= w)
        dp[i][j] = (dp[i][j] +
                    dp[i + 1][j + 1] * ((fib[j - 1] * fib[w - j - 1]) % MOD)) %
                   MOD;
      // show(dp[1][k]);
      if (j - 1 > 0)
        dp[i][j] =
            (dp[i][j] + dp[i + 1][j - 1] * ((fib[j - 2] * fib[w - j]) % MOD)) %
            MOD;
      // show(dp[1][k]);
    }
  }
  show(dp[1][k]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define endl '\n'
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define fast_io ios_base::sync_with_stdio(0);
#define show(x) cout << x << endl;
#define pret(x)                                                                \
  cout << x << endl;                                                           \
  return 0;
#define disp(x) cout << x << " ";
#define let(x, y) cout << x << " " << y << endl;
ll MOD = 1e9 + 7;
ll MAX = 9223372036854775807;

ll dp[112][15], fib[20];

int main() {
  fast_io;
  int h, w, k;
  cin >> h >> w >> k;
  fib[2] = 2, fib[3] = 3;
  fib[0] = 1, fib[1] = 1;
  fo(i, 4, 19) fib[i] = (fib[i - 1] + fib[i - 2]) % MOD;
  dp[h + 1][1] = 1;
  rf(i, h, 1) {
    // {
    // int j=2;
    fo(j, 1, w) {
      // show(dp[1][k]);
      dp[i][j] = (dp[i + 1][j] * ((fib[j - 1] * fib[w - j]) % MOD)) % MOD;
      // show(dp[1][k]);
      if (j + 1 <= w)
        dp[i][j] = (dp[i][j] +
                    dp[i + 1][j + 1] * ((fib[j - 1] * fib[w - j - 1]) % MOD)) %
                   MOD;
      // show(dp[1][k]);
      if (j - 1 > 0)
        dp[i][j] =
            (dp[i][j] + dp[i + 1][j - 1] * ((fib[j - 2] * fib[w - j]) % MOD)) %
            MOD;
      // show(dp[1][k]);
    }
  }
  show(dp[1][k]);
  return 0;
}
Problem p03222 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  long long int dp[100][10] = {};
  long long int P = 1e9 + 7;
  int Fib[8] = {1, 1, 2, 3, 5, 8, 13, 21};
  if (W == 1) {
    cout << 1 << endl;
  } else {
    dp[0][1] = 1;
    for (int i = 1; i < 105; i++) {
      dp[i][1] =
          ((dp[i - 1][1] * Fib[W - 1]) % P + (dp[i - 1][2] * Fib[W - 2]) % P) %
          P;
      dp[i][W] = ((dp[i - 1][W] * Fib[W - 1]) % P +
                  (dp[i - 1][W - 1] * Fib[W - 2]) % P) %
                 P;
      for (int j = 2; j < W; j++) {
        dp[i][j] = ((dp[i - 1][j - 1] * Fib[j - 2] * Fib[W - j]) % P +
                    (dp[i - 1][j] * Fib[j - 1] * Fib[W - j]) % P +
                    (dp[i - 1][j + 1] * Fib[j - 1] * Fib[W - j - 1]) % P) %
                   P;
      }
    }
    cout << dp[H][K] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  long long int dp[110][10] = {};
  long long int P = 1e9 + 7;
  int Fib[8] = {1, 1, 2, 3, 5, 8, 13, 21};
  if (W == 1) {
    cout << 1 << endl;
  } else {
    dp[0][1] = 1;
    for (int i = 1; i < 105; i++) {
      dp[i][1] =
          ((dp[i - 1][1] * Fib[W - 1]) % P + (dp[i - 1][2] * Fib[W - 2]) % P) %
          P;
      dp[i][W] = ((dp[i - 1][W] * Fib[W - 1]) % P +
                  (dp[i - 1][W - 1] * Fib[W - 2]) % P) %
                 P;
      for (int j = 2; j < W; j++) {
        dp[i][j] = ((dp[i - 1][j - 1] * Fib[j - 2] * Fib[W - j]) % P +
                    (dp[i - 1][j] * Fib[j - 1] * Fib[W - j]) % P +
                    (dp[i - 1][j + 1] * Fib[j - 1] * Fib[W - j - 1]) % P) %
                   P;
      }
    }
    cout << dp[H][K] << endl;
  }
}
Problem p03222 (C++)

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

const ll mod = 1000000007;

int H, W, K;
ll dp[100][10];

ll sub(int n) {
  if (n <= 0) {
    return 1;
  } else if (n == 1) {
    return 2;
  } else {
    return sub(n - 1) + sub(n - 2);
  }
}

int solve() {
  if (W == 1) {
    cout << 1 << endl;
    return 0;
  }

  // 初期値
  dp[0][0] = 1;
  for (int i = 1; i < W; i++) {
    dp[0][i] = 0;
  }

  for (int i = 1; i <= H; i++) {
    for (int j = 0; j < W; j++) {
      if (j == 0) {
        dp[i][j] = sub(W - 2) * dp[i - 1][j] + sub(W - 3) * dp[i - 1][j + 1];
      } else if (j == W - 1) {
        dp[i][j] = sub(W - 3) * dp[i - 1][j - 1] + sub(W - 2) * dp[i - 1][j];
      } else {
        dp[i][j] = sub(j - 2) * sub(W - 2 - j) * dp[i - 1][j - 1] +
                   sub(j - 1) * sub(W - 2 - j) * dp[i - 1][j] +
                   sub(j - 1) * sub(W - 3 - j) * dp[i - 1][j + 1];
      }
      dp[i][j] %= mod;
    }
  }

  cout << dp[H][K - 1] << endl;
  return 0;
}

int main() {
  cin >> H >> W >> K;
  solve();
  return 0;
}

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

const ll mod = 1000000007;

int H, W, K;
ll dp[110][15];

ll sub(int n) {
  if (n <= 0) {
    return 1;
  } else if (n == 1) {
    return 2;
  } else {
    return sub(n - 1) + sub(n - 2);
  }
}

int solve() {
  if (W == 1) {
    cout << 1 << endl;
    return 0;
  }

  // 初期値
  dp[0][0] = 1;
  for (int i = 1; i < W; i++) {
    dp[0][i] = 0;
  }

  for (int i = 1; i <= H; i++) {
    for (int j = 0; j < W; j++) {
      if (j == 0) {
        dp[i][j] = sub(W - 2) * dp[i - 1][j] + sub(W - 3) * dp[i - 1][j + 1];
      } else if (j == W - 1) {
        dp[i][j] = sub(W - 3) * dp[i - 1][j - 1] + sub(W - 2) * dp[i - 1][j];
      } else {
        dp[i][j] = sub(j - 2) * sub(W - 2 - j) * dp[i - 1][j - 1] +
                   sub(j - 1) * sub(W - 2 - j) * dp[i - 1][j] +
                   sub(j - 1) * sub(W - 3 - j) * dp[i - 1][j + 1];
      }
      dp[i][j] %= mod;
    }
  }

  cout << dp[H][K - 1] << endl;
  return 0;
}

int main() {
  cin >> H >> W >> K;
  solve();
  return 0;
}
Problem p03222 (C++)

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

const int MOD = 1e9 + 7;
int H, W, K;
vector<int> fib;
ll ans;

void input() { cin >> H >> W >> K; }

int solve(int n) {
  if (n < 0)
    return 1;
  return fib[n];
}

int main() {
  input();
  fib = vector<int>(W);
  fib[0] = 1;
  fib[1] = 2;
  for (int i = 2; i < W; i++)
    fib[i] = fib[i - 1] + fib[i - 2];
  vector<ll> w1, w2;
  w1 = vector<ll>(W, 0);
  w1[0] = 1;
  rep(i, H) {
    w2 = vector<ll>(W, 0);
    for (int j = 0; j < W; j++) {
      if (j > 0)
        w2[j - 1] = (w2[j - 1] + w1[j] * solve(j - 2) * solve(W - j - 2)) % MOD;
      w2[j] = (w2[j] + w1[j] * solve(j - 1) * solve(W - j - 2)) % MOD;
      if (j <= W)
        w2[j + 1] = (w2[j + 1] + w1[j] * solve(j - 1) * solve(W - j - 3)) % MOD;
    }
    w1 = w2;
  }
  cout << w1[K - 1] << endl;
}


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

const int MOD = 1e9 + 7;
int H, W, K;
vector<int> fib;
ll ans;

void input() { cin >> H >> W >> K; }

int solve(int n) {
  if (n < 0)
    return 1;
  return fib[n];
}

int main() {
  input();
  fib = vector<int>(W);
  fib[0] = 1;
  fib[1] = 2;
  for (int i = 2; i < W; i++)
    fib[i] = fib[i - 1] + fib[i - 2];
  vector<ll> w1, w2;
  w1 = vector<ll>(W, 0);
  w1[0] = 1;
  rep(i, H) {
    w2 = vector<ll>(W, 0);
    for (int j = 0; j < W; j++) {
      if (j > 0)
        w2[j - 1] = (w2[j - 1] + w1[j] * solve(j - 2) * solve(W - j - 2)) % MOD;
      w2[j] = (w2[j] + w1[j] * solve(j - 1) * solve(W - j - 2)) % MOD;
      if (j < W - 1)
        w2[j + 1] = (w2[j + 1] + w1[j] * solve(j - 1) * solve(W - j - 3)) % MOD;
    }
    w1 = w2;
  }
  cout << w1[K - 1] << endl;
}
Problem p03222 (C++)

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

using namespace std;

const int MOD = 1000 * 1000 * 1000 + 7;

int subcount(int n) {
  if (n <= 0) {
    return 1;
  }
  int c = 0;
  for (int i = 0; i < (1 << n); ++i) {
    bool ok = true;
    for (int k = 0; k + 1 < n; ++k) {
      if ((i & (1 << k)) && (i & (1 << (k + 1)))) {
        ok = false;
        break;
      }
    }
    if (ok) {
      ++c;
    }
  }
  return c;
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  long long dp[100][8 + 1] = {};
  dp[1][1] = subcount(W - 2);
  dp[1][2] = subcount(W - 3);
  for (int j = 3; j <= W; ++j) {
    dp[1][j] = 0;
  }

  for (int i = 2; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (1 < j) {
        dp[i][j - 1] +=
            1LL * subcount(j - 3) * subcount(W - j - 1) * dp[i - 1][j];
        dp[i][j - 1] %= MOD;
      }
      dp[i][j] += 1LL * subcount(j - 2) * subcount(W - j - 1) * dp[i - 1][j];
      dp[i][j] %= MOD;
      if (j < W) {
        dp[i][j + 1] +=
            1LL * subcount(j - 2) * subcount(W - j - 2) * dp[i - 1][j];
        dp[i][j + 1] %= MOD;
      }
    }
  }

  cout << dp[H][K] << endl;
}


Fixed submission:
#include <iostream>

using namespace std;

const int MOD = 1000 * 1000 * 1000 + 7;

int subcount(int n) {
  if (n <= 0) {
    return 1;
  }
  int c = 0;
  for (int i = 0; i < (1 << n); ++i) {
    bool ok = true;
    for (int k = 0; k + 1 < n; ++k) {
      if ((i & (1 << k)) && (i & (1 << (k + 1)))) {
        ok = false;
        break;
      }
    }
    if (ok) {
      ++c;
    }
  }
  return c;
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  long long dp[101][8 + 1] = {};
  dp[1][1] = subcount(W - 2);
  dp[1][2] = subcount(W - 3);
  for (int j = 3; j <= W; ++j) {
    dp[1][j] = 0;
  }

  for (int i = 2; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (1 < j) {
        dp[i][j - 1] +=
            1LL * subcount(j - 3) * subcount(W - j - 1) * dp[i - 1][j];
        dp[i][j - 1] %= MOD;
      }
      dp[i][j] += 1LL * subcount(j - 2) * subcount(W - j - 1) * dp[i - 1][j];
      dp[i][j] %= MOD;
      if (j < W) {
        dp[i][j + 1] +=
            1LL * subcount(j - 2) * subcount(W - j - 2) * dp[i - 1][j];
        dp[i][j + 1] %= MOD;
      }
    }
  }

  cout << dp[H][K] << endl;
}
Problem p03222 (C++)

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

using namespace std;

const long long MOD = 1e9 + 7;

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  // 1*N のところに好きに線を引く (1-index)
  vector<long long> fr = {1, 1, 2, 3, 5, 8, 13, 21, 34};

  // N*W で K で終了する DP
  vector<vector<long long>> dp(H + 1, vector<long long>(W + 1));
  dp[1][1] = fr[W - 1];
  if (W >= 2)
    dp[1][2] = fr[W - 2];
  for (int k = 3; k <= 8; k++)
    dp[1][k] = 0;

  for (int h = 2; h <= H; h++) {
    for (int k = 1; k <= W; k++) {
      dp[h][k] = (dp[h - 1][k] * fr[k - 1] % MOD) * fr[W - k] %
                 MOD; // そのまま降りてくるやつ
      if (k >= 2) {   // 左から
        dp[h][k] += (dp[h - 1][k - 1] * fr[k - 2] % MOD) * fr[W - k] % MOD;
        dp[h][k] %= MOD;
      }
      if (k <= W - 1) { // 右から
        dp[h][k] += (dp[h - 1][k + 1] * fr[k - 1] % MOD) * fr[W - k - 1] % MOD;
        dp[h][k] %= MOD;
      }
    }
  }

  cout << dp[H][K] << endl;
  return 0;
}

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

using namespace std;

const long long MOD = 1e9 + 7;

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  // 1*N のところに好きに線を引く (1-index)
  vector<long long> fr = {1, 1, 2, 3, 5, 8, 13, 21, 34};

  // N*W で K で終了する DP
  vector<vector<long long>> dp(H + 1, vector<long long>(W + 1));
  dp[1][1] = fr[W - 1];
  if (W >= 2)
    dp[1][2] = fr[W - 2];
  for (int k = 3; k <= W; k++)
    dp[1][k] = 0;

  for (int h = 2; h <= H; h++) {
    for (int k = 1; k <= W; k++) {
      dp[h][k] = (dp[h - 1][k] * fr[k - 1] % MOD) * fr[W - k] %
                 MOD; // そのまま降りてくるやつ
      if (k >= 2) {   // 左から
        dp[h][k] += (dp[h - 1][k - 1] * fr[k - 2] % MOD) * fr[W - k] % MOD;
        dp[h][k] %= MOD;
      }
      if (k <= W - 1) { // 右から
        dp[h][k] += (dp[h - 1][k + 1] * fr[k - 1] % MOD) * fr[W - k - 1] % MOD;
        dp[h][k] %= MOD;
      }
    }
  }

  cout << dp[H][K] << endl;
  return 0;
}
Problem p03222 (C++)

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

using namespace std;

int bi(int n, int k) {
  if (k == 1 && n % 2 == 0) {
    return 0;
  }
  if (k == 1 && n % 2 == 1) {
    return 1;
  }
  if (k >= 2) {
    return bi(n / 2, k - 1);
  }
}

int power(int n) {
  if (n == 0) {
    return 1;
  } else {
    return 2 * power(n - 1);
  }
}

int main() {

  int H;
  int W;
  int K;

  cin >> H >> W >> K;

  long int val[10][200] = {}; // 高さn、棒mへ行く経路の数

  int sen[10];

  int nom; // 線の引き方が正しいか

  val[0][1] = 1;

  for (int h = 1; h <= H; h++) {

    for (int i = 0; i < power(W - 1); i++) { // 線を引く箇所の決定

      for (int j = 1; j <= W; j++) {
        sen[j] = bi(i, j);
      }

      nom = 0;

      for (int j = 1; j <= W - 1; j++) { // 線の引き方が正しいか判定
        if (sen[j] == 1 && sen[j + 1] == 1) {
          nom = 1;
        }
      }

      for (int j = 1; j <= W; j++) {

        if (nom == 1) {
          break;
        }

        if (j < W && sen[j] == 1) {
          val[h][j] = val[h][j] + val[h - 1][j + 1];
        }

        else if (j > 1 && sen[j - 1] == 1) {
          val[h][j] = val[h][j] + val[h - 1][j - 1];
        }

        else {
          val[h][j] = val[h][j] + val[h - 1][j];
        }

        if (val[h][j] >= 1000000007) {
          val[h][j] = val[h][j] % 1000000007;
        }
      }
    }
  }

  cout << val[H][K];
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>

using namespace std;

int bi(int n, int k) {
  if (k == 1 && n % 2 == 0) {
    return 0;
  }
  if (k == 1 && n % 2 == 1) {
    return 1;
  }
  if (k >= 2) {
    return bi(n / 2, k - 1);
  }
}

int power(int n) {
  if (n == 0) {
    return 1;
  } else {
    return 2 * power(n - 1);
  }
}

int main() {

  int H;
  int W;
  int K;

  cin >> H >> W >> K;

  long int val[200][10] = {}; // 高さn、棒mへ行く経路の数

  int sen[10];

  int nom; // 線の引き方が正しいか

  val[0][1] = 1;

  for (int h = 1; h <= H; h++) {

    for (int i = 0; i < power(W - 1); i++) { // 線を引く箇所の決定

      for (int j = 1; j <= W; j++) {
        sen[j] = bi(i, j);
      }

      nom = 0;

      for (int j = 1; j <= W - 1; j++) { // 線の引き方が正しいか判定
        if (sen[j] == 1 && sen[j + 1] == 1) {
          nom = 1;
        }
      }

      for (int j = 1; j <= W; j++) {

        if (nom == 1) {
          break;
        }

        if (j < W && sen[j] == 1) {
          val[h][j] = val[h][j] + val[h - 1][j + 1];
        }

        else if (j > 1 && sen[j - 1] == 1) {
          val[h][j] = val[h][j] + val[h - 1][j - 1];
        }

        else {
          val[h][j] = val[h][j] + val[h - 1][j];
        }

        if (val[h][j] >= 1000000007) {
          val[h][j] = val[h][j] % 1000000007;
        }
      }
    }
  }

  cout << val[H][K];
}
Problem p03222 (C++)

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

#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using ll = long long;
const int H = 100, W = 8;
const ll MOD = 1e9 + 7;

int h, w, k;
ll dp[H][W], fib[10];

void pre() {
  fib[0] = 0, fib[1] = 1;
  for (int i = 2; i < 10; ++i)
    fib[i] = fib[i - 1] + fib[i - 2];
  int sum = 0;
  for (int i = 0; i < 10; ++i) {
    sum += fib[i];
    fib[i] = sum;
  }
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> h >> w >> k;
  dp[0][0] = 1;
  pre();

  for (int i = 1; i <= h; ++i) {
    for (int j = 0; j < w; ++j) {
      for (int l = -1; l <= 1; ++l) {
        int width = j + l;
        if (width >= 0 && width < w) {
          // Left slots avail, right slots avail
          ll la = j, ra = w - j - 1;
          --la, --ra;
          if (l == -1)
            --la;
          if (l == 1)
            --ra;
          la = max(0ll, la), ra = max(0ll, ra);
          la = fib[la] + 1, ra = fib[ra] + 1;

          ll toAdd = ((dp[i - 1][width] % MOD) * la) % MOD;
          toAdd *= ra, toAdd %= MOD;

          dp[i][j] = (dp[i][j] + toAdd) % MOD;
        }
      }
    }
  }

  cout << dp[h][k - 1] % MOD;
}


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

#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using ll = long long;
const int H = 105, W = 8;
const ll MOD = 1e9 + 7;

int h, w, k;
ll dp[H][W], fib[10];

void pre() {
  fib[0] = 0, fib[1] = 1;
  for (int i = 2; i < 10; ++i)
    fib[i] = fib[i - 1] + fib[i - 2];
  int sum = 0;
  for (int i = 0; i < 10; ++i) {
    sum += fib[i];
    fib[i] = sum;
  }
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> h >> w >> k;
  dp[0][0] = 1;
  pre();

  for (int i = 1; i <= h; ++i) {
    for (int j = 0; j < w; ++j) {
      for (int l = -1; l <= 1; ++l) {
        int width = j + l;
        if (width >= 0 && width < w) {
          // Left slots avail, right slots avail
          ll la = j, ra = w - j - 1;
          --la, --ra;
          if (l == -1)
            --la;
          if (l == 1)
            --ra;
          la = max(0ll, la), ra = max(0ll, ra);
          la = fib[la] + 1, ra = fib[ra] + 1;

          ll toAdd = ((dp[i - 1][width] % MOD) * la) % MOD;
          toAdd *= ra, toAdd %= MOD;

          dp[i][j] = (dp[i][j] + toAdd) % MOD;
        }
      }
    }
  }

  cout << dp[h][k - 1] % MOD;
}
Problem p03222 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int mod = 1e9 + 7;
int h, w, k;
vector<int> r;
int dp[100][8];

int nextPos(int cur, int row) {
  int l, r;
  l = cur > 0 ? (row >> cur - 1) % 2 : 0;
  r = (row >> cur) % 2;
  if (cur > 0 && l)
    return cur - 1;
  else if (cur < w - 1 && r)
    return cur + 1;
  else
    return cur;
}

signed main() {

  cin >> h >> w >> k;

  // cout<<nextPos(0,2)<<endl;
  // cout<<nextPos(1,2)<<endl;
  if (w > 1) {
    for (int x = 0; x < (1 << w - 1) - 1; x++)
      if ((x & x >> 1 & (1 << w - 2) - 1) == 0)
        r.push_back(x);
  } else {
    r.push_back(0);
  }

  // for(auto&& x:r)cout<<x<<" ";
  // cout<<endl;
  // cout<<r.size()<<endl;

  // static int dp[h][w];
  // fill(dp,dp+w,0);
  dp[0][0] = 1;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (auto &&x : r) {
        //      int y;
        //      y=nextPos(j,x);
        dp[i + 1][nextPos(j, x)] += (dp[i][j] % mod);
      }
    }
  }

  // for(int i=0;i<=h;i++){
  //     for(int j=0;j<w;j++){
  //         cout <<dp[i][j]<<" ";
  //     }
  //    cout <<endl;
  // }

  cout << dp[h][k - 1] % mod << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int mod = 1e9 + 7;
int h, w, k;
vector<int> r;
int dp[101][8];

int nextPos(int cur, int row) {
  int l, r;
  l = cur > 0 ? (row >> cur - 1) % 2 : 0;
  r = (row >> cur) % 2;
  if (cur > 0 && l)
    return cur - 1;
  else if (cur < w - 1 && r)
    return cur + 1;
  else
    return cur;
}

signed main() {

  cin >> h >> w >> k;

  // cout<<nextPos(0,2)<<endl;
  // cout<<nextPos(1,2)<<endl;
  if (w > 1) {
    for (int x = 0; x < (1 << w - 1) - 1; x++)
      if ((x & x >> 1 & (1 << w - 2) - 1) == 0)
        r.push_back(x);
  } else {
    r.push_back(0);
  }

  // for(auto&& x:r)cout<<x<<" ";
  // cout<<endl;
  // cout<<r.size()<<endl;

  // static int dp[h][w];
  // fill(dp,dp+w,0);
  dp[0][0] = 1;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (auto &&x : r) {
        //      int y;
        //      y=nextPos(j,x);
        dp[i + 1][nextPos(j, x)] += (dp[i][j] % mod);
      }
    }
  }

  // for(int i=0;i<=h;i++){
  //     for(int j=0;j<w;j++){
  //         cout <<dp[i][j]<<" ";
  //     }
  //    cout <<endl;
  // }

  cout << dp[h][k - 1] % mod << endl;
}
Problem p03222 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <random>
#include <vector>
using namespace std;

int main() {
  long long MOD = 1000000007;
  long long H, W, K;
  long long num[100][10];
  long long hor_f[10];

  scanf("%lld %lld %lld", &H, &W, &K);

  hor_f[0] = 1;
  hor_f[1] = 1;
  for (int i = 2; i < 10; i++) {
    hor_f[i] = hor_f[i - 1] + hor_f[i - 2];
  }

  num[0][1] = 1;
  for (int i = 2; i < W + 1; i++) {
    num[0][i] = 0;
  }

  for (int h = 1; h <= H; h++) {
    num[h][1] =
        (hor_f[W - 1] * num[h - 1][1] + hor_f[W - 2] * num[h - 1][2]) % MOD;
    num[h][W] =
        (hor_f[W - 2] * num[h - 1][W - 1] + hor_f[W - 1] * num[h - 1][W]) % MOD;
    for (int a = 2; a < W; a++) {
      num[h][a] = (hor_f[a - 2] * hor_f[W - a] * num[h - 1][a - 1] +
                   hor_f[a - 1] * hor_f[W - a] * num[h - 1][a] +
                   hor_f[a - 1] * hor_f[W - a - 1] * num[h - 1][a + 1]) %
                  MOD;
    }
  }

  printf("%lld\n", num[H][K]);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <random>
#include <vector>
using namespace std;

int main() {
  long long MOD = 1000000007;
  long long H, W, K;
  long long num[101][10];
  long long hor_f[10];

  scanf("%lld %lld %lld", &H, &W, &K);

  hor_f[0] = 1;
  hor_f[1] = 1;
  for (int i = 2; i < 10; i++) {
    hor_f[i] = hor_f[i - 1] + hor_f[i - 2];
  }

  num[0][1] = 1;
  for (int i = 2; i < W + 1; i++) {
    num[0][i] = 0;
  }

  for (int h = 1; h <= H; h++) {
    num[h][1] =
        (hor_f[W - 1] * num[h - 1][1] + hor_f[W - 2] * num[h - 1][2]) % MOD;
    num[h][W] =
        (hor_f[W - 2] * num[h - 1][W - 1] + hor_f[W - 1] * num[h - 1][W]) % MOD;
    for (int a = 2; a < W; a++) {
      num[h][a] = (hor_f[a - 2] * hor_f[W - a] * num[h - 1][a - 1] +
                   hor_f[a - 1] * hor_f[W - a] * num[h - 1][a] +
                   hor_f[a - 1] * hor_f[W - a - 1] * num[h - 1][a + 1]) %
                  MOD;
    }
  }

  printf("%lld\n", num[H][K]);
}
Problem p03222 (Python)

Buggy submission (status: Runtime Error):
def count(length, on):
    """Count of possible amida patterns"""
    if length < 0:
        return 1
    if length == 0:
        return 1
    ret = count(length - 1, False)
    if not on:
        ret += count(length - 1, True)
    return ret


# for i in range(1, 9):
#     print(count(i, True) + count(i, False))


if __name__ == "__main__":
    H, W, K = map(int, input().split())

    # Count
    B = [0 for _ in range(W)]
    B[0] = 1
    for j in range(H):
        C = [0 for _ in range(W)]
        for i in range(W):
            left_off = count(i - 1, False)
            left_on = count(i - 1, True)
            right_off = count(W - i - 2, False)
            right_on = count(W - i - 2, True)
            # print(i, left_off, left_on, right_off, right_on)

            if B[i] > 0:
                C[i] += B[i] * (left_off * right_off)

            if i > 0 and B[i - 1] > 0:
                C[i] += B[i - 1] * (left_on * right_off)

            if i < W - 1 and B[i + 1] > 0:
                C[i] += B[i + 1] * (left_off * right_on)

        B = [ci for ci in C]
        # print(B)

    MOD = 1_000_000_007
    ANS = C[K - 1] % MOD
    print(ANS)


Fixed submission:
def count(length, on):
    """Count of possible amida patterns"""
    if length < 0:
        return 1
    if length == 0:
        return 1
    ret = count(length - 1, False)
    if not on:
        ret += count(length - 1, True)
    return ret


# for i in range(1, 9):
#     print(count(i, True) + count(i, False))


if __name__ == "__main__":
    H, W, K = map(int, input().split())

    # Count
    B = [0 for _ in range(W)]
    B[0] = 1
    for j in range(H):
        C = [0 for _ in range(W)]
        for i in range(W):
            left_off = count(i - 1, False)
            left_on = count(i - 1, True)
            right_off = count(W - i - 2, False)
            right_on = count(W - i - 2, True)
            # print(i, left_off, left_on, right_off, right_on)

            if B[i] > 0:
                C[i] += B[i] * (left_off * right_off)

            if i > 0 and B[i - 1] > 0:
                C[i] += B[i - 1] * (left_on * right_off)

            if i < W - 1 and B[i + 1] > 0:
                C[i] += B[i + 1] * (left_off * right_on)

        B = [ci for ci in C]
        # print(B)

    # MOD = 1_000_000_007  # Underscore is not supported until 3.6
    MOD = 1000000007
    ANS = C[K - 1] % MOD
    print(ANS)
Problem p03222 (C++)

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

using ull = unsigned long long;
static ull const Mod = 1000000007;

std::vector<ull> Fib;
std::map<std::tuple<int, int, int>, int> Memo;

ull Amida1(int W, int J, int K) {
  if (J < 1 || K < 1 || J > W || K > W || std::abs(J - K) > 1) {
    return 0;
  }

  auto s = std::min(J, K);
  auto l = std::max(J, K);

  return (Fib[s - 1] * Fib[W - l]) % Mod;
}

ull Amida(int H, int W, int K) {
  if (K < 1 || K > W || std::abs(K - 1) > H) {
    return 0;
  }

  auto key = std::make_tuple(H, W, K);
  if (Memo.count(key) > 0)
    return Memo.at(key);

  if (H == 1) {
    auto ans = Amida1(W, 1, K);
    Memo.insert(std::make_pair(key, ans));
    return ans;
  }

  ull ans;
  ans = Amida(H - 1, W, K - 1) * Amida1(W, K - 1, K);
  ans %= Mod;
  ans += Amida(H - 1, W, K) * Amida1(W, K, K);
  ans %= Mod;
  ans += Amida(H - 1, W, K + 1) * Amida1(W, K + 1, K);
  ans %= Mod;

  Memo.insert(std::make_pair(key, ans));
  return ans;
}

int main() {
  int H, W, K;

  std::cin >> H >> W >> K;

  Fib.resize(W);
  Fib[0] = 1;
  Fib[1] = 1;

  for (size_t i = 2; i < W + 1; i++)
    Fib[i] = Fib[i - 2] + Fib[i - 1];

  std::cout << Amida(H, W, K) << std::endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <map>
#include <tuple>
#include <vector>

using ull = unsigned long long;
static ull const Mod = 1000000007;

std::vector<ull> Fib;
std::map<std::tuple<int, int, int>, int> Memo;

ull Amida1(int W, int J, int K) {
  if (J < 1 || K < 1 || J > W || K > W || std::abs(J - K) > 1) {
    return 0;
  }

  auto s = std::min(J, K);
  auto l = std::max(J, K);

  return (Fib[s - 1] * Fib[W - l]) % Mod;
}

ull Amida(int H, int W, int K) {
  if (K < 1 || K > W || std::abs(K - 1) > H) {
    return 0;
  }

  auto key = std::make_tuple(H, W, K);
  if (Memo.count(key) > 0)
    return Memo.at(key);

  if (H == 1) {
    auto ans = Amida1(W, 1, K);
    Memo.insert(std::make_pair(key, ans));
    return ans;
  }

  ull ans;
  ans = Amida(H - 1, W, K - 1) * Amida1(W, K - 1, K);
  ans %= Mod;
  ans += Amida(H - 1, W, K) * Amida1(W, K, K);
  ans %= Mod;
  ans += Amida(H - 1, W, K + 1) * Amida1(W, K + 1, K);
  ans %= Mod;

  Memo.insert(std::make_pair(key, ans));
  return ans;
}

int main() {
  int H, W, K;

  std::cin >> H >> W >> K;

  Fib.resize(W + 1);
  Fib[0] = 1;
  Fib[1] = 1;

  for (size_t i = 2; i < W + 1; i++)
    Fib[i] = Fib[i - 2] + Fib[i - 1];

  std::cout << Amida(H, W, K) << std::endl;

  return 0;
}
Problem p03223 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

#define Maxn 100005
#define LL long long
#define R register

int n, a[Maxn];
LL ans;

int main() {
  scanf("%d", &n);
  for (R int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), a[n + i] = a[i];
  sort(a + 1, a + n * 2 + 1);
  //	for (R int i=1;i<=2*n;++i)printf("%d ",a[i]);puts("");
  if (n == 2) {
    printf("%d\n", abs(a[3] - a[2]));
    return 0;
  }
  if (n & 1) {
    for (R int i = 2 * n; i >= n + 4; --i)
      ans += a[i];
    for (R int i = 1; i <= n - 3; ++i)
      ans -= a[i];
    ans += a[n + 3] - a[n - 2];
    ans = max(ans + a[n + 2] - a[n + 1], ans + a[n] - a[n - 1]);
  } else {
    for (R int i = 2 * n; i >= n + 3; --i)
      ans += a[i];
    for (R int i = 1; i <= n - 2; ++i)
      ans -= a[i];
    ans += a[n + 2] - a[n - 1];
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

#define Maxn 200005
#define LL long long
#define R register

int n, a[Maxn];
LL ans;

int main() {
  scanf("%d", &n);
  for (R int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), a[n + i] = a[i];
  sort(a + 1, a + n * 2 + 1);
  //	for (R int i=1;i<=2*n;++i)printf("%d ",a[i]);puts("");
  if (n == 2) {
    printf("%d\n", abs(a[3] - a[2]));
    return 0;
  }
  if (n & 1) {
    for (R int i = 2 * n; i >= n + 4; --i)
      ans += a[i];
    for (R int i = 1; i <= n - 3; ++i)
      ans -= a[i];
    ans += a[n + 3] - a[n - 2];
    ans = max(ans + a[n + 2] - a[n + 1], ans + a[n] - a[n - 1]);
  } else {
    for (R int i = 2 * n; i >= n + 3; --i)
      ans += a[i];
    for (R int i = 1; i <= n - 2; ++i)
      ans -= a[i];
    ans += a[n + 2] - a[n - 1];
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03223 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
const int MAX = 10000;

int main() {
  int N;
  int A[MAX];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  sort(A, A + N);

  vector<int> v1, v2;
  long long ans1 = 0;
  long long ans2 = 0;

  v1.push_back(-1);
  v1.push_back(N & 1 ? -1 : 1);
  v2.push_back(1);
  v2.push_back(N & 1 ? 1 : -1);

  for (int i = 0; i < N - 2; i++) {
    v1.push_back(i & 1 ? -2 : 2);
    v2.push_back(i & 1 ? 2 : -2);
  }

  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());

  for (int i = 0; i < N; i++) {
    ans1 += A[i] * v1[i];
    ans2 += A[i] * v2[i];
  }

  cout << max(ans1, ans2) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
const int MAX = 100000;

int main() {
  int N;
  int A[MAX];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  sort(A, A + N);

  vector<int> v1, v2;
  long long ans1 = 0;
  long long ans2 = 0;

  v1.push_back(-1);
  v1.push_back(N & 1 ? -1 : 1);
  v2.push_back(1);
  v2.push_back(N & 1 ? 1 : -1);

  for (int i = 0; i < N - 2; i++) {
    v1.push_back(i & 1 ? -2 : 2);
    v2.push_back(i & 1 ? 2 : -2);
  }

  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());

  for (int i = 0; i < N; i++) {
    ans1 += A[i] * v1[i];
    ans2 += A[i] * v2[i];
  }

  cout << max(ans1, ans2) << endl;

  return 0;
}
Problem p03223 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
vector<int> doit(const vector<int> &A) {
  int N = A.size();
  vector<int> B(N);
  for (int i = 0; i < N / 2; i++) {
    B[N / 2 - 1 - i] = A[i];
    B[N / 2 + i] = A[N - 1 - i];
    if (i % 2)
      swap(B[N / 2 - i - i], B[N / 2 + i]);
  }
  if (N % 2 == 1)
    B[N - 1] = A[N / 2];
  return B;
}
Int sumdiff(const vector<int> &A) {
  Int diff = 0;
  for (int i = 0; i < A.size() - 1; i++) {
    diff += abs((Int)A[i + 1] - A[i]);
  }
  return diff;
}
void print(const vector<int> &A) {
  for (int i = 0; i < A.size(); i++)
    cout << A[i] << ' ';
  cout << endl;
}
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  vector<int> B = doit(A);
  Int b = sumdiff(B);
  sort(A.rbegin(), A.rend());
  vector<int> C = doit(A);
  Int c = sumdiff(C);
  cout << max(b, c) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
vector<int> doit(const vector<int> &A) {
  int N = A.size();
  vector<int> B(N);
  for (int i = 0; i < N / 2; i++) {
    B[N / 2 - 1 - i] = A[i];
    B[N / 2 + i] = A[N - 1 - i];
    if (i % 2)
      swap(B[N / 2 - 1 - i], B[N / 2 + i]);
  }
  if (N % 2 == 1)
    B[N - 1] = A[N / 2];
  return B;
}
Int sumdiff(const vector<int> &A) {
  Int diff = 0;
  for (int i = 0; i < A.size() - 1; i++) {
    diff += abs((Int)A[i + 1] - A[i]);
  }
  return diff;
}
void print(const vector<int> &A) {
  for (int i = 0; i < A.size(); i++)
    cout << A[i] << ' ';
  cout << endl;
}
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  vector<int> B = doit(A);
  Int b = sumdiff(B);
  sort(A.rbegin(), A.rend());
  vector<int> C = doit(A);
  Int c = sumdiff(C);
  cout << max(b, c) << endl;
  return 0;
}
Problem p03223 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  int n, a[10000], b[10000];
  long long c = 0, d = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);

  if (n % 2 == 0) {
    b[0] = a[n / 2];
    for (int i = 1; i < n; i++) {
      if (i % 2 == 0) {
        b[i] = a[n / 2 + i / 2];
      } else {
        b[i] = a[i / 2];
      }
      c += abs(b[i] - b[i - 1]);
    }
  } else {
    b[0] = a[n / 2];
    for (int i = 1; i < n; i++) {
      if (i % 2 == 0) {
        b[i] = a[(i - 1) / 2];
      } else {
        b[i] = a[(n + i) / 2];
      }
      c += abs(b[i] - b[i - 1]);
    }
    b[0] = a[n / 2];
    for (int i = 1; i < n; i++) {
      if (i % 2 == 0) {
        b[i] = a[n - i / 2];
      } else {
        b[i] = a[i / 2];
      }
      d += abs(b[i] - b[i - 1]);
    }
  }
  cout << max(c, d) << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  int n, a[100000], b[100000];
  long long c = 0, d = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);

  if (n % 2 == 0) {
    b[0] = a[n / 2];
    for (int i = 1; i < n; i++) {
      if (i % 2 == 0) {
        b[i] = a[n / 2 + i / 2];
      } else {
        b[i] = a[i / 2];
      }
      c += abs(b[i] - b[i - 1]);
    }
  } else {
    b[0] = a[n / 2];
    for (int i = 1; i < n; i++) {
      if (i % 2 == 0) {
        b[i] = a[(i - 1) / 2];
      } else {
        b[i] = a[(n + i) / 2];
      }
      c += abs(b[i] - b[i - 1]);
    }
    b[0] = a[n / 2];
    for (int i = 1; i < n; i++) {
      if (i % 2 == 0) {
        b[i] = a[n - i / 2];
      } else {
        b[i] = a[i / 2];
      }
      d += abs(b[i] - b[i - 1]);
    }
  }
  cout << max(c, d) << endl;
}
Problem p03224 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (int i = 0; i < N; ++i)
typedef long long int LL;

int main() {
  LL N;
  in >> N;

  if (N * 2 != (LL)sqrt(N * 2) * ((LL)sqrt(N * 2) + 1)) {
    out << "No" << std::endl;
    return 0;
  }

  LL size = sqrt(N * 2);
  std::vector<std::vector<LL>> field(size);
  LL num = 1;
  rep(i, size) {
    rep(j, i + 1) {
      field[i].push_back(num);
      ++num;
    }
  }

  std::vector<std::vector<LL>> ans(size + 1);
  rep(i, size) {
    ans[0].push_back(field[i][0]);
    ans[1].push_back(field[size - 1][i]);
    ans[2].push_back(field[i][i]);
  }
  rep(i, size - 2) {
    rep(j, i + 1) ans[i + 3].push_back(field[i + 1][j]);
    rep(j, size - i - 1) ans[i + 3].push_back(field[i + 1 + j][i + 1]);
  }

  out << "Yes" << std::endl << ans.size() << std::endl;
  rep(i, ans.size()) {
    out << ans[i].size() << " ";
    rep(j, ans[i].size()) out << ans[i][j]
                              << (j < ans[i].size() - 1 ? " " : "\n");
  }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (int i = 0; i < N; ++i)
typedef long long int LL;

int main() {
  LL N;
  in >> N;

  if (N * 2 != (LL)sqrt(N * 2) * ((LL)sqrt(N * 2) + 1)) {
    out << "No" << std::endl;
    return 0;
  }
  if (N == 1) {
    out << "Yes" << std::endl
        << 2 << std::endl
        << 1 << " " << 1 << std::endl
        << 1 << " " << 1 << std::endl;
    return 0;
  }

  LL size = sqrt(N * 2);
  std::vector<std::vector<LL>> field(size);
  LL num = 1;
  rep(i, size) {
    rep(j, i + 1) {
      field[i].push_back(num);
      ++num;
    }
  }

  std::vector<std::vector<LL>> ans(size + 1);
  rep(i, size) {
    ans[0].push_back(field[i][0]);
    ans[1].push_back(field[size - 1][i]);
    ans[2].push_back(field[i][i]);
  }
  rep(i, size - 2) {
    rep(j, i + 1) ans[i + 3].push_back(field[i + 1][j]);
    rep(j, size - i - 1) ans[i + 3].push_back(field[i + 1 + j][i + 1]);
  }

  out << "Yes" << std::endl << ans.size() << std::endl;
  rep(i, ans.size()) {
    out << ans[i].size() << " ";
    rep(j, ans[i].size()) out << ans[i][j]
                              << (j < ans[i].size() - 1 ? " " : "\n");
  }
  return 0;
}
Problem p03224 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
typedef long long LL;
int N;
vector<int> G[401];
void answer(int x) {
  cout << "Yes" << endl;
  cout << x + 1 << endl;
  int now = 1;
  REP(i, x) {
    REP(j, x - i) {
      G[i].push_back(now);
      G[i + 1 + j].push_back(now);
      now++;
    }
  }
  REP(i, x + 1) {
    cout << x << " ";
    REP(j, x) {
      cout << G[i][j];
      if (j != x - 1) {
        cout << " ";
      } else {
        cout << endl;
      }
    }
  }
}

int main() {
  cin >> N;
  REP(i, N + 1) {
    if (2 * N == i * (i + 1)) {
      answer(i);
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
typedef long long LL;
int N;
vector<int> G[701];
void answer(int x) {
  cout << "Yes" << endl;
  cout << x + 1 << endl;
  int now = 1;
  REP(i, x) {
    REP(j, x - i) {
      G[i].push_back(now);
      G[i + 1 + j].push_back(now);
      now++;
    }
  }
  REP(i, x + 1) {
    cout << x << " ";
    REP(j, x) {
      cout << G[i][j];
      if (j != x - 1) {
        cout << " ";
      } else {
        cout << endl;
      }
    }
  }
}

int main() {
  cin >> N;
  REP(i, N + 1) {
    if (2 * N == i * (i + 1)) {
      answer(i);
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}
Problem p03224 (C++)

Buggy submission (status: Runtime Error):
//----------------------------おまじない
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
//----------------------------
#define FOR(i, j, n) for (int i = (j); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPN(i, n) for (int i = (n); i > 0; i--)
#define I(n) scanf("%d", &(n))
#define LL(n) scanf("%lld", &(n))
#define pb(n) push_back((n))
#define mp(i, j) make_pair((i), (j))
#define eb(i, j) emplace_back((i), (j))
#include <bits/stdc++.h>
using namespace std;
//------------------------------typedef集
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef vector<vvi> vvvi;
typedef long long ll;
const int mod = 1000000009;

int n;
vvi ans;

int main() {
  cin >> n;
  int k = sqrt((double)n * 2);

  if (n == 1) {
    cout << "Yes" << endl;
    cout << 2 << endl;
    cout << "1 1" << endl;
    cout << "1 1" << endl;
  } else if (k * (k + 1) == 2 * n) {
    cout << "Yes" << endl;
    cout << k + 1 << endl;

    ans.resize(k + 1);
    int elm = 1;

    REP(i, n) {
      int st = ans[i].size();
      FOR(j, st + 1, k + 1) {
        ans[i].pb(elm);
        ans[j].pb(elm);
        elm++;
      }
      cout << k << " ";
      for (auto v : ans[i])
        cout << v << " ";
      cout << endl;
    }

  } else {
    cout << "No" << endl;
  }
}

Fixed submission:
//----------------------------おまじない
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
//----------------------------
#define FOR(i, j, n) for (int i = (j); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPN(i, n) for (int i = (n); i > 0; i--)
#define I(n) scanf("%d", &(n))
#define LL(n) scanf("%lld", &(n))
#define pb(n) push_back((n))
#define mp(i, j) make_pair((i), (j))
#define eb(i, j) emplace_back((i), (j))
#include <bits/stdc++.h>
using namespace std;
//------------------------------typedef集
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef vector<vvi> vvvi;
typedef long long ll;
const int mod = 1000000009;

int n;
vvi ans;

int main() {
  cin >> n;
  int k = sqrt((double)n * 2);

  if (n == 1) {
    cout << "Yes" << endl;
    cout << 2 << endl;
    cout << "1 1" << endl;
    cout << "1 1" << endl;
  } else if (k * (k + 1) == 2 * n) {
    cout << "Yes" << endl;
    cout << k + 1 << endl;

    ans.resize(k + 1);
    int elm = 1;

    REP(i, k + 1) {
      int st = ans[i].size();
      FOR(j, st + 1, k + 1) {
        ans[i].pb(elm);
        ans[j].pb(elm);
        elm++;
      }
      cout << k << " ";
      for (auto v : ans[i])
        cout << v << " ";
      cout << endl;
    }

  } else {
    cout << "No" << endl;
  }
}
Problem p03224 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int ans[350][350];
int main() {
  int n, k = 1;
  scanf("%d", &n);
  while (k * (k - 1) / 2 < n)
    k++;
  if (k * (k - 1) / 2 == n) {
    printf("Yes\n%d\n", k);
    int cur = 1;
    for (int i = 0; i < k - 1; i++) {
      for (int j = i; j < k - 1; j++)
        ans[i][j] = cur + j - i;
      for (int j = i + 1; j < k; j++)
        ans[j][i] = cur + j - i - 1;
      cur += k - i - 1;
    }
    for (int i = 0; i < k; i++) {
      printf("%d", k - 1);
      for (int j = 0; j < k - 1; j++)
        printf(" %d", ans[i][j]);
      printf("\n");
    }
  } else
    printf("No");
}

Fixed submission:
#include <iostream>
using namespace std;
int ans[500][500];
int main() {
  int n, k = 1;
  scanf("%d", &n);
  while (k * (k - 1) / 2 < n)
    k++;
  if (k * (k - 1) / 2 == n) {
    printf("Yes\n%d\n", k);
    int cur = 1;
    for (int i = 0; i < k - 1; i++) {
      for (int j = i; j < k - 1; j++)
        ans[i][j] = cur + j - i;
      for (int j = i + 1; j < k; j++)
        ans[j][i] = cur + j - i - 1;
      cur += k - i - 1;
    }
    for (int i = 0; i < k; i++) {
      printf("%d", k - 1);
      for (int j = 0; j < k - 1; j++)
        printf(" %d", ans[i][j]);
      printf("\n");
    }
  } else
    printf("No");
}
Problem p03224 (C++)

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

using i64 = int64_t;

#define rep(i, x, y)                                                           \
  for (i64 i = i64(x), i##_max_for_repmacro = i64(y);                          \
       i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int N;
  cin >> N;

  int k = 1;
  while (k * (k - 1) / 2 <= N) {
    if (k * (k - 1) / 2 == N) {
      break;
    }
    ++k;
  }

  if (k * (k - 1) / 2 != N) {
    cout << "No" << endl;
    return;
  }

  cout << "Yes" << endl;
  vector<pair<int, int>> x(N);
  int j = 0;
  rep(i, 0, k) {
    rep(l, i + 1, k) {
      x[j] = make_pair(i, l);
      ++j;
    }
  }

  cout << k << endl;

  vector<vector<int>> ans(k);
  rep(i, 0, N) {
    ans[x[i].first].push_back(i + 1);
    ans[x[i].second].push_back(i + 1);
  }

  rep(i, 0, k) {
    cout << ans[i].size();
    for (int j : ans[k]) {
      cout << " " << j;
    }
    cout << endl;
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}


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

using i64 = int64_t;

#define rep(i, x, y)                                                           \
  for (i64 i = i64(x), i##_max_for_repmacro = i64(y);                          \
       i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int N;
  cin >> N;

  int k = 1;
  while (k * (k - 1) / 2 <= N) {
    if (k * (k - 1) / 2 == N) {
      break;
    }
    ++k;
  }

  if (k * (k - 1) / 2 != N) {
    cout << "No" << endl;
    return;
  }

  cout << "Yes" << endl;
  vector<pair<int, int>> x(N);
  int j = 0;
  rep(i, 0, k) {
    rep(l, i + 1, k) {
      x[j] = make_pair(i, l);
      ++j;
    }
  }

  cout << k << endl;

  vector<vector<int>> ans(k);
  rep(i, 0, N) {
    ans[x[i].first].push_back(i + 1);
    ans[x[i].second].push_back(i + 1);
  }

  rep(i, 0, k) {
    cout << ans[i].size();
    for (int j : ans[i]) {
      cout << " " << j;
    }
    cout << endl;
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}
Problem p03224 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, Ans[400][400], cnt[400];

int main() {
  cin >> n;
  int k = 0;
  for (int i = 1; i * (i - 1) / 2 <= n; i++)
    if (i * (i - 1) / 2 == n)
      k = i;
  if (!k)
    return puts("No"), 0;
  printf("Yes\n%d\n", k);
  for (int i = 1, id = 1; i <= k; i++)
    for (int j = i + 1; j <= k; j++) {
      Ans[i][++cnt[i]] = id;
      Ans[j][++cnt[j]] = id;
      id++;
    }
  for (int i = 1; i <= k; i++) {
    printf("%d ", k - 1);
    for (int j = 1; j < k; j++)
      printf("%d ", Ans[i][j]);
    puts("");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, Ans[1000][1000], cnt[1000];

int main() {
  cin >> n;
  int k = 0;
  for (int i = 1; i * (i - 1) / 2 <= n; i++)
    if (i * (i - 1) / 2 == n)
      k = i;
  if (!k)
    return puts("No"), 0;
  printf("Yes\n%d\n", k);
  for (int i = 1, id = 1; i <= k; i++)
    for (int j = i + 1; j <= k; j++) {
      Ans[i][++cnt[i]] = id;
      Ans[j][++cnt[j]] = id;
      id++;
    }
  for (int i = 1; i <= k; i++) {
    printf("%d ", k - 1);
    for (int j = 1; j < k; j++)
      printf("%d ", Ans[i][j]);
    puts("");
  }
  return 0;
}