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导航插件One-Page-Nav演示-显示命名锚记
jQuery导航插件One-Page-Nav演示-显示命名锚记 简介 使用 选项 示例 推荐 简介 电商网站的分类比较明确,比如1楼是手机通讯产品,2楼是家用电器,3楼是服装鞋包等等,旁边还会有一个固 ...
- jQuery实现页面锚点滚动效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [系统]no such partition
电脑系统是win8.1+ubuntu14.2,为了卸载ubuntu,安装CentOS,于是在win8.1下把ubuntu的分区给删除了,重启,出现no such partition grub resc ...
- RHEL7配置中文输入法-智能拼音
RHEL7配置中文输入法-智能拼音 RHEL7.x(CentOS7.x)系统相对之前的6.x系统变化较大,虽然安装时选择了中文环境,但是进入系统后,在控制台及编辑器中仍无法切换输入法进行中文输入. 原 ...
- uvalive 3231
3231 - Fair ShareAsia - Seoul - 2004/2005You are given N processors and M jobs to be processed. Two ...
- Luogu P2866 [USACO06NOV]糟糕的一天Bad Hair Day
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a ...
- JAVA学习总结-常用数据结构
java中集合框架其实就是数据结构的实现的封装; 参考资料:任小龙教学视频 1,什么是数据结构? 数据结构是计算机存储,组织数据的方式; 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合; ...
- BZOJ 1051 HAOI 2006 受欢迎的牛
[题解] 先用tarjan缩点,然后如果某个强联通分量的出度为0,则该强联通分量内的点数为答案,否则无解.因为若其他所有的强联通分量都有边连向Ai,则Ai必定没有出边,否则Ai连向的点所属的强联通分量 ...
- App后台开发运维和架构实践学习总结(2)——RESTful API设计技巧
前言 移动互联网时代,RESTful API成为越来越重要的移动端和服务器端交互的形式.尤其是在很多互联网公司或者传统行业拥抱移动互联网的时候,一套设计良好的Restful API能够帮助互联网产品支 ...
- Mysql中文乱码以及导出为sql语句和Excel问题解决
Mysql中文乱码以及导出为sql语句和Excel问题解决 这几天基于Heritrix写了一个爬虫,用到mysql,在导入导出数据时,遇到一些乱码问题,好不容易解决了,记录一下,以备查看.一.导出数据 ...