snowy_smile的博客

还没有退役!再打一年!要进Final!有时间还会继续写题解~

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

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 转载还请注明下出处哦,谢谢^_^ https://blog.csdn.net/snowy_smile/article/details/49922947
上一篇【FOJ2207 11月月赛C】【DFS栈性质应用 离线处理】以撒的结合 从x到y路径上的第k个点 询问众多
下一篇【Educational Codeforces Round 1B】【字符串平移 水题】Queries on a String 字符串平移水题
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭