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.
InputThe single line contains the positive integer n (1?≤?n?≤?1015).
OutputPrint f(n) in a single line.
Sample test(s) input4output
2input
5output
-3Note
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