关闭

【Educational Codeforces Round 1A】【水题】Tricky Sum 1~n之和减去2的幂

标签: 题库-CF CodeForces水题
277人阅读 评论(0) 收藏 举报
分类:

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 ofn 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.



#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int n;
int main()
{
	scanf("%d",&casenum);
	for(casei=1;casei<=casenum;casei++)
	{
		scanf("%d",&n);
		LL sum=(LL)(n+1)*n/2;//全部都算为正
		for(int bit=1;bit<=n;bit<<=1)
		{
			sum-=bit;
			sum-=bit;
		}
		printf("%lld\n",sum);
	}
	return 0;
}
/*
【题意】
让你求出从1~n的和,但是2^k产生的贡献将是负的。

【类型】
水题

【分析】
先把2^k当做正数处理,求出(1+n)n/2,然后再枚举2^k,筛除即可

【时间复杂度&&优化】
O(log(n))

【数据】
1000000000

*/


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:269237次
    • 积分:8270
    • 等级:
    • 排名:第2396名
    • 原创:555篇
    • 转载:0篇
    • 译文:0篇
    • 评论:135条
    文章分类
    最新评论