题解
使用求和公式求出1到n的和 减去2倍出现过的二次幂的和即为答案
二次幂的和等于n范围内最大二进制*2-1 不断除以2求出最大二进制为2的几次方
AC代码
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
int T;
cin >> T;
while (T--)
{
int N;
cin >> N;
ll ans = (1LL + N) * N / 2; //1到n求和
int k = 0;
while (N)
N /= 2, k++; //n范围内最大k次幂
k = 1 << k; //2的k次方
cout << ans - (k - 1) * 2 << endl;
}
return 0;
}