Codeforces--598A--Tricky Sum(数学)
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
2
4
1000000000
-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(数学)的更多相关文章
- 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的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ...
- 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 396B On Sum of Fractions 数论
题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...
- codeforces 963A Alternating Sum
codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- woj1010 alternate sum 数学 woj1011 Finding Teamates 数学
title: woj1010 alternate sum 数学 date: 2020-03-10 categories: acm tags: [acm,woj,数学] 一道数学题.简单. 题意 给一个 ...
- Codeforces 577B Modulo Sum:数学 结论【选数之和为m的倍数】
题目链接:http://codeforces.com/problemset/problem/448/C 题意: 给你n个数字,给定m. 问你是否能从中选出若干个数字,使得这些数字之和为m的倍数. 题解 ...
- 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 ...
- CodeForces 534C Polycarpus' Dice (数学)
题意:第一行给两个数,n 和 A,n 表示有n 个骰子,A表示 n 个骰子掷出的数的和.第二行给出n个数,表示第n个骰子所能掷出的最大的数,这些骰子都有问题, 可能或多或少的掷不出几个数,输出n个骰子 ...
随机推荐
- jQuery——属相操作
属性获取:attr(属性名), 属性设置:attr(属性名,具体值) 移除属性:removeAttr(属性名) 特殊情况:prop(属性名).prop(属性名,具体值):表单中状态属性checked. ...
- java攻城狮之路--复习JDBC(PrepareStatement)
PreparedStatement: 1.可以通过调用 Connection 对象的 preparedStatement() 方法获取 PreparedStatement 对象 2.PreparedS ...
- 删除ListView item数据 页面不刷新
最近碰到一个匪夷所思的事情.就是我删除listView中一条item数据 网络请求成功了 但是页面不成功,一番折腾 ,找到问题 ,原来我给item 添加了北京点击事假,又给listView 被禁设置 ...
- dotnetnuke 头像调用 头像缩放
public static string GetProfileImage(int userId, int width, int height) { ...
- Java 基础入门随笔(1) JavaSE版——java语言三种技术架构
1.java语言的三种技术架构: J2SE(java 2 Platform Standard Edition):标准版,是为开发普通桌面和商务应用程序提供的解决方案.该技术体系是其他两者的基础,可以完 ...
- 15、Scala隐式转换和隐式参数
1.隐式转换 2.使用隐式转换加强现有类型 3.隐式转换函数的作用域与导入 4.隐式转换发生时机 5.隐式参数 1.隐式转换 要实现隐式转换,只要程序可见的范围内定义隐式转换函数即可.Scala会自动 ...
- 2015.12.25-2016.01.01 大论文迭代B
大论文B轮迭代,稍重前端 12.25 周五,完善摘要 12.26 周六,完善第一章 12.27 周天,完善第二章 12.28 周一,完善第三章 12.29 周二,完善第四章 12.30 周三,完善第五 ...
- 【sqli-labs】 less49 GET -Error based -String -Blind -Order By Clause(GET型基于盲注的字符型Order By从句注入)
都是order by的注入,作者连图片都懒得改了... 注意和整型的区别,前引号用提交的引号闭合,后引号用#注释 http://192.168.136.128/sqli-labs-master/Les ...
- C# 计算百分比
//计算比率 decimal A =(decimal) 200.20; decimal B = (decimal)1000.20; decimal t = decimal.Parse((A/B).To ...
- 小程序 之picker-view省市县
代码地址:https://github.com/yangsphp/area-picker