【CodeForces】598A - Tricky Sum(计数)
2016-07-21 20:13
465 查看
点击打开题目
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
output
Note
The answer for the first sample is explained in the statement.
以前做过类似题,不是很难,注意数据类型。
代码如下:
#include <cstdio>
int main()
{
int u;
__int64 n;
__int64 sum,ans;
__int64 ant; //次数
scanf ("%d",&u);
while (u--)
{
scanf ("%I64d",&n);
ans = (1 + n) * n / 2;
ant = 0;
int t = 1;
while (t <= n)
{
ant++;
t <<= 1;
}
sum = ((__int64)1 << ant) - 1;
ans -= sum;
ans -= sum;
printf ("%I64d\n",ans);
}
return 0;
}
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
2 4 1000000000
output
-4 499999998352516354
Note
The answer for the first sample is explained in the statement.
以前做过类似题,不是很难,注意数据类型。
代码如下:
#include <cstdio>
int main()
{
int u;
__int64 n;
__int64 sum,ans;
__int64 ant; //次数
scanf ("%d",&u);
while (u--)
{
scanf ("%I64d",&n);
ans = (1 + n) * n / 2;
ant = 0;
int t = 1;
while (t <= n)
{
ant++;
t <<= 1;
}
sum = ((__int64)1 << ant) - 1;
ans -= sum;
ans -= sum;
printf ("%I64d\n",ans);
}
return 0;
}
相关文章推荐
- python--模块
- Java 自带的加密类MessageDigest类(加密MD5和SHA)
- cookie,session,localStorage之间的联系与区别
- POJ2728 Desert King 最优比率生成树
- Uncowed Forces
- 使用 Arrays 类操作 Java 中的数组
- Qt之QSizePolicy
- CodeForces615ABulbs(水题)
- 第一章-部署NetScaler VPX10.5服务器
- Patrick and Shopping
- Java 中的Synchronizer
- Codeforces 80B Depression
- 2016夏季练习——二叉树
- QSizePolicy
- Qt之QSizePolicy
- Qt之QSizePolicy
- Leetcode 190. Reverse Bits (Easy) (cpp)
- nagios系列(三)之nagios被动监控模式之添加系统负载load、swap、磁盘iostat及memory内存监控详解
- 2016ACM多校训练第一场_1001_Abandoned Country 并查集+DFS
- nodejs express项目结构分析(windows)