void DLL::push_after(int val,int after){ LI::iterator it=find(after); if(it!=dt.end()){ ++it; dt.insert(it,val); } }
void LI::divide(StackLI & st, int s, int t) // розділяе число на т+1 частину і записує у "правильному" напрямку { LI timeli; int r; int act = active_n; active_n = s * (t + 1); for (int j = 0; j < t + 1; ++j) { timeli.Nul(); for (int i = 0; i < s; ++i) { r = digits[active_n - 1 - i - 3 * j]; timeli.digits[i] = r; ++timeli.active_n; } int k = 0; int act_time = timeli.active_n; for (; act_time > 1 && timeli.digits[k] == 0; ++k, --act_time){} timeli.Shift_left(k); st.push(timeli); } active_n = act; //delete []timeli.digits; //timeli.digits = NULL; }
const LI LI :: operator / (const int & second) { LI res; LI zero; zero.From_Int(0); if (second == 0) { cout << "Div by 0\n"; exit(1); } if ((*this).active_n == 1) { int r = (*this).digits[0] / second; res.From_Int(r); return res; } res.active_n = (*this).active_n; int ost = 0, dig; for (int i = 0; i < (*this).active_n; ++i) { dig = ost*p + (*this).digits[(*this).active_n - 1 - i]; res.digits[(*this).active_n - 1 - i] = dig / second; ost = dig - res.digits[(*this).active_n - 1 - i] * second; } while (res.active_n > 1 && res.digits[res.active_n - 1] == 0) --res.active_n; return res; }
const LI LI :: powLI (const int & n) { LI one; one.From_Int(1); LI res = one; for (int i = 0; i <= n; ++i) res = res.Karatsuba (*this); return res; }
/** * @param numbers: Give an array numbersbers of n integer * @param target: you need to find four elements that's sum of target * @return: Find all unique quadruplets in the array which gives the sum of * zero. */ LLI fourSum(LI nums, int target) { sort(nums.begin(), nums.end()); LLI res; LI set; fourSum(res, set, nums, 0, target); return res; }
const LI LI :: powLI (LI & n) { LI zero, one; zero.From_Int(0); one.From_Int(1); LI res = one; for (LI i = zero; n > i; i = i + one) res = res.Karatsuba(*this); return res; }
int LRUCacheMiss(vector<int> input, int maxSize) { typedef list<int> LI; typedef list<int>::iterator LII; // key iterator typedef unordered_map<int, LII> MLII; // key, (key iterator) LI cache; MLII check; int count = 0; for (int i = 0; i < input.size(); i++) { if (check.count(input[i]) > 0) { // found in the LRU cache. cache.erase(check[input[i]]); cache.push_front(input[i]); check[input[i]] = cache.begin(); } else { // not found, miss happens. count++; cache.push_front(input[i]); check[input[i]] = cache.begin(); if (cache.size() > maxSize) { int key = cache.back(); cache.pop_back(); check.erase(key); } } } return count; }
void combine(LLI &results, LI &set, int parent, int n, int k) { if (set.size() == k) { results.push_back(set); return; } for (int i = parent + 1; i <= n; ++i) { set.push_back(i); combine(results, set, i, n, k); set.pop_back(); } }
void set(int key, int value) { auto it = cache.find(key); if(it!=cache.end()) { update(it); } else { if(used.size()==cap) { cache.erase(used.back()); used.pop_back(); } used.push_front(key); } cache[key] = {value, used.begin()}; }
const LI LI :: gcd(const LI & second) { LI a = (*this), b = second; LI zero; zero.From_Int(0); if (a == zero) return b; if (b == zero) return a; while (b != zero) { a = a % b; a.swap(b); } return a; }
const void LI::S_S() // Solovay–Strassen primality test { int k = 3; LI zero, one, two, three; zero.From_Int(0); one.From_Int(1); two.From_Int(2); three.From_Int(3); int N = (*this).active_n; LI a; srand(time(NULL)); for (int i = 0; i < k; ++i) { a = three + (*this) / (1 + rand() % k); while (three > a) a = a + one; LI nod = (*this).gcd(a); if (nod > one) { cout << "The number is composite\n"; return; } LI st = (*this - one) / 2; LI mod, A, JacLI; int Jac; mod = a.pow_mod(st, (*this)); Jac = a.Jacobi(*this); JacLI.From_Int(Jac); if (JacLI == -one ) JacLI = (*this) - one; if (mod != JacLI) { cout << "The number is composite\n"; return; } } cout << "The number is probably prime\n"; return; }
/* const int LI :: Jacobi( LI & n) { cout << "in Jacobi \n"; LI a = (*this), a1; int symb; LI zero, one; zero.From_Int(0); one.From_Int(1); symb = 1; if (a == zero) return 0; if (a == one) return 1; LI dop = a, n1; int i; for (i = 0; dop%2 == 0; ++i) dop = dop / 2; a1 = dop; if (i % 2 == 1) symb = 1; else { if ((n % 8 == 1) || (n % 8 == -1)) symb = 1; if ((n % 8 == 3) || (n % 8 == -3)) symb = -1; } if ((n % 4 == 3) && (a1 % 4 == 3)) symb = -symb; n1 = n % a1; if (a1 == one) return symb; else return (n1.Jacobi(a1)) * symb; } */ const int LI::Jacobi(const LI & n) { LI a = (*this), c; LI zero, one; LI dop = n; zero.From_Int(0); one.From_Int(1); LI nod = a.gcd(n); if (nod != one) return 0; int symb = 1, i; if (!(a >= zero)) { a = -a; if (dop % 4 == 3) symb = -symb; } do { for (i = 0; a % 2 == 0; ++i) a = a / 2; if (i % 2 == 1) { if (dop % 8 == 3 || dop % 8 == 5) symb = -symb; } if (a % 4 == 3 && dop % 4 == 3) symb = -symb; c = a; a = dop % a; dop = c; } while (a != zero); return symb; }
void fourSum(LLI &res, LI &set, const LI &nums, int lo, int target) { int sum = 0; for (const int &x : set) sum += x; if (set.size() == 4 && sum == target) { res.push_back(set); return; } if (4 <= set.size()) return; for (int i = lo; i < nums.size(); ++i) { if (i != lo && nums[i] == nums[i - 1]) continue; set.push_back(nums[i]); fourSum(res, set, nums, i + 1, target); set.pop_back(); } }
void search(TreeNode* root, int k1, int k2, LI &vals) { if (!root) return; int m = root->val; if (k1 < m) search(root->left, k1, k2, vals); if (k1 <= m && m <= k2) vals.push_back(root->val); if (m < k2) search(root->right, k1, k2, vals); }
const LI LI :: pow_mod(LI & n, const LI & mod) { LI res, zero, one, two, a = (*this); zero.From_Int(0); one.From_Int(1); two.From_Int(2); res = one; while (n != zero) { LI d; d.From_Int(n%2); if (d == zero) { n = n / 2; a = (a * a) % mod; } else { n = n - one; res = (res * a) % mod; } } return res; }
void combine(LLI &results, LI &set, LI &candidates, int k, int target) { int sum = 0; for (int x : set) sum += x; if (sum == target) { LI sorted = set; sort(sorted.begin(), sorted.end()); results.push_back(sorted); return; } if (target < sum) return; for (int i = k; i < candidates.size(); ++i) { set.push_back(candidates[i]); combine(results, set, candidates, i, target); set.pop_back(); } }
void f(){ a.f(); b.f(); }
void update(HPIL::iterator it){ int key = it->first; lru.erase(it->second.second); lru.push_front(key); it->second.second = lru.begin(); }
void push_back(int val){dt.push_back(val);}
void push_front(int val){dt.push_front(val);}
void DLL::print(){ LI::iterator it; for(it=dt.begin();it!=dt.end();++it) cout << (*it) << "->"; cout << "NULL" << endl; }
void update(HIPII::iterator it) { int key = it->first; used.erase(it->second.second); used.push_front(key); it->second.second = used.begin(); }
LI::iterator DLL::find(int val){ LI::iterator it; for(it=dt.begin();it!=dt.end();++it) if((*it) == val) return it; return it; }
void DLL::del(int val){ LI::iterator it=find(val); if(it!=dt.end()) dt.erase(it); }
int isEmpty(){ return dt.empty();}