string minMultiples(int N, vector <int> forbiddenDigits) { LL _N = N; int f[10] = {0}; int i; for (i = 0; i < (int)forbiddenDigits.size(); ++i) { f[forbiddenDigits[i] % 10] = 1; } IntSet allowed; for (i = 0; i < 10; ++i) { if (!f[i]) { allowed.insert(i); } } const LL Max = 1000000000; LL ans = Max; IntSet M; IIQueue Q; IntSet::const_iterator it; for (it = allowed.begin(); it != allowed.end(); ++it) { Q.push(LLPair(*it, 10)); } while (!Q.empty()) { LLPair p = Q.front(); Q.pop(); LL n = p.first; if (n != 0) { int r = (int)(n % N); if (r == 0) { ans = min(ans, n); continue; } else if (M.find(r) != M.end()) { continue; } else { M.insert(r); } } if (ans < Max) { continue; } for (it = allowed.begin(); it != allowed.end(); ++it) { Q.push(LLPair(p.second * *it + n, p.second * 10)); } } if (ans < Max) { char res[32]; if (ans <= 99999999) { sprintf(res, "%lld", ans); } else { sprintf(res, "%lld", ans); int digits = strlen(res); sprintf(res + 3, "...%d(%d digits)", (int)(ans % 1000), digits); } return res; } return "IMPOSSIBLE"; }
static void Set_Intersect(const IntSet& aset, IntSet& bset) { IntSet tmp; tmp.swap(bset); std::set_intersection( aset.begin(), aset.end(), tmp.begin(), tmp.end(), std::inserter(bset, bset.begin())); }
string ableToDivide(int n) { n *= 2; IntSet::const_iterator it; for (it = prime.begin(); it != prime.end(); ++it) { int x = n - *it; if (x < 2) { break; } if (prime.find(x) != prime.end()) { return "YES"; } } return "NO"; }
IntSet multiplySet(const IntSet& s, int f) { IntSet ret; for (IntSet::const_iterator it = s.begin(); it != s.end(); ++it) ret.insert(*it * f); return ret; }
int maxCities(int n, vector <int> a, vector <int> b, vector <int> len) { if (n <= 2) { return n; } int d[50][50]; memset(d, 0x3f, sizeof(d)); for (int i = 0; i < (int)a.size(); ++i) { d[a[i]-1][b[i]-1] = len[i]; d[b[i]-1][a[i]-1] = len[i]; } for (int k = 0; k < n; ++k) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } int ans = 2; for (int i = 0; i < n; ++i) { for (int j = i+1; j < n; ++j) { int r = d[i][j]; if (r < 1e6) { IntSet s; s.insert(i); s.insert(j); for (int k = 0; k < n; ++k) { if (i != k && j != k) { IntSet::const_iterator it; for (it = s.begin(); it != s.end(); ++it) { if (d[k][*it] != r) { break; } } if (it == s.end()) { s.insert(k); } } } ans = max(ans, (int)s.size()); } } } return ans; }
// """ ------------------------------------------------------- // // Assignment operator // The bits of the other SBV are copied. // // """ ------------------------------------------------------- SparseBitVect &SparseBitVect::operator=(const SparseBitVect &other) { IntSet *bv = other.dp_bits; delete dp_bits; d_size = other.getNumBits(); dp_bits = new IntSet; std::copy(bv->begin(), bv->end(), std::inserter(*dp_bits, dp_bits->end())); return *this; }
Graph::Graph(IntSet vSet, adjHash& edges) { this->vSet = vSet; // Loop throug each vertex IntSet::iterator u; for(u = vSet.begin(); u != vSet.end(); u++) { IntSet& incVertices = edges[*u]; // Add only those which are in vSet IntSet::iterator v; for(v = incVertices.begin(); v != incVertices.end(); v++) { if (vSet.find(*v) != vSet.end()) { this->eSet[*u].insert(*v); } } } }
int getFortunate(vector <int> a, vector <int> b, vector <int> c) { IntSet ab; IntSet Z; VI::const_iterator ia, ib, ic; for (ia = a.begin(); ia != a.end(); ++ia) { for (ib = b.begin(); ib != b.end(); ++ib) { ab.insert(*ia + *ib); } } for (ic = c.begin(); ic != c.end(); ++ic) { IntSet::const_iterator s; for (s = ab.begin(); s != ab.end(); ++s) { Z.insert(*ic + *s); } } int r = 0; IntSet::const_iterator z; for (z = Z.begin(); z != Z.end(); ++z) { if (isFortunate(*z)) { ++r; } } return r; }
bool IntSet::IsEqual(const IntSet & my_set) const { set<int>::const_iterator it1, it2; it1 =begin(); it2 = my_set.begin(); while(it1 != end()){ if(*it1 != *it2) return false; it1++, it2++; } if(it2 == my_set.end()) return true; return false; }
inline void DiscrepancyCorrection:: apply_multiplicative(const Variables& vars, RealVector& approx_fns) { for (ISIter it=surrogateFnIndices.begin(); it!=surrogateFnIndices.end(); ++it) approx_fns[*it] *= multCorrections[*it].value(vars); }
/** * Note: This method assumes that there really are some preferred nodes and that those nodes * are probably active. So do not use it as a replacement for the version without preferred nodes! * * @param allowNonPreferredNodes true to enable use of non-preferred nodes if not enough * preferred nodes are active to satisfy numNodes * @param outNodes might cotain less than numNodes if not enough nodes are known */ void NodeStoreServers::chooseStorageNodesWithPref(unsigned numNodes, UInt16List* preferredNodes, bool allowNonPreferredNodes, UInt16Vector* outNodes) { SafeMutexLock mutexLock(&mutex); // L O C K if(!localNode && !activeNodes.size() ) { // there's nothing we can do without any storage targets mutexLock.unlock(); // U N L O C K return; } NodeReferencer localNodeRefer(localNode, false); /* don't move this into if-brackets, because it would be removed from stack then and we need to access it again further below.*/ // temporary insertion of localNode to include it in the possible storage targets if(localNode) activeNodes.insert(NodeMapVal(localNode->getNumID(), &localNodeRefer) ); unsigned nodesSize = activeNodes.size(); // max number of nodes is limited by the number of known active nodes if(numNodes > nodesSize) numNodes = nodesSize; // Stage 1: add all the preferred nodes that are actually available to the outNodes /* note: we use a separate map for the outNodes here to quickly find out (in stage 2) whether we already added a certain node from the preferred nodes (in stage 1) */ IntSet outNodesSet; UInt16ListIter preferredIter; NodeMapIter activeNodesIter; // (will be re-used in stage 2) moveIterToRandomElem<UInt16List, UInt16ListIter>(*preferredNodes, preferredIter); // walk over all the preferred nodes and add them to outNodes when they are available // (note: iterTmp is just used to avoid calling preferredNodes->size() ) for(UInt16ListIter iterTmp = preferredNodes->begin(); (iterTmp != preferredNodes->end() ) && numNodes; iterTmp++) { activeNodesIter = activeNodes.find(*preferredIter); if(activeNodesIter != activeNodes.end() ) { // this preferred node is active => add to outNodes and to outNodesSet outNodes->push_back(*preferredIter); outNodesSet.insert(*preferredIter); numNodes--; } moveIterToNextRingElem<UInt16List, UInt16ListIter>(*preferredNodes, preferredIter); } // Stage 2: add the remaining requested number of nodes from the active nodes /* if numNodes is greater than 0 then we have some requested nodes left, that could not be taken from the preferred nodes */ /* we keep it simple here, because usually there will be enough preferred nodes available, so that this case is quite unlikely */ if(allowNonPreferredNodes && numNodes) { IntSetIter outNodesSetIter; moveIterToRandomElem<NodeMap, NodeMapIter>(activeNodes, activeNodesIter); // while we haven't found the number of requested nodes while(numNodes) { outNodesSetIter = outNodesSet.find(activeNodesIter->first); if(outNodesSetIter == outNodesSet.end() ) { outNodes->push_back(activeNodesIter->first); outNodesSet.insert(activeNodesIter->first); numNodes--; } moveIterToNextRingElem<NodeMap, NodeMapIter>(activeNodes, activeNodesIter); } } if(localNode) // remove local node activeNodes.erase(localNode->getNumID() ); mutexLock.unlock(); // U N L O C K }