传送门:

http://codeforces.com/problemset/problem/598/A

A. Tricky Sum
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
Input

Copy
2
4
1000000000
Output

Copy
-4
499999998352516354
Note

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

分析:

题意:

先输入一个T,表示有几组输入,再输入一个n,表示要计算n的和sum。

计算规则为,从1---n这n个数中任意一个数m,如果m这个数是2的次方的话,sum+=(-m),否则sum+=m。

思路:

先求出总的和sum,再用快速幂,求出所有是2的次方的数的和num,最后求差即可sum=sum-2*num。

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL qm(int n,int m)//快速幂
{
LL s=,x=n;
while(m)
{
if(m&)
{
s*=x;
}
x*=x;
m>>=;
}
return s;
}
int main()
{
int t;
cin>>t;
LL n;
LL sum;
while(t--)
{
scanf("%I64d",&n);
sum=n*(n+)/;//等差数列求和公式
for(int i=;qm(,i)<=n;i++)
{
sum-=(*qm(,i));
}
printf("%I64d\n",sum);
}
return ;
}

CodeForces - 598A Tricky Sum (数学,快速幂的运用)的更多相关文章

  1. codeforces 598A Tricky Sum

    题目链接:http://codeforces.com/contest/598/problem/A 题目分类:大数 题意:1到n 如果是2的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ...

  2. codeforces magic five --快速幂模

    题目链接:http://codeforces.com/contest/327/problem/C 首先先算出一个周期里面的值,保存在ans里面,就是平常的快速幂模m做法. 然后要计算一个公式,比如有k ...

  3. 【codeforces 623E】dp+FFT+快速幂

    题目大意:用$[1,2^k-1]$之间的证书构造一个长度为$n$的序列$a_i$,令$b_i=a_1\ or\ a_2\ or\ ...\ or a_i$,问使得b序列严格递增的方案数,答案对$10^ ...

  4. CodeForces - 691E Xor-sequences 【矩阵快速幂】

    题目链接 http://codeforces.com/problemset/problem/691/E 题意 给出一个长度为n的序列,从其中选择k个数 组成长度为k的序列,因为(k 有可能 > ...

  5. Codeforces 954 dijsktra 离散化矩阵快速幂DP 前缀和二分check

    A B C D 给你一个联通图 给定S,T 要求你加一条边使得ST的最短距离不会减少 问你有多少种方法 因为N<=1000 所以N^2枚举边数 迪杰斯特拉两次 求出Sdis 和 Tdis 如果d ...

  6. Codeforces 691E题解 DP+矩阵快速幂

    题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...

  7. HDU 4599 Dice (概率DP+数学+快速幂)

    题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n ...

  8. Codeforces 691E Xor-sequences(矩阵快速幂)

    You are given n integers a1,  a2,  ...,  an. A sequence of integers x1,  x2,  ...,  xk is called a & ...

  9. codeforces 696C C. PLEASE(概率+快速幂)

    题目链接: C. PLEASE time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. SecureCRT 连接 CentOS虚拟机

    SecureCRT 连接 CentOS虚拟机 1.安装SecureCRT SecureCRT是一款支持SSH等协议的终端仿真软件,可以在windows下登录Linux服务器,这样大大方便了开发工作.安 ...

  2. NetCDF 共享软件 中文

    NetCDF 共享软件   转载 在 Models-3 模式中,使用的数据存取接口称为 I/O API,其实就是 NetCDF 文件格式.而由于我们需要了解 Models-3 输出档案的数据情况,因此 ...

  3. rpm重装python和yum

    前些天升级的python, yum就不能用了, 提示 "No module named yum", 然后搜索了一下, 说要重装python和yum, 也没多想, 就按照那些教程去做 ...

  4. 获取memcache中所有数据

    remap_table方法是用的一个框架写的: $gvs = $this->pageObj->get;是获取通过get方式传递过来的数据: $mem = $this->pageObj ...

  5. jqGrid -treeGrid 按需加载

    Load Rows On Demand (AJAX) 参考:http://www.guriddo.net/demo/treegridjs/

  6. JS && || 陷阱 javascript 逻辑与、逻辑或 【转】

    通常来说逻辑运算a&&b和a||b分别是逻辑与运算和逻辑或运算,返回的是一个布尔值,要么为true,要么为false. 比如在PHP里面a&&b返回类型永远是布尔值,非 ...

  7. npm安装gulp-sass失败处理办法

    最近在做一个基于gulp的前端自动化的项目,在github上拉取代码后,按照正常的流程,首先我们要 npm  install ,在安装的过程中,一直提示我gulp-sass 安装失败,我用尽了好多办法 ...

  8. spring boot 2.0.0 + mybatis 报:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

    spring boot 2.0.0 + mybatis 报:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 无法启动 ...

  9. scss-颜色运算符

    scss允许使用颜色分量以及算术运算和任何颜色表达式返回颜色值. 例如scss代码: $color1: #333399; $color2: #CC3399; p{ color: $color1 + $ ...

  10. C++中 结构体和类的异同

    在C++中,结构体是一种特殊形态的类. 结构体和类的唯一区别就是:  结构体和类具有不同的默认访问控制属性. 类中,对于未指定访问控制属性的成员,其访问控制属性为私有类型(private) 结构体中, ...