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 ...
随机推荐
- Java学习笔记--继承和多态(上)
1.通过继承来开发超类(superclass) 2.使用super 关键词唤起超类的构造方法 3.在超类中覆盖方法 4.区分override和overload 5.在Object类中探索toStrin ...
- Git常用操作命令收集
Git常用操作命令收集 1.进入本地仓库访问位置之后执行命令 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远 ...
- JS实现2048
2048这个游戏是通过对二维数组的操作来实现的,其算法核心如下: (以一行左移为例) c从0开始,遍历当前行中的元素,到<CN-1(CN是一个常量,表示的是游戏格子的列数)结束,每次+1 找到当 ...
- 构建第一个Spring Boot2.0应用之application.properties和application.yml(八)
本节学习在项目中配置文件配置的方式,一种是通过applicaiton.properties,一种是通过application.yml方式. 一.环境: IDE:IntelliJ IDEA 2017.1 ...
- Git 团队常用命令操作指南
命令如下: git clone -b <branch name> [remote repository address] 主要就是在clone的时候,后面添加branch的信息. 报错: ...
- zabbix-2.2.2(Ubuntu 14.04 LTS/OpenLogic 7.2)
平台: arm 类型: ARM 模板 软件包: apache-2.4.7 mariadb-5.5.50 mysql-5.5.52-0ubuntu0.14.04.1 php-5.4.16 php-5.5 ...
- Team Foundation 版本控制
与 Visual Studio 的一流集成. 使用富文件和文件夹差异工具突出显示代码更改. 借助强大的可视化跨分支跟踪代码更改. 集成的代码评审工具有助于在签入代码之前获得反馈. 使用托管版本或本地版 ...
- vue的MVVM模式和生命周期总结(一)
一.MVVM模式 MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel ...
- html body上有一条空白!!!
html body 上莫名其妙的就出现了一条空白,怎么搞都搞不定,弄了一下午...... 解决了!!! 格式问题/
- IOS 绘制条纹背景
@interface NJViewController () @property (weak, nonatomic) IBOutlet UITextView *contentView; - (IBAc ...