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. Mybatis与Ibatis的区别

    Mybatis与Ibatis的区别: 1.Mybatis实现了接口绑定,使用更加方便 在ibatis2.x中我们需要在DAO的实现类中指定具体对应哪个xml映射文件, 而Mybatis实现了DAO接口 ...

  2. 无需控件直接导出xls(csv)

    /// <summary> /// 执行导出 /// </summary> /// <param name="ds">要导出的DataSet&l ...

  3. subline 安装 package control 连接服务器失败,解决办法

    解决办法: 打开C:\Windows\system32\drivers\etc\hosts文件 增加 50.116.34.243 sublime.wbond.net50.116.34.243 pack ...

  4. eclipse调试(转)

    step into : 单步执行,遇到子函数就进入并且继续单步执行(F5) step over: 在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行,而是将子函数整个执行完在停止,也就是把子函 ...

  5. 有关 C# 命名参数和可选参数

    有关 C# 命名参数和可选参数 #1.命名参数: 所谓“命名参数 ( Named Arguments )”,是指方法中定义了一些“有名字”的参数. 给方法参数命名之后,在调用方法时就可以直接根据参数名 ...

  6. Visual Studio 2010 vs2010 英文版 使用 已有的中文版 MSDN 帮助文档

    第一步 设置Help Library Manager区域语言 打开Microsoft Visual Studio 2010开始菜单里Visual Studio Tools里的Manage Help S ...

  7. 软件测试Lab 1 Junit and Eclemma

    首先安装eclipse 然后下载hamcrest-core-1.3.jar,下载地址:http://mvnrepository.com/artifact/org.hamcrest/hamcrest-c ...

  8. [Unity3D] 如何识别屏幕边缘

    出现的问题 Unity3D中长度单位是米 使用Screen.resolutions获取的屏幕信息单位是像素 也就是说,即使获取了屏幕相关信息及参数,也无法把信息转换成可在editor中使用的信息.当时 ...

  9. PHP函数:method_exists和function_exists

    method_exists 检查类的方法是否存在 bool method_exists ( mixed $object , string $method_name ) 检查类的方法是否存在于指定的ob ...

  10. 爬虫系统-日志、初始化url

    1.日志log4j 1.1.DEBUG:debug级别 1.2.stdout:输出到控制台 1.3.D:输出到文件 log4j.rootLogger=DEBUG, stdout,D #Console ...