【Educational Codeforces Round 1A】【水题】Tricky Sum 1~n之和减去2的幂
2015-11-19 09:02
597 查看
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 ofn 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.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int n;
int main()
{
scanf("%d",&casenum);
for(casei=1;casei<=casenum;casei++)
{
scanf("%d",&n);
LL sum=(LL)(n+1)*n/2;//全部都算为正
for(int bit=1;bit<=n;bit<<=1)
{
sum-=bit;
sum-=bit;
}
printf("%lld\n",sum);
}
return 0;
}
/*
【题意】
让你求出从1~n的和,但是2^k产生的贡献将是负的。
【类型】
水题
【分析】
先把2^k当做正数处理,求出(1+n)n/2,然后再枚举2^k,筛除即可
【时间复杂度&&优化】
O(log(n))
【数据】
1000000000
*/
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 ofn 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.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int n;
int main()
{
scanf("%d",&casenum);
for(casei=1;casei<=casenum;casei++)
{
scanf("%d",&n);
LL sum=(LL)(n+1)*n/2;//全部都算为正
for(int bit=1;bit<=n;bit<<=1)
{
sum-=bit;
sum-=bit;
}
printf("%lld\n",sum);
}
return 0;
}
/*
【题意】
让你求出从1~n的和,但是2^k产生的贡献将是负的。
【类型】
水题
【分析】
先把2^k当做正数处理,求出(1+n)n/2,然后再枚举2^k,筛除即可
【时间复杂度&&优化】
O(log(n))
【数据】
1000000000
*/
相关文章推荐
- HDU 5240 Exam (好水的题)
- 北大—1006——Biorhythms
- 时间计算(heaven.pas/cpp)
- 工作依赖(job.cpp/pas)
- 题目 英雄 (BFS)
- kmp 学习 hihocoder #1015
- HDU 1096 A+B for Input-Output Practice (VIII)
- P1478
- P1035
- P1008 难度2.7
- 2020 绝对值排序
- 2021 发工资咯
- 2022 海选女主角
- 2024 C语言合法标识符
- 2025 查找最大元素
- 2026 首字母变大写
- 2027 统计元音
- 2028 Lowest Common Multiple Plus
- 2018 母牛的故事
- 2023 求平均成绩