Need help in Problem C of AtCoder Beginner Contest 126

Revision en1, by haribhatt34, 2019-05-19 16:50:52

Link to the problem link to my solution

My Solution - ~~~~~

include <bits/stdc++.h>

using namespace std;

map<int, int> mp;

int findPower(int i, int k) { int t = i, cnt = 0; while (t <= k) { if (mp.find(t) != mp.end()) { cnt += mp[t]; break; } t *= 2; ++cnt; } return mp[i] = cnt; }

int main() { int n, k; cin >> n >> k; // power of 2 vector p2(31); p2[0] = 1; for (int i=1; i<p2.size(); ++i) p2[i] = p2[i-1] * 2;

long double b = 0.0;
for (int i=n; i>=1; --i)
{
int p = findPower(i, k);
p = p2[p];
b += (long double)1/p;
}
cout << ((long double)1/n) * b << '\n';
return 0;

} ~~~~~

I know my solution is unnecessarily long, I was trying to catch value, so to use them later. I don't know what I am doing wrong, moreover I am not getting correct decimal precision. For sample — 100000 5, the output should be — 0.999973749998, but my program is returning 0.9999687500, why could it be?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English haribhatt34 2019-05-19 16:52:18 4 Tiny change: 'bc126_c)\n[link to' -> 'bc126_c)\n\n[link to'
en2 English haribhatt34 2019-05-19 16:51:35 3 Tiny change: 'lution -\n~~~~~\n#' -> 'lution -\n\n~~~~~\n#'
en1 English haribhatt34 2019-05-19 16:50:52 1213 Initial revision (published)