【codeforces div2 A】Calculating Function(思维)
2016-09-29 14:19
399 查看
A. Calculating Function
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
For a positive integer n let's define a function f:
f(n) = - 1 + 2 - 3 + .. + ( - 1)nn
Your task is to calculate f(n) for a given integer n.
Input
The single line contains the positive integer n (1 ≤ n ≤ 1015).
Output
Print f(n) in a single line.
Sample test(s)
input
output
input
output
Note
f(4) = - 1 + 2 - 3 + 4 = 2
f(5) = - 1 + 2 - 3 + 4 - 5 = - 3
思维题,发现中间过程的规律,本题的是每两项加一。而我开始竟然想暴力循环,显然超时了;而后又想用等差数列公式,结果是过掉了部分测试数据;我怎么这么菜。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
int main() {
__int64 n;
while(scanf("%I64d",&n)!=EOF) {
__int64 sum=0;
if(n%2==0){
sum=n/2;
}
else{
sum=(n-1)/2-n;
}
printf("%I64d\n",sum);
}
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
For a positive integer n let's define a function f:
f(n) = - 1 + 2 - 3 + .. + ( - 1)nn
Your task is to calculate f(n) for a given integer n.
Input
The single line contains the positive integer n (1 ≤ n ≤ 1015).
Output
Print f(n) in a single line.
Sample test(s)
input
4
output
2
input
5
output
-3
Note
f(4) = - 1 + 2 - 3 + 4 = 2
f(5) = - 1 + 2 - 3 + 4 - 5 = - 3
思维题,发现中间过程的规律,本题的是每两项加一。而我开始竟然想暴力循环,显然超时了;而后又想用等差数列公式,结果是过掉了部分测试数据;我怎么这么菜。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
int main() {
__int64 n;
while(scanf("%I64d",&n)!=EOF) {
__int64 sum=0;
if(n%2==0){
sum=n/2;
}
else{
sum=(n-1)/2-n;
}
printf("%I64d\n",sum);
}
return 0;
}
相关文章推荐
- Codeforces 428(div 2)B. Game of the Rows(思维题)
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery && Codeforces 360 A 详解(思维+维护理论值)
- Codeforces Round #378 (Div. 2) && codeforces 733D(思维枚举)
- codeforces 355(Div 2)C. Vanya and Label(思维,预处理)
- Codeforces 456 div2 A B ***D(思维题) ***E(二分/DFS/数论)
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree - Codeforces(思维)
- 【Codeforces Round #369 (Div. 2)】Codeforces 711C Coloring Trees
- Codeforces 782C Andryusha and Colored Balloons【思维+Dfs暴力染色】
- Codeforces 598B Queries on a String 【思维】
- CodeForces 3C---Tic-tac-toe--思维题
- Codeforces 594A - Warrior and Archer (思维)
- Codeforces 606 C Sorting Railway Cars【思维】
- Codeforces 344C Rational Resistance【思维+贪心】
- Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque(思维 贪心)
- Codeforces Round #418 (Div. 2) C 预处理/思维题
- codeforces 428(div2) A-D
- Codeforces 301A Yaroslav and Sequence 【思维】
- CodeForces 496 D. Tennis Game【思维】
- Codeforces 876E National Property【思维建图+2-sat+输出可行解】好题~
- 第三十三次codeforces竞技结束 #441 Div 2