cses

My solution for the coding challenges of cses.fi
git clone https://git.tronto.net/cses
Download | Log | Files | Refs | README

counting_bits_1146.cpp (264B)


      1 #include <iostream>
      2 
      3 int main() {
      4 	unsigned long long n, f, p, s{0};
      5 	std::cin >> n;
      6 
      7 	for (unsigned long long i = 1, m = n; i <= n; i <<= 1, m >>= 1) {
      8 		f = i * (m / 2);
      9 		p = (n % (2*i)) + 1;
     10 		p = p > i ? p - i : 0;
     11 		s += f + p;
     12 	}
     13 	std::cout << s << "\n";
     14 }