// Multiply a ciphertext vector by a plaintext dense matrix // and/or build a cache with the multiplication constants void multilpy(Ctxt* ctxt) { RBak bak; bak.save(); ea.getTab().restoreContext(); // idxes describes a genealized diagonal, {(i,idx[i])}_i // initially just the identity, idx[i]==i vector<long> idxes(ea.size()); for (long i = 0; i < ea.size(); i++) idxes[i] = i; // call the recursive procedure to do the actual work rec_mul(ctxt, 0, 0, idxes); if (ctxt!=nullptr && res!=nullptr) *ctxt = *res; // copy the result back to ctxt // "install" the cache (if needed) if (buildCache == cachezzX) mat.installzzxcache(zCache); else if (buildCache == cacheDCRT) mat.installDCRTcache(dCache); }
bool containsNearbyDuplicate(vector<int>& nums, int k) { if (nums.empty()) { return false; } vector<int> idxes(nums.size()); for (int i=0; i<nums.size(); ++i) { idxes[i] = i; } sort(idxes.begin(), idxes.end(), [&nums](int a, int b) { return nums[a] < nums[b]; }); int last = nums[idxes[0]]; int last_idx = idxes[0]; for (int i=1; i<idxes.size(); ++i) { if (last == nums[idxes[i]] && idxes[i] - last_idx <= k) { return true; } last = nums[idxes[i]]; last_idx = idxes[i]; } return false; }