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
  1. 2
  2. 4
  3. 1000000000
Output
  1. -4
  2. 499999998352516354

Note

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

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

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

  1. #include<cstdio>
  2. long long a[40];
  3. long long pow(int n)
  4. {
  5. if(n==0)
  6. return 1;
  7. else
  8. {
  9. long long k1=1;
  10. for(int i=0;i<n;i++)
  11. k1*=2;
  12. return k1;
  13. }
  14.  
  15. }
  16. int main()
  17. {
  18. int t;
  19. scanf("%d",&t);
  20. for(int i=0;i<33;i++)
  21. a[i]=pow(i);
  22. while(t--)
  23. {
  24. long long n;
  25. scanf("%lld",&n);
  26. long long sum;
  27. sum=(1+n)*n/2;
  28. int i;
  29.  
  30. for( i=0;i<33;i++)
  31. if(n<=a[i])
  32. break;
  33. if(n==a[i])
  34. i=i+1;
  35. long long sum1=0;
  36.  
  37. for(int j=0;j<i;j++)
  38. sum1+=a[j];
  39. sum=sum-sum1*2;
  40. printf("%lld\n",sum);
  41.  
  42. }
  43. }

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. 解决gradle下载慢的问题

    解决方法要做两部 一 打开用户主目录 linux平台/home/用户名/.gradle windows平台c:\Users\用户名\.gradle macos平台/Users/用户名/.gradle ...

  2. Centos 6.5 修改默认分辨率

    需要两步: 第一步: 编辑/etc/grub.conf文件,删除“nomodeset” 单词 ,翻到该页最后一行,就可以看到该词: 第二步: 删除文件/etc/X11/xorg.conf , Inte ...

  3. java下的串口通信-RXTX

    关于java实现的串口通信,使用的是开源项目RXTX,之前sun公司也有JCL项目,不过已经很久没有更新了,RXTX项目地址:点击打开,但是两个项目的API用法是一样的,只是导入的包不一样而已.简单的 ...

  4. jQuery-显示与隐藏不用判断的快捷方法

    功能:显示被隐藏的元素,隐藏已显示的元素. 常规方法:(需要先判断元素状态) $("button").click(function(){ if ($(".content& ...

  5. PPPOE+FREERADIUS+MYSQL+LINUX

    环境: OS:Linux Centos 6.9 x86_x64 PPPOE : ppp-2.4.7.tar.gz   rp-pppoe-3.12.tar.gz FreeRadius : V3.0.X ...

  6. sublime相关小技巧

    1.快速建立一个新文件:Ctrl+n 2.修改多个相同符号:Ctrl+D 3.建立语言后缀的文件保存,例如我想创建PHP的语言脚本,先按Ctrl+Shift+p,打开Command Palette,输 ...

  7. IDA逆向:数组的逆向

    阅读<IDA Pro权威指南>第八章,整理的一些笔记,作为逆向的基础,可能有很多认识不足. //全局分配数组 *************************************** ...

  8. 新建framework的bundle资源 图片资源被编译成了ttf后缀 解決

    设置combine_hidpi_images为no

  9. java poi读取excel公式,返回计算值(转)

    http://blog.csdn.net/CYZERO/article/details/6573015 经测试,确实可以 1 package hrds.zpf.poi;  2  3  import o ...

  10. 19课 Vue第二节

    事件修饰符 stop 禁止冒泡once 单次事件prevent 阻止默认事件native 原生事件(组件)keycode|name 筛选按键   组合键 : @keydown.ctrl.enter s ...