Tricky Sum
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
- 2
- 4
- 1000000000
- -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的更多相关文章
- Educational Codeforces Round 1 A. Tricky Sum 暴力
A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...
- CodeForces - 598A Tricky Sum (数学,快速幂的运用)
传送门: http://codeforces.com/problemset/problem/598/A A. Tricky Sum time limit per test 1 second memor ...
- codeforces 598A Tricky Sum
题目链接:http://codeforces.com/contest/598/problem/A 题目分类:大数 题意:1到n 如果是2的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ...
- Codeforces--598A--Tricky Sum(数学)
Tricky Sum Tricky SumCrawling in process... Crawling failed Time Limit:1000MS Memory Limit:26 ...
- HPU周赛题目解析
A - Wilbur and Swimming Pool Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- 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/25 CSU-ACM2018暑假集训比赛1
题目链接 [A - Tricky Sum ] In this problem you are to calculate the sum of all integers from 1 to n, but ...
- Codefoces 429 D. Tricky Function
裸的近期点对.... D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对
D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...
随机推荐
- 解决gradle下载慢的问题
解决方法要做两部 一 打开用户主目录 linux平台/home/用户名/.gradle windows平台c:\Users\用户名\.gradle macos平台/Users/用户名/.gradle ...
- Centos 6.5 修改默认分辨率
需要两步: 第一步: 编辑/etc/grub.conf文件,删除“nomodeset” 单词 ,翻到该页最后一行,就可以看到该词: 第二步: 删除文件/etc/X11/xorg.conf , Inte ...
- java下的串口通信-RXTX
关于java实现的串口通信,使用的是开源项目RXTX,之前sun公司也有JCL项目,不过已经很久没有更新了,RXTX项目地址:点击打开,但是两个项目的API用法是一样的,只是导入的包不一样而已.简单的 ...
- jQuery-显示与隐藏不用判断的快捷方法
功能:显示被隐藏的元素,隐藏已显示的元素. 常规方法:(需要先判断元素状态) $("button").click(function(){ if ($(".content& ...
- 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 ...
- sublime相关小技巧
1.快速建立一个新文件:Ctrl+n 2.修改多个相同符号:Ctrl+D 3.建立语言后缀的文件保存,例如我想创建PHP的语言脚本,先按Ctrl+Shift+p,打开Command Palette,输 ...
- IDA逆向:数组的逆向
阅读<IDA Pro权威指南>第八章,整理的一些笔记,作为逆向的基础,可能有很多认识不足. //全局分配数组 *************************************** ...
- 新建framework的bundle资源 图片资源被编译成了ttf后缀 解決
设置combine_hidpi_images为no
- java poi读取excel公式,返回计算值(转)
http://blog.csdn.net/CYZERO/article/details/6573015 经测试,确实可以 1 package hrds.zpf.poi; 2 3 import o ...
- 19课 Vue第二节
事件修饰符 stop 禁止冒泡once 单次事件prevent 阻止默认事件native 原生事件(组件)keycode|name 筛选按键 组合键 : @keydown.ctrl.enter s ...