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.

Example

Input
2
4
1000000000
Output
-4
499999998352516354

Note

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

题意     计算-1-2+3-4+5+6+7-8........这个公式。

解法     将所有2的次方存起来。

#include<cstdio>
long long a[40];
long long pow(int n)
{
if(n==0)
return 1;
else
{
long long k1=1;
for(int i=0;i<n;i++)
k1*=2;
return k1;
} }
int main()
{
int t;
scanf("%d",&t);
for(int i=0;i<33;i++)
a[i]=pow(i);
while(t--)
{
long long n;
scanf("%lld",&n);
long long sum;
sum=(1+n)*n/2;
int i; for( i=0;i<33;i++)
if(n<=a[i])
break;
if(n==a[i])
i=i+1;
long long sum1=0; for(int j=0;j<i;j++)
sum1+=a[j];
sum=sum-sum1*2;
printf("%lld\n",sum); }
}

Tricky Sum的更多相关文章

  1. Educational Codeforces Round 1 A. Tricky Sum 暴力

    A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...

  2. CodeForces - 598A Tricky Sum (数学,快速幂的运用)

    传送门: http://codeforces.com/problemset/problem/598/A A. Tricky Sum time limit per test 1 second memor ...

  3. codeforces 598A Tricky Sum

    题目链接:http://codeforces.com/contest/598/problem/A 题目分类:大数 题意:1到n 如果是2的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ...

  4. Codeforces--598A--Tricky Sum(数学)

     Tricky Sum Tricky SumCrawling in process... Crawling failed Time Limit:1000MS     Memory Limit:26 ...

  5. HPU周赛题目解析

    A - Wilbur and Swimming Pool Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  6. Educational Codeforces Round 1

    598A - Tricky Sum    20171103$$ans=\frac{n(n+1)}{2} - 2\sum_{k=0}^{\left \lfloor \log_2 n \right \rf ...

  7. 7/25 CSU-ACM2018暑假集训比赛1

    题目链接 [A - Tricky Sum ] In this problem you are to calculate the sum of all integers from 1 to n, but ...

  8. Codefoces 429 D. Tricky Function

    裸的近期点对.... D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  9. Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对

    D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...

随机推荐

  1. xml 文件转化Dictionary

    下面是xml文件 <?xml version="1.0" encoding="utf-8" ?><nodes> <国土局> ...

  2. 显示单位px、dip以及sp的区别

    dip: Device Independent Pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA推荐使用这个,不依赖像素. p ...

  3. python property用法

    参考 http://openhome.cc/Gossip/Python/Property.html http://pyiner.com/2014/03/09/Python-property.html ...

  4. 五、c++实现离散傅里叶变换

    C++离散傅里叶变换 一.序言: 该教程基于之前的图像处理类MYCV,是对其的补充. 二.设计目标 对图像进行简单的离散傅里叶变换,并输出生成的频谱图. 三.需要提前掌握的知识 二维傅里叶变换公式: ...

  5. Java Annotation使用详解

    Java  Annotation是JDK5.0引入的一种注释机制.它与注释有一定区别,可以理解为代码上的特殊标记,通过这些标记我们可以在编译,类加载,运行等程序类的生命周期内被读取.执行相应的处理.通 ...

  6. linux 命令——39 grep (转)

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

  7. Redis基础对象

    Redis 中每个对象都由一个 redisObject 结构表示 typedef struct redisObject { //类型 unsigned type:; //编码 unsigned enc ...

  8. Redis(5.0.0)持久化AOF和 RDB 结合源码分析

    主要是挖个坑.候补(代码还没看完..) https://github.com/antirez/redis/tree/5.0 一.Redis保存持久化文件 二.Redis启动加载持久化文件 src/se ...

  9. PHP数组排序方法总结

    随着PHP的快速发展,用它的人越来越多,在PHP数组学习摘录部分了解到最基本的PHP数组的建立和数组元素的显示.需要深入学习下PHP数组的相关操作.首先接触的就是PHP数组排序.降序的排序问题. so ...

  10. python_40_通过脚本转换参数实现替换

    import sys f=open('yesterday','r',encoding='utf-8') f_new=open('yesterday_update','w',encoding='utf- ...