CF#277 (Div. 2) A.(找规律)
来源:互联网 发布:2017淘宝黑搜索技术 编辑:程序博客网 时间:2024/06/06 03:43
A. Calculating Function
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
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
4
output
2
input
5
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); }}
0 0
- CF#277 (Div. 2) A.(找规律)
- CF#280 (Div. 2) A.(找规律)
- CF #273 (Div. 2) A.(简单规律)
- CF - 201A 找规律
- cf(找规律,模拟)
- Codeforces Round #431 (Div. 2) A. Odds and Ends(找规律)
- CF 615 E Hexagons(找规律)
- CF找规律
- CF#284 (Div. 2) C.(几何规律)
- Codeforces Round #327 (Div. 2) (591A,591B,590A(找规律),590B(二分))
- CF 286A(Lucky Permutation-数列找规律)
- Codeforces Round #277 (Div. 2)---A. Calculating Function (规律)
- Codeforces Round #264 (Div. 2)C(找规律)
- Codeforces #275 (Div. 2)(数学:找规律)
- cf 190div 2 A
- CF #230 (Div. 2) A
- CF#257 (Div. 2) A.
- CF#256 (Div. 2) A.
- Reduce任务的Map输出复制器—ReduceCopier
- C++中的namespace
- django中模板models设置时间
- 树形DP(01组合背包The Ghost Blows Light HDU4276)
- JQuery上传插件Uploadify API详解
- CF#277 (Div. 2) A.(找规律)
- java.lang.RuntimeException: Error receiving broadcast Intent { act=PhotoUploadSuccess_Receiver flg=0
- C语言考试总结
- 程序猿之---C语言细节22(函数返回指针注意事项<悬空指针>、查看进程可以分配的内存大小)
- 程序代码阅读技巧
- SQL中内连接和外连接区别
- 【设计模式】——工厂家族
- poj1423解题报告
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState