fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cin >> n;
  7.  
  8. long long sum = n * (n+1)/2; // مجموع 1..n
  9. long long f = 1;
  10.  
  11. while(f <= n) {
  12. sum -= 2*f; // نطرح ضعف كل قوة 2
  13. f *= 2; // القوة التالية
  14. }
  15.  
  16. cout << sum << endl; // الناتج النهائي
  17. }
Success #stdin #stdout 0.01s 5316KB
stdin
2
stdout
-3