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. 海盗(Haidao)网店系统最新官方版

    产品介绍:经过开发团队半个月的努力,Haidao v1.1 beta版终于和大家见面了,在这个版本中我们重点系统目录结构和整体框架进行了改进,同时新增加了多个功能模块. 四大功能模块意在打造最灵活的电 ...

  2. Java 基础入门随笔(2) JavaSE版——关键字、进制转换、类型转换

    1.Java语言-关键字 关键字:被java语言赋予了特殊含义的词,特点是所有的字母都为小写. java涉及到的关键字整理: 用于定义数据类型的关键字 class interface byte sho ...

  3. Linux监控实时log

    https://jingyan.baidu.com/article/93f9803f5545a3e0e46f5596.html

  4. Linux Shell 小知识

    ${} ——变量替换 通常 $var 与 ${var} 没有区别,但是用 ${} 会比较精确的界定变量名称的范围. name='Ace' echo "result1: my name is ...

  5. HDU_3549_网络流(最大流)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  6. C# 时间对比

    public bool IfTime(string StartTime, string EndTime) { DateTime dt1 = Convert.ToDateTime(StartTime); ...

  7. querySelector

     this.el.nativeElement.querySelector import {Component, ElementRef, OnInit} from '@angular/core'; 

  8. CSS超链接常见四种状态在控制

    一.首选设置一下以下4种状态常: 1.a:active是超级链接的初始状态 2.a:hover是把鼠标放上去时的状况 3.a:link 是鼠标点击时 4.a:visited是访问过后的情况.

  9. B.2 列表

    从很多方面来说,列表是最简单也最自然的集合类型.框架中包含很多实现,具有各种功能 和性能特征.一些常用的实现在哪里都可以使用,而一些较有难度的实现则有其专门的使用场景. B.2.1  List< ...

  10. Oracle存储过程及函数的练习题

    --存储过程.函数练习题 --(1)创建一个存储过程,以员工号为参数,输出该员工的工资create or replace procedure p_sxt1(v_empno in emp.empno%t ...