Problem p03043 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(), a.end()
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
const long long MOD = 1000000007, INF = 1e17;

#define endl '\n'
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

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

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
//******************************************************************************

int N, K;
signed main() {
  // IOS();
  cin >> N >> K;
  vector<double> d(N, 1.0);
  rep(i, 0, N + 1) { d[i + 1] = d[i] / 2.0; }
  double ans = 0;
  rep(i, 1, N + 1) {
    int t = i;
    double res = 1.0 / N;
    while (t < K) {
      res /= 2.0;
      t *= 2;
    }
    ans += res;
  }
  printf("%.20lf", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(), a.end()
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
const long long MOD = 1000000007, INF = 1e17;

#define endl '\n'
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

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

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
//******************************************************************************

int N, K;
signed main() {
  // IOS();
  cin >> N >> K;
  double ans = 0;
  rep(i, 1, N + 1) {
    int t = i;
    double res = 1.0 / N;
    while (t < K) {
      res /= 2.0;
      t *= 2;
    }
    ans += res;
  }
  printf("%.20lf", ans);
}