Tricky Sum

Tricky SumCrawling in process...
Crawling failed
Time Limit:1000MS    
Memory Limit:262144KB    
64bit IO Format:
%I64d & %I64u

Description

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.

Sample Input

Input
2
4
1000000000
Output
-4
499999998352516354

Sample Output

Hint

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

1--n全部加起来,如果是2的次幂  数字就为负的,求最后的和,先利用等差序列公式(n+1)*n/2,然后扣除2的次幂

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
__int64 n;
cin>>n;
__int64 ans=(n+1)*n/2-2;
for(int i=1;pow(2,i)<=n;i++)
ans-=pow(2,i+1);
cout<<ans<<endl;
}
return 0;
}

Codeforces--598A--Tricky Sum(数学)的更多相关文章

  1. CodeForces - 598A Tricky Sum (数学,快速幂的运用)

    传送门: http://codeforces.com/problemset/problem/598/A A. Tricky Sum time limit per test 1 second memor ...

  2. codeforces 598A Tricky Sum

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

  3. Educational Codeforces Round 1 A. Tricky Sum 暴力

    A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...

  4. Codeforces 396B On Sum of Fractions 数论

    题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...

  5. codeforces 963A Alternating Sum

    codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...

  6. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  7. woj1010 alternate sum 数学 woj1011 Finding Teamates 数学

    title: woj1010 alternate sum 数学 date: 2020-03-10 categories: acm tags: [acm,woj,数学] 一道数学题.简单. 题意 给一个 ...

  8. Codeforces 577B Modulo Sum:数学 结论【选数之和为m的倍数】

    题目链接:http://codeforces.com/problemset/problem/448/C 题意: 给你n个数字,给定m. 问你是否能从中选出若干个数字,使得这些数字之和为m的倍数. 题解 ...

  9. Codeforces D. The Sum of the k-th Powers(拉格朗日插值)

    题目描述: The Sum of the k-th Powers time limit per test 2 seconds memory limit per test 256 megabytes i ...

  10. CodeForces 534C Polycarpus' Dice (数学)

    题意:第一行给两个数,n 和 A,n 表示有n 个骰子,A表示 n 个骰子掷出的数的和.第二行给出n个数,表示第n个骰子所能掷出的最大的数,这些骰子都有问题, 可能或多或少的掷不出几个数,输出n个骰子 ...

随机推荐

  1. 图解TCP/IP笔记(2)——数据链路

    [转载请注明]https://www.cnblogs.com/igoslly/p/9396066.html ——终端节点之间的包传递 MAC寻址(物理寻址).介质共享.非公有网络.分组交换.环路检测. ...

  2. 关于类似vue-cli 脚手架

    #!/usr/bin/env node const download = require('download-git-repo') const program = require('commander ...

  3. html5——应用缓存

    基本概念 1.HTML5中我们可以轻松的构建一个离线(无网络状态)应用,只需要创建一个cache manifest文件 2.可配置需要缓存的资源,网络无连接应用仍可用,本地读取缓存资源,提升访问速度, ...

  4. SQL基本操作——ALTER

    ALTER TABLE 语句用于在已有的表中添加.修改或删除列. Persons 表: ID LastName FirstName Address City 1 Adams John Oxford S ...

  5. 【译】x86程序员手册14-5.1段转换

    5.1 Segment Translation 段转换 Figure 5-2 shows in more detail how the processor converts a logical add ...

  6. Python语言之模块

    模块基本上就是一个包含了所有你定义的函数和变量的文件.它用处在于使你你能在别的程序中重用它提供的功能和服务. 1.模块的使用方法 模块的文件名必须以.py作为扩展名. 当我们需要使用某个模块时,我们需 ...

  7. 惊了!!! 小白零基础学java (月薪过万是你的梦想嘛) 手把手教学 就怕你不动手【二十五】第二章【初识MySQL】

    初识MySQL1. 了解主流的数据库和数据库分类1.1 数据库概念数据库:按照数据结构来组织.存储和管理数据的一种建立在计算机存储设备上的仓库. 数据库的优势: 1. 可以持久化存储大量的数据.方便我 ...

  8. (1)搜索广告CTR预估

    https://www.cnblogs.com/futurehau/p/6181008.html 一.广告投放系统 广告系统包含多个子系统.除了上图所示的广告投放系统外,还包含商业系统(广告库的获得) ...

  9. 【剑指Offer】66、机器人的运动范围

      题目描述:   地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时 ...

  10. C语言中的DEBUG

    #cat aa.c #include <stdio.h> #include <stdarg.h> #include <stdlib.h> #include < ...