CF#277 (Div. 2) A.(找规律)_html/css_WEB-ITnose
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
推荐阅读
-
Codeforces Round #156 (Div. 2)-A. Greg's Workout_html/css_WEB-ITnose
-
Codeforces Round #107 (Div. 2)-A. Soft Drinking_html/css_WEB-ITnose
-
Codeforces Round #191 (Div. 2)-A. Flipping Game_html/css_WEB-ITnose
-
Codeforces Beta Round #75 (Div. 2)-A. Chips_html/css_WEB-ITnose
-
Codeforces Round #144 (Div. 2)-A. Perfect Permutation_html/css_WEB-ITnose
-
Codeforces Round #135 (Div. 2)-A. k-String_html/css_WEB-ITnose
-
Codeforces Round #256 (Div. 2) A. Rewards_html/css_WEB-ITnose
-
Codeforces Round #175 (Div. 2)-A. Slightly Decreasing Permutations_html/css_WEB-ITnose
-
Codeforces Round #112 (Div. 2)-A. Supercentral Point_html/css_WEB-ITnose
-
Codeforces Round #190 (Div. 2)-A. Ciel and Dancing_html/css_WEB-ITnose