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

原创 2015年11月19日 09:02:53

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

*/


版权声明:题解中哪里写错请一定要指出来QwQ 转载还请注明下出处哦,谢谢^_^

相关文章推荐

【Codeforces Round 336 (Div 2) B】【水题 前缀和】Hamming Distance Sum 双字符串所有位置匹配下的差异数之和

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabyte...

【Codeforces Round 354 (Div 2)A】【水题 贪心】Nicholas and Permutation 恰好交换一次的pos[n]-pos[1]

A. Nicholas and Permutation time limit per test 1 second memory limit per test 256 megab...

【Educational Codeforces Round 1B】【字符串平移 水题】Queries on a String 字符串平移水题

B. Queries on a String time limit per test 2 seconds memory limit per test 256 megabytes...

【Educational Codeforces Round 3 B】【水题 基本容斥】The Best Gift n本书任选两个不能同一类型的方案数

B. The Best Gift time limit per test 2 seconds memory limit per test 256 megabytes ...

【Codeforces Round 331 (Div 2)A】【水题】Wilbur and Swimming Pool 给出矩形1~4个顶点让你还原矩形

Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabyte...

Codeforces Round #397(Div. 1 + Div. 2 combined)B. Code obfuscation【水题】

B. Code obfuscation time limit per test 2 seconds memory limit per test 512 megabytes inp...

【Codeforces Round 339 (Div 2)B】【水题】Gena's Code 若干10数+1特殊数的乘积

B. Gena's Code time limit per test 0.5 seconds memory limit per test 256 megabytes ...

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)A. A Serial Killer(水题)

题目: A. A Serial Killer time limit per test 2 seconds memory limit per test 256 mega...

Educational Codeforces Round 13——D. Iterated Linear Function(矩阵快速幂或普通快速幂水题)

D. Iterated Linear Function time limit per test 1 second memory limit per test 256 megabytes i...

【Educational Codeforces Round 1C】【计算几何-极角排序 atan2 long double】Nearest vectors 平面图上原点引出角度最小的两个

C. Nearest vectors time limit per test 2 seconds memory limit per test 256 megabytes ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)