Codeforces Round #277 (Div. 2) A. Calculating Function 水题
A. Calculating Function
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/486/problem/A
Description
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.
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 Input
4
Sample Output
2
HINT
题意
f(n) = - 1 + 2 - 3 + .. + ( - 1)^n
给你n,然后让你求f(n)
题解:
分奇偶啦,奇数就是(n-1)/2-n,偶数就是n/2
代码
#include<stdio.h> #include<iostream> using namespace std; int main() { long long n;scanf("%lld",&n); if(n%2==1) printf("%lld\n",(n-1LL)/2LL - n); else printf("%lld\n",n/2LL); }