1 /*
2 * PKU2081.cpp
3 *
4 * Created on: 2011-3-19
5 * Author: Administrator
6 */
7
8 #include <stdio.h>
9 //#include <set>
10 #include <ext/hash_set>
11 using namespace std;
12 using namespace __gnu_cxx;
13
14 const int MAXK = 500000 + 1;
15
16 //set<int> s1;
17 hash_set<int> s1;
18 int a[MAXK];
19
20 int main () {
21 a[0] = 0;
22 s1.insert(a[0]);
23
24 for (int i = 1; i <= MAXK; i++) {
25 a[i] = a[i-1] - i;
26 if (a[i] > 0 && s1.find(a[i]) == s1.end())
27 ;
28 else
29 a[i] = a[i-1] + i;
30
31 s1.insert(a[i]);
32 }
33
34 // for (int i = 0; i < 20; i++)
35 // printf("%d: %d\n", i, a[i]);
36 while (true) {
37 int n;
38 scanf("%d", &n);
39 if (n == -1)
40 break;
41
42 printf("%d\n", a[n]);
43 }
44
45 return 0;
46 }
头文件:
#include <ext/hash_set>
#include <ext/hash_map>
命名空间:
using namespace __gnu_cxx;