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 <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;int main(){ #ifdef DoubleQ freopen("in.txt","r",stdin); #endif LL n; while(~scanf("%lld",&n)) { LL c = n / 2; if(n % 2 != 0) { c += 1; printf("-"); } printf("%lld\n" , c); }}