您的位置:首页 > 其它

Codeforces 598A Tricky Sum 【计数】

2015-11-15 18:27 369 查看
A. Tricky Sum

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

In this problem you are to calculate the sum of all integers from 1 to n,
but you should take all powers of two with minus in the sum.

For example, for n = 4 the sum is equal to  - 1 - 2 + 3 - 4 =  - 4,
because 1, 2 and 4 are 20, 21 and 22 respectively.

Calculate the answer for t values of n.

Input

The first line of the input contains a single integer t (1 ≤ t ≤ 100)
— the number of values of n to be processed.

Each of next t lines contains a single integer n (1 ≤ n ≤ 109).

Output

Print the requested sum for each of t integers n given
in the input.

Sample test(s)

input
2
4
1000000000


output
-4
499999998352516354


Note

The answer for the first sample is explained in the statement.

题意:给定一个数n,把1-n里面2的幂次数变为负数。求n个数之和。

思路:等比数列求出2的幂次数之和temp,用1-n总和 - 2*temp就可以了。

AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-4
#define MAXN (100+10)
#define MAXM (1000000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 100000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int main()
{
int t; Ri(t);
W(t)
{
LL n; Rl(n);
LL ans = n * (n + 1) / 2;
LL cnt = 0;
while(n)
{
cnt++;
n >>= 1;
}
LL temp = (1LL<<cnt)-1;
ans = ans - temp - temp;
Pl(ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: