POJ-2081-Recaman's Sequence-Hash思想解题

#include <iostream> #include <string> #include <vector> using namespace std; const int MAX_K = 500002; const int MOD_VAL = 499991; int a[MAX_K]; vector<int> hash[MOD_VAL]; //hash表项中存储的是一个实际的数 bool isIn(int n) { int key = n % MOD_VAL; for(vector<int>::size_type i = 0; i != hash[key].size(); i++) { if(n == hash[key][i]) return true; } return false; } void pushInHash(int n) { int key = n % MOD_VAL; hash[key].push_back(n); } void computeRec() { a[0] = 0; for(int i = 1; i <= MAX_K; i++) { int t1 = a[i - 1] - i; int t2 = a[i - 1] + i; if(t1 > 0 && !isIn(t1)) a[i] = t1; else a[i] = t2; pushInHash(a[i]); } } int main() { /*先直接求出前500000个ak*/ computeRec(); int k; while(cin >> k, k != -1) { cout << a[k] << endl; } return 0; }

你可能感兴趣的:(存储,include)