Codeforces 598A Tricky Sum 【计数】
2015-11-15 18:27
369 查看
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.
Sample test(s)
input
output
Note
The answer for the first sample is explained in the statement.
题意:给定一个数n,把1-n里面2的幂次数变为负数。求n个数之和。
思路:等比数列求出2的幂次数之和temp,用1-n总和 - 2*temp就可以了。
AC代码:
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.
Sample test(s)
input
2 4 1000000000
output
-4 499999998352516354
Note
The answer for the first sample is explained in the statement.
题意:给定一个数n,把1-n里面2的幂次数变为负数。求n个数之和。
思路:等比数列求出2的幂次数之和temp,用1-n总和 - 2*temp就可以了。
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <vector> #define INF 0x3f3f3f3f #define eps 1e-4 #define MAXN (100+10) #define MAXM (1000000) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 100000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 using namespace std; int main() { int t; Ri(t); W(t) { LL n; Rl(n); LL ans = n * (n + 1) / 2; LL cnt = 0; while(n) { cnt++; n >>= 1; } LL temp = (1LL<<cnt)-1; ans = ans - temp - temp; Pl(ans); } return 0; }
相关文章推荐
- kotterKnife+dagger2+otto+okhttp+picasso+retrofit+rxjava示例项目,使用kotlin编写。
- UINavigationBar自定义返回按钮的设置
- UINavigationBar的使用入门学习
- java WebService简单使用案例
- POJ1426 想象不到的广搜...
- 简约至上 交互设计四策略 读书心得
- java字符串中多个空格替换成一个空格
- IOS键盘隐藏方法
- linux驱动模型开发——linux platform总线机制讲解与实例开发
- (转) iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
- (转)iOS学习之UINavigationController详解与使用(三)ToolBar
- android studio导出lib和使用第三方lib
- 动手动脑,课后作业
- 信息安全系统设计基础第十周学习
- 信息安全系统设计基础第十周学习总结
- 信息安全系统第十周学习总结 20135218 姬梦馨
- SVN版本更新后,upData工程之后,Xcode 工程文件打不开解决办法
- Android网络通信中数据传输的XML格式使用
- (转)IOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
- hdu 3376 Matrix Again