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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;

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

const ll MOD = 1000000007;
const ll INF = 1000000000;

int h[105];
ll ans = 0;

int main(void) {
  int n, k;
  int h[10010];
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  sort(h, h + n);
  int ans = INF;
  for (int i = 0; i <= n - k; i++)
    ans = min(ans, h[i + k - 1] - h[i]);
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;

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

const ll MOD = 1000000007;
const ll INF = 1000000000;

int h[105];
ll ans = 0;

int main(void) {
  int n, k;
  int h[100010];
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  sort(h, h + n);
  int ans = INF;
  for (int i = 0; i <= n - k; i++)
    ans = min(ans, h[i + k - 1] - h[i]);
  cout << ans << endl;
}
Problem p03208 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h.begin(), h.end());
  ll ans = 1000000000000;
  for (int i = 0; i <= N - K; i++) {
    for (int j = i; j <= N - K; j++) {
      ans = min(ans, h[j + K - 1] - h[j]);
    }
  }
  cout << ans << endl;
}


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

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h.begin(), h.end());
  ll ans = 1000000000000;
  for (int i = 0; i <= N - K; i++) {
    ans = min(ans, h[i + K - 1] - h[i]);
  }
  cout << ans << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define mm(arr) memset(arr, 0, sizeof(arr))
#define scanArray(a, n)                                                        \
  for (int i = 0; i < n; i++) {                                                \
    cin >> a[i];                                                               \
  }
#define pb push_back
#define PI 3.141592653589793
#define MOD 1000000007

using namespace std;
int main() {
  FAST ll n, k;
  cin >> n >> k;
  ll a[n];
  scanArray(a, n);
  sort(a, a + n);
  ll mndf = 1e10;
  if (n == 2 && k == 2) {
    cout << abs(a[n - 1] - a[0]) << endl;
    return 0;
  }
  bool ok = true;
  for (ll i = 0; i < n; i++) {
    if (i == n - 1)
      break;
    if (abs(a[i] - a[i + 1]) != 1) {
      ok = false;
      break;
    }
  }
  if (ok == true) {
    cout << "1" << endl;
    return 0;
  }
  for (ll i = 0; i < i + k - 1; i++) {
    if (i == n - 1) {
      break;
    }
    mndf = min(mndf, abs(a[i] - a[i + k - 1]));
  }
  cout << mndf << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define mm(arr) memset(arr, 0, sizeof(arr))
#define scanArray(a, n)                                                        \
  for (int i = 0; i < n; i++) {                                                \
    cin >> a[i];                                                               \
  }
#define pb push_back
#define PI 3.141592653589793
#define MOD 1000000007

using namespace std;
int main() {
  FAST ll n, k;
  cin >> n >> k;
  ll a[n];
  scanArray(a, n);
  sort(a, a + n);
  ll mndf = 1e10;
  for (ll i = 0; i + k - 1 < n; i++) {
    if (i == n - 1) {
      break;
    }
    mndf = min(mndf, abs(a[i] - a[i + k - 1]));
  }
  cout << mndf << endl;

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define pb(a) push_back(a)

typedef long long ll;
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

// const int dy[] = { 0, 0, 1, -1 };
// const int dx[] = { 1, -1, 0, 0 };
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b);
ll lcm(ll x, ll y);

vector<ll> h, hh;

int main() {
  ll n, k, ans;

  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    h.push_back(tmp);
  }

  sort(h.begin(), h.end());
  ans = INF;
  for (int i = 0; i < n - k + 1; i++) {
    hh.clear();
    for (int j = 0; j < k; j++) {
      hh.push_back(h[i + j]);
    }
    ll tmp = hh[k - 1] - hh[0];
    ans = min(ans, tmp);
  }

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


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define pb(a) push_back(a)

typedef long long ll;
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

// const int dy[] = { 0, 0, 1, -1 };
// const int dx[] = { 1, -1, 0, 0 };
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b);
ll lcm(ll x, ll y);

vector<ll> h, hh;

int main() {
  ll n, k, ans;

  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    h.push_back(tmp);
  }

  sort(h.begin(), h.end());
  ans = INF;
  for (int i = 0; i < n - k + 1; i++) {
    ans = min(ans, h[i + k - 1] - h[i]);
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  int k;
  int min;
  cin >> n >> k;
  vector<int> tree(n);
  for (int i = 0; i < n; i++) {
    cin >> tree[i];
  }
  sort(tree.begin(), tree.end());
  min = tree[k - 1] - tree[0];
  for (int j = 0; j < n - k; j++) {
    if (min > tree[k - 1 - j] - tree[j]) {
      min = tree[k - 1 - j] - tree[j];
    }
  }
  cout << min;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  int k;
  int min;
  cin >> n >> k;
  vector<int> tree(n);
  for (int i = 0; i < n; i++) {
    cin >> tree[i];
  }
  sort(tree.begin(), tree.end());
  min = tree[k - 1] - tree[0];
  for (int j = 0; j < n - k + 1; j++) {
    if (min > (tree[k - 1 + j] - tree[j])) {
      min = tree[k - 1 + j] - tree[j];
    }
  }
  cout << min;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N, K;
  cin >> N >> K;

  vector<long long int> h(N);

  for (long long int i = 0; i, N; i++) {
    cin >> h[i];
  }
  sort(h.begin(), h.end());

  long long int min = 1000000000;

  for (long long int i = 0; i < N - K + 1; i++) {
    if (min > h[i + K - 1] - h[i]) {
      min = h[i + K - 1] - h[i];
    }
  }

  cout << min << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N, K;
  cin >> N >> K;

  vector<long long int> h(N);

  for (long long int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h.begin(), h.end());

  long long int min = 1000000000;

  for (long long int i = 0; i < N - K + 1; i++) {
    if (min > h[i + K - 1] - h[i]) {
      min = h[i + K - 1] - h[i];
    }
  }

  cout << min << endl;
}
Problem p03208 (C++)

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

int n, k, a[12345], s = 1000000000;

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n - k + 1; i++)
    s = min(s, a[i + k - 1] - a[i]);
  printf("%d", s);
  return 0;
}

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

int n, k, a[123456], s = 1000000000;

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n - k + 1; i++)
    s = min(s, a[i + k - 1] - a[i]);
  printf("%d", s);
  return 0;
}
Problem p03208 (C++)

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

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

  vector<int> h(N);
  for (int i = 0; i < N; i++)
    cin >> h[i];

  sort(h.begin(), h.end());
  int max, min;
  int ans = h[2] - h[0];
  for (int i = 1; i < (N - 3); i++) {
    min = h[i];
    max = h[i + K - 1];
    if (ans > (max - min)) {
      ans = (max - min);
    }
  }

  cout << ans << endl;

  return 0;
}

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

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

  vector<int> h(N);
  for (int i = 0; i < N; i++)
    cin >> h[i];

  sort(h.begin(), h.end());
  int ans = h[K - 1] - h[0];
  for (int i = 1; i < (N - (K - 1)); i++) {
    if (ans > (h[i + K - 1] - h[i])) {
      ans = (h[i + K - 1] - h[i]);
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

typedef long long ll;

#define rep(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define INF 10e12
#define MAX 51000
#define all(x) (x).begin(), (x).end()
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))

using namespace std;

const long long MOD = 1e9 + 7;

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> h(n);
  rep(i, 0, n) cin >> h[i];
  sort(all(h));
  ll ans = MOD;
  rep(i, 0, n) ans = min(ans, abs(h[i] - h[i + k - 1]));
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

typedef long long ll;

#define rep(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define INF 10e12
#define MAX 51000
#define all(x) (x).begin(), (x).end()
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))

using namespace std;

const long long MOD = 1e9 + 7;

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> h(n);
  rep(i, 0, n) cin >> h[i];
  sort(all(h));
  ll ans = MOD;
  rep(i, 0, n - k + 1) ans = min(ans, abs(h[i] - h[i + k - 1]));
  cout << ans << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define LL long long
#define MAXN 110
#define debug(x) cerr << #x << " " << x << endl
using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    f = ch == '-' ? -1 : 1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

const int MOD = 1e9 + 7;
int n;
int p[MAXN];
int k;
int main() {
  n = read();
  k = read();
  for (int i = 1; i <= n; ++i) {
    p[i] = read();
  }
  sort(p + 1, p + n + 1);
  int minn = MOD;
  for (int i = 1; i <= n - k + 1; ++i) {
    minn = min(minn, p[i + k - 1] - p[i]);
  }
  cout << minn << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define LL long long
#define MAXN 100010
#define debug(x) cerr << #x << " " << x << endl
using namespace std;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    f = ch == '-' ? -1 : 1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

const int MOD = 1e9 + 7;
int n;
int p[MAXN];
int k;
int main() {
  n = read();
  k = read();
  for (int i = 1; i <= n; ++i) {
    p[i] = read();
  }
  sort(p + 1, p + n + 1);
  int minn = MOD;
  for (int i = 1; i <= n - k + 1; ++i) {
    minn = min(minn, p[i + k - 1] - p[i]);
  }
  cout << minn << endl;
  return 0;
}
Problem p03208 (Python)

Buggy submission (status: Runtime Error):
n, k = map(int, input().split())
h = sorted([int(input()) for _ in range(n)])
ans = 1000000000

for i in range(n - k + 1):
    ans = min(ans, h[i + k - 11] - h[i])

print(ans)


Fixed submission:
n, k = map(int, input().split())
h = sorted([int(input()) for _ in range(n)])
ans = 1000000000

for i in range(n - k + 1):
    ans = min(ans, h[i + k - 1] - h[i])

print(ans)
Problem p03208 (C++)

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

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

  vector<int> h(n);

  for (int i = 0; i < n; i++)
    cin >> h[i];

  sort(h.begin(), h.end());

  int mi = 10000000000;

  for (int i = 0; i < n - 2; i++) {

    if (h[k + i - 1] - h[i] < mi) {
      mi = h[k + i - 1] - h[i];
    }
  }

  cout << mi << endl;

  return 0;
}

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

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

  vector<int> h(n);

  for (int i = 0; i < n; i++)
    cin >> h[i];

  sort(h.begin(), h.end());

  int mi = 10000000000;

  for (int i = 0; i < n + 1 - k; i++) {

    if (h[k + i - 1] - h[i] < mi) {
      mi = h[k + i - 1] - h[i];
    }
  }

  cout << mi << endl;

  return 0;
}
Problem p03208 (C++)

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

using namespace std;

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

#define fs first
#define sc second

ll si[51];
ll pa[51];

int main() {
  int N, K;
  int h[20000];
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h, h + N);
  int res = 1000000007;
  for (int i = 0; i < N; i++) {
    if (i + K - 1 >= N)
      break;
    res = min(res, h[i + K - 1] - h[i]);
  }
  cout << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <float.h>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

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

#define fs first
#define sc second

ll si[51];
ll pa[51];

int main() {
  int N, K;
  int h[200000];
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h, h + N);
  int res = 1000000007;
  for (int i = 0; i < N; i++) {
    if (i + K - 1 >= N)
      break;
    res = min(res, h[i + K - 1] - h[i]);
  }
  cout << res << endl;

  return 0;
}
Problem p03209 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
long long a[51];
long long p[51];
long long f(int n, long long x) {
  if (n == 0)
    if (x <= 0)
      return 0;
    else
      return 1;
  else if (x <= 1 + a[n - 1])
    return f(n - 1, x - 1);
  else
    return p[n - 1] + 1 + f(n - 1, x - 2 - a[n - 1]);
}
int main() {
  int n;
  long long x;
  freopen("burgers.in", "r", stdin);
  freopen("burgers.out", "w", stdout);
  scanf("%d%lld", &n, &x);
  p[0] = 1;
  a[0] = 1;
  for (int i = 1; i <= n; i++) {
    a[i] = a[i - 1] * 2 + 3;
    p[i] = p[i - 1] * 2 + 1;
  }
  printf("%lld", f(n, x));
  return 0;
}


Fixed submission:
#include <cstdio>
long long a[51];
long long p[51];
long long f(int n, long long x) {
  if (n == 0)
    if (x <= 0)
      return 0;
    else
      return 1;
  else if (x <= 1 + a[n - 1])
    return f(n - 1, x - 1);
  else
    return p[n - 1] + 1 + f(n - 1, x - 2 - a[n - 1]);
}
int main() {
  int n;
  long long x;
  // freopen("burgers.in" , "r" , stdin);
  // freopen("burgers.out" , "w" , stdout);
  scanf("%d%lld", &n, &x);
  p[0] = 1;
  a[0] = 1;
  for (int i = 1; i <= n; i++) {
    a[i] = a[i - 1] * 2 + 3;
    p[i] = p[i - 1] * 2 + 1;
  }
  printf("%lld", f(n, x));
  return 0;
}
Problem p03209 (C++)

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

vector<ll> a(51), p(51);

ll f(ll N, ll X) {
  if (X == 1) {
    return 0;
  } else if (X <= 1 + a[N - 1]) {
    return f(N - 1, X - 1);
  } else {
    return p[N - 1] + 1 + f(N - 1, X - 2 - a[N - 1]);
  }
}

int main() {
  ll n, x;
  cin >> n >> x;
  a[0] = 1;
  p[0] = 1;
  for (int i = 1; i <= 50; ++i) {
    a[i] = 2 * a[i - 1] + 3;
    p[i] = 2 * p[i - 1] + 1;
  }
  cout << f(n, x) << "\n";
  return 0;
}

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

vector<ll> a(51), p(51);

ll f(ll N, ll X) {
  if (N == 0 && X >= 1) {
    return 1;
  } else if (X <= 0) {
    return 0;
  } else if (X <= 1 + a[N - 1]) {
    return f(N - 1, X - 1);
  } else {
    return p[N - 1] + 1 + f(N - 1, X - 2 - a[N - 1]);
  }
}

int main() {
  ll n, x;
  cin >> n >> x;
  a[0] = 1;
  p[0] = 1;
  for (int i = 1; i <= 50; ++i) {
    a[i] = 2 * a[i - 1] + 3;
    p[i] = 2 * p[i - 1] + 1;
  }
  cout << f(n, x) << "\n";
  return 0;
}
Problem p03209 (C++)

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

vector<long long> P(55), ALL(55);

long long calc(long long N, long long X) {
  if (X == 0)
    return 0;
  else if (1 < X && X < ALL.at(N - 1) + 2)
    return calc(N - 1, X - 1);
  else if (X == ALL.at(N - 1) + 2)
    return P.at(N - 1) + 1;
  else if (ALL.at(N - 1) + 2 < X && X < 2 * ALL.at(N - 1) + 3)
    return P.at(N - 1) + 1 + calc(N - 1, X - 2 - ALL.at(N - 1));
  else if (X == 2 * ALL.at(N - 1) + 3)
    return 2 * P.at(N - 1) + 1;
}

int main() {

  long long N, X;
  cin >> N >> X;

  P.at(0) = 1;
  ALL.at(0) = 1;
  for (int i = 1; i <= N; i++) {
    P.at(i) = 2 * P.at(i - 1) + 1;
    ALL.at(i) = 2 * ALL.at(i - 1) + 3;
  }

  long long ans = calc(N, X);
  cout << ans << endl;
}


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

vector<long long> P(55), ALL(55);

long long calc(long long N, long long X) {
  if (N == 0) {
    if (X <= 0)
      return 0;
    else
      return 1;
  } else if (X == 0)
    return 0;
  else if (1 < X && X < ALL.at(N - 1) + 2)
    return calc(N - 1, X - 1);
  else if (X == ALL.at(N - 1) + 2)
    return P.at(N - 1) + 1;
  else if (ALL.at(N - 1) + 2 < X && X < 2 * ALL.at(N - 1) + 3)
    return P.at(N - 1) + 1 + calc(N - 1, X - 2 - ALL.at(N - 1));
  else if (X == 2 * ALL.at(N - 1) + 3)
    return 2 * P.at(N - 1) + 1;
}

int main() {

  long long N, X;
  cin >> N >> X;

  P.at(0) = 1;
  ALL.at(0) = 1;
  for (int i = 1; i <= N; i++) {
    P.at(i) = 2 * P.at(i - 1) + 1;
    ALL.at(i) = 2 * ALL.at(i - 1) + 3;
  }

  long long ans = calc(N, X);
  cout << ans << endl;
}
Problem p03209 (C++)

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

long long f(int N, long long X) {
  vector<long long> sum = {1};
  vector<long long> pat = {1};
  for (int i = 0; i < N; i++) {
    sum.push_back(sum[i] * 2 + 3);
    pat.push_back(pat[i] * 2 + 1);
  }
  if (X == 1) {
    return 0;
  } else if (X <= 1 + sum[N - 1]) {
    return f(N - 1, X - 1);
  } else {
    return pat[N - 1] + 1 + f(N - 1, X - 2 - sum[N - 1]);
  }
}

int main() {
  int N;
  cin >> N;
  long long X;
  cin >> X;
  cout << f(N, X) << endl;
}

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

long long f(int N, long long X) {
  vector<long long> sum = {1};
  vector<long long> pat = {1};
  for (int i = 0; i < N; i++) {
    sum.push_back(sum[i] * 2 + 3);
    pat.push_back(pat[i] * 2 + 1);
  }
  if (N == 0) {
    if (X <= 0) {
      return 0;
    } else {
      return 1;
    }
  } else if (X <= 1 + sum[N - 1]) {
    return f(N - 1, X - 1);
  } else {
    return pat[N - 1] + 1 + f(N - 1, X - 2 - sum[N - 1]);
  }
}

int main() {
  int N;
  cin >> N;
  long long X;
  cin >> X;
  cout << f(N, X) << endl;
}
Problem p03209 (C++)

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int64 i = 0; i < (n); i++)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int64 mod = 1e9 + 7;
int64 N, X;
vector<int64> lay, pat;

int64 dfs(int64 lev, int64 rest) {
  if (lev < 0)
    return 0;
  if (rest == 0)
    return 0;
  if (rest == 1)
    return lev == 0;
  if (lay[lev] <= rest)
    return pat[lev];
  return dfs(lev - 1, rest - 1) + dfs(lev - 1, rest - lay[lev - 1] - 2) +
         (rest >= lay[lev - 1] + 2 ? 1 : 0);
}

int main(void) {
  cin >> N >> X;
  lay.resize(N + 1);
  pat.resize(N + 1);
  lay[0] = 1;
  pat[0] = 1;
  FOR(i, 1, N + 1) {
    lay[i] = lay[i - 1] * 2 + 3;
    pat[i] = pat[i - 1] * 2 + 1;
  }
  cout << dfs(N, X) << endl;
}


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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int64 i = 0; i < (n); i++)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int64 mod = 1e9 + 7;
int64 N, X;
vector<int64> lay, pat;

int64 dfs(int64 lev, int64 rest) {
  if (lev < 0)
    return 0;
  if (rest <= 0)
    return 0;
  if (rest == 1)
    return lev == 0;
  if (lay[lev] <= rest)
    return pat[lev];
  return dfs(lev - 1, rest - 1) + dfs(lev - 1, rest - lay[lev - 1] - 2) +
         (rest >= lay[lev - 1] + 2 ? 1 : 0);
}

int main(void) {
  cin >> N >> X;
  lay.resize(N + 1);
  pat.resize(N + 1);
  lay[0] = 1;
  pat[0] = 1;
  FOR(i, 1, N + 1) {
    lay[i] = lay[i - 1] * 2 + 3;
    pat[i] = pat[i - 1] * 2 + 1;
  }
  cout << dfs(N, X) << endl;
}
Problem p03209 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
typedef long long li;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define df 0
template <class T> void print(const T &t) { cout << t << "\n"; }
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
  cout << t;
  if (sizeof...(ts))
    cout << " ";
  print(ts...);
}

li solve(li x, const vector<li> &bgr, const vector<li> &pty, int n) {
  if (x <= n)
    return 0;
  else if (x >= bgr[n - 1] + 2) {
    return pty[n - 1] + solve(x - bgr[n - 1] - 2, bgr, pty, n - 1) + 1;
  } else if (x == bgr[n - 1] + 1) {
    return pty[n - 1];
  } else {
    return solve(x - 1, bgr, pty, n - 1);
  }
}

int main() {
  int n;
  li x;
  cin >> n >> x;
  vector<li> bgr(n + 1, 0), pty(n + 1, 0);
  bgr[0] = 1;
  pty[0] = 1;
  rep(i, n) {
    bgr[i + 1] = bgr[i] * 2 + 3;
    pty[i + 1] = pty[i] * 2 + 1;
  }
  print(solve(x, bgr, pty, n));
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
typedef long long li;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define df 0
template <class T> void print(const T &t) { cout << t << "\n"; }
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
  cout << t;
  if (sizeof...(ts))
    cout << " ";
  print(ts...);
}

li solve(li x, const vector<li> &bgr, const vector<li> &pty, int n) {
  if (x <= n)
    return 0;
  else if (x == bgr[n]) {
    return pty[n];
  } else if (x >= bgr[n - 1] + 2) {
    return pty[n - 1] + solve(x - bgr[n - 1] - 2, bgr, pty, n - 1) + 1;
  } else if (x == bgr[n - 1] + 1) {
    return pty[n - 1];
  } else {
    return solve(x - 1, bgr, pty, n - 1);
  }
}

int main() {
  int n;
  li x;
  cin >> n >> x;
  vector<li> bgr(n + 1, 0), pty(n + 1, 0);
  bgr[0] = 1;
  pty[0] = 1;
  rep(i, n) {
    bgr[i + 1] = bgr[i] * 2 + 3;
    pty[i + 1] = pty[i] * 2 + 1;
  }
  print(solve(x, bgr, pty, n));
}
Problem p03209 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

long long counter = 0;

void set(long long n, long long x) {
  if (n == 0 & x == 1) {
    counter += 1;
    return;
  } else if (x == 1 || x == 0) {
    return;
  } else if (x >= pow(2, n + 1) - 1) {
    counter += pow(2, n);
    set(n - 1, x - pow(2, n + 1) + 1);
    return;
  } else {
    set(n - 1, x - 1);
    return;
  }
}

int main() {
  long long N;
  long long X;
  cin >> N >> X;
  set(N, X);
  cout << counter << endl;
}


Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;

long long counter = 0;

void set(long long n, long long x) {
  if (n == 0) {
    if (x == 0) {
      return;
    } else {
      counter += 1;
      return;
    }
  } else if (x == 1 || x == 0) {
    return;
  } else if (x >= pow(2, n + 1) - 1) {
    counter += pow(2, n);
    set(n - 1, x - pow(2, n + 1) + 1);
    return;
  } else {
    set(n - 1, x - 1);
    return;
  }
}

int main() {
  long long N;
  long long X;
  cin >> N >> X;
  set(N, X);
  cout << counter << endl;
}
Problem p03209 (C++)

Buggy submission (status: Runtime Error):
/*
[abc115] D - Christmas
*/

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

int N;
ll X;

ll count(int level, ll idx) {
  assert(0 <= idx && idx < (1LL << (level + 2)) - 3);
  if (level == 0) {
    return idx;
  }
  ll len = (1LL << (level + 2)) - 3;
  if (idx == 0) {
    return 0;
  }
  if (idx <= len / 2) {
    return count(level - 1, idx - 1);
  }
  if (idx < len) {
    return (1LL << (level)) - 1 + 1 + count(level - 1, idx - len / 2 - 1);
  }
  return (1LL << (level + 1)) - 1;
}

ll solve() { return count(N, X); }

int main() {
  cin >> N >> X;

  cout << solve() << endl;

  return 0;
}


Fixed submission:
/*
[abc115] D - Christmas
*/

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

int N;
ll X;

ll count(int level, ll idx) {
  if (level == 0) {
    return idx;
  }
  ll len = (1LL << (level + 2)) - 3;
  if (idx == 0) {
    return 0;
  }
  if (idx <= len / 2) {
    return count(level - 1, idx - 1);
  }
  if (idx < len) {
    return (1LL << (level)) - 1 + 1 + count(level - 1, idx - len / 2 - 1);
  }
  return (1LL << (level + 1)) - 1;
}

ll solve() { return count(N, X); }

int main() {
  cin >> N >> X;

  cout << solve() << endl;

  return 0;
}
Problem p03209 (C++)

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

using namespace std;
typedef long long ll;

vector<ll> p = {1}, b = {1};

ll solve(ll n, ll x) {
  if (x == 1)
    return 0;
  else if (x <= 1 + b[n - 1])
    return solve(n - 1, x - 1);
  else
    return p[n - 1] + 1 + solve(n - 1, x - 2 - b[n - 1]);
}

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

  for (ll i = 1; i <= n; i++) {
    p.push_back(2 * p[i - 1] + 1);
    b.push_back(2 * b[i - 1] + 3);
  }

  cout << solve(n, x) << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

vector<ll> p = {1}, b = {1};

ll solve(ll n, ll x) {
  if (n == 0)
    return x <= 0 ? 0 : 1;
  else if (x <= 1 + b[n - 1])
    return solve(n - 1, x - 1);
  else
    return p[n - 1] + 1 + solve(n - 1, x - 2 - b[n - 1]);
}

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

  for (ll i = 1; i <= n; i++) {
    p.push_back(2 * p[i - 1] + 1);
    b.push_back(2 * b[i - 1] + 3);
  }

  cout << solve(n, x) << endl;
}
Problem p03209 (C++)

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

ll all(ll n) {
  if (n == 0)
    return 1;
  else
    return all(n - 1) * 2 + 3;
}

ll apy(ll n) {
  if (n == 0)
    return 1;
  return apy(n - 1) * 2 + 1;
}

ll pty(ll n, ll k) {
  ll a = all(n - 1);
  ll sum = (k > a + 1);
  if (k <= 0 || k <= n)
    return 0;
  if (n == 0)
    return 1;
  if (k >= all(n))
    return apy(n - 1) * 2 + 1;
  if (k <= a)
    return pty(n - 1, k - 1);
  sum += apy(n - 1);
  if (k > a + 2)
    sum += pty(n - 1, k - (a + 2));
  return sum;
}

int main() {
  ll n, x;
  cin >> n >> x;
  cout << pty(n, x) << endl;
  return 0;
}

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

ll all(ll n) {
  if (n < 0)
    return 0;
  if (n == 0)
    return 1;
  else
    return all(n - 1) * 2 + 3;
}

ll apy(ll n) {
  if (n == 0)
    return 1;
  return apy(n - 1) * 2 + 1;
}

ll pty(ll n, ll k) {
  ll a = all(n - 1);
  ll sum = (k > a + 1);
  if (k <= 0 || k <= n)
    return 0;
  if (n == 0)
    return 1;
  if (k >= all(n))
    return apy(n - 1) * 2 + 1;
  if (k <= a)
    return pty(n - 1, k - 1);
  sum += apy(n - 1);
  if (k > a + 2)
    sum += pty(n - 1, k - (a + 2));
  return sum;
}

int main() {
  ll n, x;
  cin >> n >> x;
  cout << pty(n, x) << endl;
  return 0;
}
Problem p03209 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
map<pair<ll, ll>, ll> mp;
ll f(ll i, ll x) {
  if (x-- == 0)
    return 0;
  if (i-- == 0)
    return 1;
  ll a = (1LL << i + 2) - 3;
  ll res;
  if (!x)
    res = 0;
  else if (x < a + 1)
    res = f(i, x);
  else
    res = f(i, a) + f(i, x - a - 1) + 1;
  mp[{i + 1, x + 1}] = res;
  return res;
}
int main() {
  ll n, x;
  cin >> n >> x;
  cout << f(n, x) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
map<pair<ll, ll>, ll> mp;
ll f(ll i, ll x) {
  if (mp.count({i, x}))
    return mp[{i, x}];
  if (x-- == 0)
    return 0;
  if (i-- == 0)
    return 1;
  ll a = (1LL << i + 2) - 3;
  ll res;
  if (!x)
    res = 0;
  else if (x < a + 1)
    res = f(i, x);
  else
    res = f(i, a) + f(i, x - a - 1) + 1;
  mp[{i + 1, x + 1}] = res;
  return res;
}
int main() {
  ll n, x;
  cin >> n >> x;
  cout << f(n, x) << endl;
}
Problem p03209 (C++)

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

ll ttl(ll n) {
  if (n == 0)
    a.at(n) = 1;
  else
    a.at(n) = 3 + 2 * ttl(n - 1);
  return a.at(n);
}

ll dfs(int n, ll i) {
  if (!n)
    return 1;

  if (i == 1)
    return 0;
  if (2 <= i && i <= 1 + a.at(n - 1))
    return dfs(n - 1, i - 1);
  if (i == 2 + a.at(n - 1))
    return dfs(n - 1, a.at(i - 1)) + 1;
  if (3 + a.at(n - 1) <= i && i <= 2 + 2 * a.at(n - 1))
    return dfs(n - 1, a.at(n - 1)) + 1 + dfs(n - 1, i - 2 - a.at(n - 1));
  else
    return 1 + 2 * dfs(n - 1, a.at(n - 1));
}

int main() {
  ll n, x;
  cin >> n >> x;
  ttl(n);

  cout << dfs(n, x);
}

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

ll ttl(ll n) {
  if (n == 0)
    a.at(n) = 1;
  else
    a.at(n) = 3 + 2 * ttl(n - 1);
  return a.at(n);
}

ll dfs(int n, ll i) {
  if (!n)
    return 1;

  if (i == 1)
    return 0;
  if (2 <= i && i <= 1 + a.at(n - 1))
    return dfs(n - 1, i - 1);
  if (i == 2 + a.at(n - 1))
    return dfs(n - 1, a.at(n - 1)) + 1;
  if (3 + a.at(n - 1) <= i && i <= 2 + 2 * a.at(n - 1))
    return dfs(n - 1, a.at(n - 1)) + 1 + dfs(n - 1, i - 2 - a.at(n - 1));
  else
    return 1 + 2 * dfs(n - 1, a.at(n - 1));
}

int main() {
  ll n, x;
  cin >> n >> x;
  ttl(n);

  cout << dfs(n, x);
}
Problem p03209 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, X;
  cin >> N >> X;
  if (N == 1) {
    if (X < 5)
      cout << X - 1 << endl;
    else
      cout << X - 2 << endl;
    return 0;
  }
  // P /-1- /1
  // BPPPB 1-1/-3- (2/3)/3+1*2=5
  // BBPPPBPBPPPBB2-1-1-2/-3-1-3 (6/7)/3+5*2=13
  // BBBPPPBPBPPPBBPBBPPPBPBPPPBBB3-1-1-2-2-1-1-3/-3-1-3-1-3-1-3/
  // (14/15)3+13*2=29
  // BBBBPPPBPBPPPBBPBBPPPBPBPPPBBBPBBBPPPBPBPPPBBPBBPPPBPBPPPBBBB
  // (30/31)/3+29*2=61
  // 4-1-1-2-2-1-1-3-3-1-1-2-2-1-1-4/-3-1-3-1-3-1-3-1-3-1-3-1-3-1-3
  // BBBBBPPPBPBPPPBBPBBPPPBPBPPPBBBPBBBPPPBPBPPPBBPBBPPPBPBPPPBBBBPBBBBPPPBPBPPPBBPBBPPPBPBPPPBBBPBBBPPPBPBPPPBBPBBPPPBPBPPPBBBBB
  // B>P>B
  // 総数出しとく。抜粋数に対してどうする？
  // B0P0B=1
  // B1P1B=2
  // B2P2B=3
  // B3P3B=4
  //-----=50
  // 2文探索みたいな感じで終了地点を探す。～以下にあるなら総数/2を足してそうじゃないなら範囲狭めるだけ
  vector<ll> Z(N, 1);
  for (int i = 1; i <= N; i++) {
    Z[i] = Z[i - 1] * 2 + 3;
  }
  ll ans = 0, h = Z[N], l = 1, mid = (h + l) / 2;
  while (1) {
    if (mid <= X) {
      l = mid;
      ans += (h - l) / 2 + 1;
    } else if (mid > X)
      h = mid;
    l++;
    h--;
    if (l > h)
      break;
    mid = (h + l) / 2;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, X;
  cin >> N >> X;
  if (N == 1) {
    if (X < 5)
      cout << X - 1 << endl;
    else
      cout << X - 2 << endl;
    return 0;
  }
  // P /-1- /1
  // BPPPB 1-1/-3- (2/3)/3+1*2=5
  // BBPPPBPBPPPBB2-1-1-2/-3-1-3 (6/7)/3+5*2=13
  // BBBPPPBPBPPPBBPBBPPPBPBPPPBBB3-1-1-2-2-1-1-3/-3-1-3-1-3-1-3/
  // (14/15)3+13*2=29
  // BBBBPPPBPBPPPBBPBBPPPBPBPPPBBBPBBBPPPBPBPPPBBPBBPPPBPBPPPBBBB
  // (30/31)/3+29*2=61
  // 4-1-1-2-2-1-1-3-3-1-1-2-2-1-1-4/-3-1-3-1-3-1-3-1-3-1-3-1-3-1-3
  // BBBBBPPPBPBPPPBBPBBPPPBPBPPPBBBPBBBPPPBPBPPPBBPBBPPPBPBPPPBBBBPBBBBPPPBPBPPPBBPBBPPPBPBPPPBBBPBBBPPPBPBPPPBBPBBPPPBPBPPPBBBBB
  // B>P>B
  // 総数出しとく。抜粋数に対してどうする？
  // B0P0B=1
  // B1P1B=2
  // B2P2B=3
  // B3P3B=4
  //-----=50
  // 2文探索みたいな感じで終了地点を探す。～以下にあるなら総数/2を足してそうじゃないなら範囲狭めるだけ
  vector<ll> Z(N + 1, 1);
  for (int i = 1; i <= N; i++) {
    Z[i] = Z[i - 1] * 2 + 3;
  }
  ll ans = 0, h = Z[N], l = 1, mid = (h + l) / 2;
  while (1) {
    if (mid <= X) {
      l = mid;
      ans += (h - l) / 2 + 1;
    } else if (mid > X)
      h = mid;
    l++;
    h--;
    if (l > h)
      break;
    mid = (h + l) / 2;
  }
  cout << ans << endl;
}
Problem p03209 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <istream>
#include <random>
#include <sstream>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;
typedef long long ll;

ll total = 1;
vector<ll> length;
vector<ll> P_length;

ll sum(ll N, ll X) {
  if (N == 0) {
    if (X <= 0) {
      return 0;
    } else {
      return 1;
    }
  } else if (X <= 1 + length[N - 1]) {
    return sum(N - 1, X - 1);
  } else {
    return P_length[N - 1] + 1 + sum(N - 1, X - 2 - length[N - 1]);
  }
}

int main() {

  ll N, X;
  cin >> N >> X;

  // length
  length = vector<ll>(N, 1);
  P_length = vector<ll>(N, 1);
  for (ll i = 1; i <= N; i++) {
    length[i] = 2 * length[i - 1] + 3;
    P_length[i] = 2 * P_length[i - 1] + 1;
  }
  /*
  cout << length[N] << endl;
  //バンズのとこだけ
  if(N >= X){
      cout << "0" << endl;
      return 0;
  }
  //パティをすべて食べれるとき
  if(N >= length[N] - X ){
      cout << P_length[N] << endl;
      return 0;
  }
  //パティを半分くらいまで食べれるとき
  if(length[N - 1] == X){
      cout << P_length[N-1] << endl;
      return 0;
  }
  //半分より少し多めに食べれるとき
  if(length[N - 1] + 1 <= X || length[N - 1] + (N - 1) >= X){
      cout << P_length[N-1] + 1 << endl;
      return 0;
  }
  */
  // それ以外
  // レベルLバーガーの中にL-1バーガー更にL-1バーガーの中に．．．というとき
  total = sum(N, X);
  cout << total << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <istream>
#include <random>
#include <sstream>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;
typedef long long ll;

ll total = 1;
vector<ll> length;
vector<ll> P_length;

ll sum(ll N, ll X) {
  if (N == 0) {
    if (X <= 0) {
      return 0;
    } else {
      return 1;
    }
  } else if (X <= 1 + length[N - 1]) {
    return sum(N - 1, X - 1);
  } else {
    return P_length[N - 1] + 1 + sum(N - 1, X - 2 - length[N - 1]);
  }
}

int main() {

  ll N, X;
  cin >> N >> X;

  // length
  length = vector<ll>(N + 1, 1);
  P_length = vector<ll>(N + 1, 1);
  for (ll i = 1; i <= N; i++) {
    length[i] = 2 * length[i - 1] + 3;
    P_length[i] = 2 * P_length[i - 1] + 1;
  }
  /*
  cout << length[N] << endl;
  //バンズのとこだけ
  if(N >= X){
      cout << "0" << endl;
      return 0;
  }
  //パティをすべて食べれるとき
  if(N >= length[N] - X ){
      cout << P_length[N] << endl;
      return 0;
  }
  //パティを半分くらいまで食べれるとき
  if(length[N - 1] == X){
      cout << P_length[N-1] << endl;
      return 0;
  }
  //半分より少し多めに食べれるとき
  if(length[N - 1] + 1 <= X || length[N - 1] + (N - 1) >= X){
      cout << P_length[N-1] + 1 << endl;
      return 0;
  }
  */
  // それ以外
  // レベルLバーガーの中にL-1バーガー更にL-1バーガーの中に．．．というとき
  total = sum(N, X);
  cout << total << endl;
}
Problem p03209 (C++)

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

using namespace std;

typedef long long ll;
vector<ll> num;
vector<ll> p;

// レベルN層の左からXにあるPの数
ll findNum(int N, ll X) {
  if (N == 0)
    return 1;

  if (X == 1)
    return 0;
  else if (X <= num[N] / 2) {
    return findNum(N - 1, X - 1);
  } else if (X == (num[N] / 2 + 1)) {
    return 1 + findNum(N - 1, X - 2);
  }

  return 1 + p[N - 1] + findNum(N - 1, X - 2 - num[N - 1]);
}

int main() {
  int N;
  ll X;
  cin >> N >> X;

  num.resize(N);
  p.resize(N);

  num[0] = 1;
  p[0] = 1;
  for (int i = 0; i < 50; ++i) {
    num[i + 1] = 2 * num[i] + 3;
    p[i + 1] = 2 * p[i] + 1;
  }

  ll ans = findNum(N, X);

  cout << ans << endl;

  return 0;
}


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

using namespace std;

typedef long long ll;
vector<ll> num;
vector<ll> p;

// レベルN層の左からXにあるPの数
ll findNum(int N, ll X) {
  if (N == 0)
    return 1;

  if (X == 1)
    return 0;
  else if (X <= num[N] / 2) {
    return findNum(N - 1, X - 1);
  } else if (X == (num[N] / 2 + 1)) {
    return 1 + findNum(N - 1, X - 2);
  }

  return 1 + p[N - 1] + findNum(N - 1, X - 2 - num[N - 1]);
}

int main() {
  int N;
  ll X;
  cin >> N >> X;

  num.resize(50);
  p.resize(50);

  num[0] = 1;
  p[0] = 1;
  for (int i = 0; i < 50; ++i) {
    num[i + 1] = 2 * num[i] + 3;
    p[i + 1] = 2 * p[i] + 1;
  }

  ll ans = findNum(N, X);

  cout << ans << endl;

  return 0;
}
Problem p03209 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1LL << 30;
#define MOD 1000000007LL

ll N, X;
vll length(51);
vll pnum(51);

ll func(ll lv, ll x) {
  if (x == length.at(lv))
    return pnum.at(lv);
  ll xx;
  if (x < length.at(lv) / 2) {
    xx = x;
    if (xx == 1)
      return 0;
    return func(lv - 1, xx - 1);
  } else {
    xx = length.at(lv) - x;
    if (xx == 1)
      return pnum.at(lv);
    return pnum.at(lv) - func(lv - 1, xx - 1);
  }
}

int main() {
  cin >> N >> X;
  length.at(0) = 1LL;
  pnum.at(0) = 1LL;
  for (int i = 1; i <= N; i++) {
    length.at(i) = 2LL * length.at(i - 1) + 3LL;
    pnum.at(i) = 2LL * pnum.at(i - 1) + 1LL;
  }
  cout << func(N, X) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1LL << 30;
#define MOD 1000000007LL

ll N, X;
vll length(51);
vll pnum(51);

ll func(ll lv, ll x) {
  if (x == length.at(lv))
    return pnum.at(lv);
  ll xx;
  if (x <= length.at(lv) / 2) {
    xx = x;
    if (xx == 1)
      return 0;
    return func(lv - 1, xx - 1);
  } else {
    xx = length.at(lv) - x;
    if (xx == 1)
      return pnum.at(lv);
    return pnum.at(lv) - func(lv - 1, xx - 1);
  }
}

int main() {
  cin >> N >> X;
  length.at(0) = 1LL;
  pnum.at(0) = 1LL;
  for (int i = 1; i <= N; i++) {
    length.at(i) = 2LL * length.at(i - 1) + 3LL;
    pnum.at(i) = 2LL * pnum.at(i - 1) + 1LL;
  }
  cout << func(N, X) << endl;
}
Problem p03209 (C++)

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

/*
L(0) := P
L(n) := B L(n-1) P L(n-1) B
*/

ll f(int n) { return n == 0 ? 1 : f(n - 1) * 2 + 3; }

ll g(int n) { return n == 0 ? 1 : g(n - 1) * 2 + 1; }

ll calc(int n, ll x) {
  if (x <= 0)
    return 0;
  if (n == 0) {
    assert(x == 1);
    return 1;
  }
  ll fn = f(n);
  if (x > fn / 2)
    return g(n - 1) + 1 + calc(n - 1, x - (fn / 2 + 1));
  else
    return calc(n - 1, x - 1);
}

int main(void) {
  int n;
  ll x;
  cin >> n >> x;
  cout << calc(n, x) << endl;
  return 0;
}


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

/*
L(0) := P
L(n) := B L(n-1) P L(n-1) B
*/

ll f(int n) { return n == 0 ? 1 : f(n - 1) * 2 + 3; }

ll g(int n) { return n == 0 ? 1 : g(n - 1) * 2 + 1; }

ll calc(int n, ll x) {
  if (x <= 0)
    return 0;
  if (n == 0) {
    // assert(x==1);
    return 1;
  }
  ll fn = f(n);
  if (x > fn / 2)
    return g(n - 1) + 1 + calc(n - 1, x - (fn / 2 + 1));
  else
    return calc(n - 1, x - 1);
}

int main(void) {
  int n;
  ll x;
  cin >> n >> x;
  cout << calc(n, x) << endl;
  return 0;
}
Problem p03209 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <chrono>
#include <climits>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<int, int>
#define debug(x) cerr << #x << ": " << x << ", "
#define debugln(x) cerr << #x << ": " << x << '\n'

using namespace std;
using ll = long long;
const int mod = 1e9 + 7;

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

  ll N, X;
  cin >> N >> X;

  vector<ll> vec(51);
  vector<ll> cnt(51);
  vec.at(0) = 1;
  cnt.at(0) = 1;
  for (int i = 1; i <= 50; i++) {
    vec.at(i) = vec.at(i - 1) * 2 + 3;
    cnt.at(i) = cnt.at(i - 1) * 2 + 1;
  }

  debugln(vec.at(50));
  debugln(cnt.at(50));

  ll ans = 0;

  for (int i = N; i >= 0; i--) {
    if (X >= vec.at(i)) {
      ans += cnt.at(i);
      break;
    }
    if (X >= vec.at(i - 1) + 2) {
      X -= vec.at(i - 1) + 2;
      ans += cnt.at(i - 1) + 1;
    } else if (X == vec.at(i - 1) + 1) {
      ans += cnt.at(i - 1);
      break;
    } else if (X >= 1) {
      X--;
    } else {
      break;
    }
    debug(i);
    debugln(X);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <chrono>
#include <climits>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<int, int>
#define debug(x) cerr << #x << ": " << x << ", "
#define debugln(x) cerr << #x << ": " << x << '\n'

using namespace std;
using ll = long long;
const int mod = 1e9 + 7;

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

  ll N, X;
  cin >> N >> X;

  vector<ll> vec(51);
  vector<ll> cnt(51);
  vec.at(0) = 1;
  cnt.at(0) = 1;
  for (int i = 1; i <= 50; i++) {
    vec.at(i) = vec.at(i - 1) * 2 + 3;
    cnt.at(i) = cnt.at(i - 1) * 2 + 1;
  }

  debugln(vec.at(50));
  debugln(cnt.at(50));

  ll ans = 0;

  for (int i = N; i >= 0; i--) {
    if (X >= vec.at(i)) {
      ans += cnt.at(i);
      break;
    }
    if (i == 0)
      continue;
    if (X >= vec.at(i - 1) + 2) {
      X -= vec.at(i - 1) + 2;
      ans += cnt.at(i - 1) + 1;
    } else if (X == vec.at(i - 1) + 1) {
      ans += cnt.at(i - 1);
      break;
    } else if (X >= 1) {
      X--;
    } else {
      break;
    }
    debug(i);
    debugln(X);
  }
  cout << ans << endl;
}