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 ...
随机推荐
- eclipse配置android开发环境并搭建第一个helloWord工程
一.搭建Android在eclipse下环境 一.JDK(不用安装 下载地址: http://www.xp510.com/xiazai/Application/program/23625.ht ...
- js之BOM和DOM
今天我们来学习js中的一些基础的操作. 一.BOM对象 BOM(浏览器对象模型),可以对浏览器窗口进行访问和操作.使用 BOM,开发者可以移动窗口.改变状态栏中的文本以及执行其他与页面内容不直接相 ...
- localstorage本地存储的应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SQL Server date 设置默认值
根据时间做数据统计计算最讨厌开始和结束时间字段是NULL,为了处理NULL要写很多语句. 那么在数据库设计的时候给一个默认值:0001-01-01和9999-12-31,会给开发人员带来很大的便利. ...
- 构建第一个Spring Boot2.0应用之Controller(三)
Controller控制器主要是接收浏览器请求.下面说一说@Controller注解和@RestController的区别: (1)@Controller类中的方法可以直接通过返回String跳转到j ...
- Win10 应用商店管理应用
在企业日常办公中,对 Windows 10 应用商店软件不需要,希望办公系统干净一些.企业运维中,我们可以使用组策略来管理Windows 10 微软Store应用程序.可以根据组织的要求进行配置,多项 ...
- win10搜索不到蓝牙设备
多半是驱动不兼容的问题. 解决方法: 此电脑右键,设备管理器,然后将蓝牙下的驱动,右键.卸载设备. 安装驱动精灵,会自动检测到缺少蓝牙驱动,安装即可.
- this.value = this.placeholder || this.getAttribute('placeholder')
this.value = this.placeholder || this.getAttribute('placeholder') 鉴于不同的浏览器对为止属性的实现方式有所不用,这里同时使用了HTML ...
- IOS类似9.png
图形用户界面中的图形有两种实现方式,一种是用代码画出来,比如Quartz 2D技术,狠一点有OpenGL ES,另一种则是使用图片. 代码画的方式比较耗费程序员脑力,CPU或GPU; 图片则耗费磁盘空 ...
- IOS 一些好的框架和 技术大牛的博客
http://blog.csdn.net/rodulf/article/details/51871093 比较好的框架链接: http://www.jianshu.com/p/9216c561b0f ...