A. Calculating Function
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output题目链接: http://codeforces.com/contest/486/problem/A
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
-3
Note
f(4)?=??-?1?+?2?-?3?+?4?=?2
f(5)?=??-?1?+?2?-?3?+?4?-?5?=??-?3
解题思路:
给你n,求f(n) 。这是一道规律题,首先确定不能暴力求解,因为n实在太大,暴力必超时。同时也开不了那么大的数组来暴力。
提笔简单算了几个,发现f(1) = -1 , f(2) = 1, f(3) = -2, f(4) = 2, f(5) = -3, f(6) = 3··········这样规律就看出来了,两个数一组,n/2如果为偶数那么符号为正,奇数符号为负。并且如果n/2为奇数的话,我们向上取整,即为n / 2 + 1
最后,中间值什么的都开成long long。
完整代码:
#include#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include
#include