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";
	}
Example #2
0
	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";
	}
Example #3
0
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);
            }
        }
    }

}
Example #4
0
/**
 * 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
}