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);}
}
转载于:https://www.cnblogs.com/-xiangyang/p/9220273.html
Tricky Sum相关推荐
- CodeForces 598A Tricky Sum
水题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #inc ...
- Educational Codeforces Round 1
被C坑的不行不行的...其他题目都还可以. A - Tricky Sum 求1,2,3,...,n的加和,其中2^x(x>=0)为负. 因为2^x的个数很少,所以以每个2^x为分界点进行判断. ...
- Maximum XOR Sum 系列问题
给定 $n$ 个两两不同的正整数 $a_1, a_2, \dots, a_n$,$a_i < 2^k$ . Problem 1(经典问题) 求 $a_i \xor a_j$ 的最大值,$ 1\l ...
- Two Sum (I ~ IV)
Two Sum I 题目:Given an array of integers, return indices of the two numbers such that they add up to ...
- Prefix Sum 总结
注意:prefixsum一般都是用PrefixSum[n+1]的数组,prefix[0] = 0; 那么 因为i, j 是A里面的坐标,而prefixsum比A多了1:所以原来的sum[j] - su ...
- go build 编译报错 missing go.sum entry for module providing package
go build 编译报错 missing go.sum entry for module providing package 解决方法 // 移除未使用的依赖 go mod tidy 再次编译,就可 ...
- pytorch之expand,gather,squeeze,sum,contiguous,softmax,max,argmax
目录 gather squeeze expand sum contiguous softmax max argmax gather torch.gather(input,dim,index,out=N ...
- C语言计算e1 1/1! 1/2!,c语言程序填空 下面程序是计算sum=1+(1+1/2)+(1+1/2+1/3)+…(1+1/2...
匿名用户 1级 2016-06-25 回答 <C语言>综合练习题一注意:以下"□"为空格,为回车一.单选题1.阅读以下程序,当输入数据的形式为:25,13,10,正确的 ...
- PyTorch 笔记(07)— Tensor 的归并运算(torch.mean、sum、median、mode、norm、dist、std、var、cumsum、cumprod)
1. Tensor 归并运算函数 此类操作会使输出形状小于输入形状,并可以沿着某一维度进行指定操作,如加法, 既可以计算整个 tensor 的和,也可以计算 tensor 每一行或者 每一列的和, 常 ...
最新文章
- MyEclipse内安装与使用SVN
- keras.utils.to_categorical用法
- Tungsten Fabric SDN — 与 OpenStack 的集成架构
- 用js写一个模板引擎
- Oracle中用户解锁
- inxtrackup mysql_雪糕 的动态 - SegmentFault 思否
- centos 7 构造iptables开放80port
- Hadoop之Hadoop数据压缩
- Redis实战(八):面试常问:击穿,穿透,雪崩,分布式锁,API(jedis,luttce,springboot:low/high level)
- linux批量杀进程_Linux下批量杀JAVA进程
- path hdu6705
- 几种距离公式的总结思考
- CSDN 积分的作用
- Internet Download Manager v6.41 Build 2
- Typora 官网下载
- 高通 lcd dtsi
- 高德地图API 前端调用 搜索定位
- Aseprite学习/技巧
- icpc西部区域赛_江西软件大学夺得第八届高校联赛西部大区总冠军
- VBA(14)排序Sort