Artifact Content
Not logged in

Artifact 925481462bafb3b08cdb2195e70c11ee992d97df


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class MultiplesWithLimit { public:
	string minMultiples(int N, vector <int> forbiddenDigits)
	{
		vector<int> allowed;
		for(int i=0; i<=9; ++i)
			if( find(forbiddenDigits.begin(), forbiddenDigits.end(), i) == forbiddenDigits.end() )
				allowed.push_back(i);

		vector< vector< pair<char,int> > > G(N);
		for(int v=0; v<N; ++v)
			for(int k=0; k<allowed.size(); ++k)
				G[v].push_back( make_pair(char('0'+allowed[k]), (v*10 + allowed[k])%N) );

		vector<int> dist_to_zero(N, INF);
		queue<int> Q;
		dist_to_zero[0] = 0;
		Q.push(0);
		while( !Q.empty() )
		{
			int v = Q.front();
			Q.pop();
			for(int i=0; i<allowed.size(); ++i)
			{
				int vv = (allowed[i]*POW(10,dist_to_zero[v],N) + v) % N;
				if( dist_to_zero[vv] == INF ) {
					dist_to_zero[vv] = dist_to_zero[v] + 1;
					Q.push(vv);
				}
			}
		}

		string all;
		int v = 0;
		do {
			int best = INF;
			int besta = -1;
			for(int i=0; i<allowed.size(); ++i)
				if(v>0 || allowed[i]>0)
				{
					int vv = (v*10 + allowed[i]) % N;
					if( dist_to_zero[vv] < best ) {
						best = dist_to_zero[vv];
						besta = allowed[i];
					}
				}
			if( best == INF )
				return "IMPOSSIBLE";
			all += char('0' + besta);
			v = (v*10 + besta) % N;
			cerr << v << " " << dist_to_zero[v] << " " << besta << endl;
		} while( v );

		if( all.size() <= 8 )
			return all;
		else {
			stringstream ss;
			ss << all.substr(0,3) << "..." << all.substr(all.size()-3) << "(" << all.size() << " digits)";
			return ss.str();
		}
	}

	int POW(int v, int e, int m)
	{
		int r = 1%m;
		for(; e; e>>=1,v=v*v%m)
			if( e&1 )
				r = r*v%m;
		return r;
	}

	static const int INF = 0x1fffffff;
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const string& Expected, const string& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, MultiplesWithLimit().minMultiples(N, forbiddenDigits));}
int main(){

CASE(0)
	int N = 8; 
	int forbiddenDigits_[] = {2,3,4,5,6,7,8,9};
	  vector <int> forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); 
	string _ = "1000"; 
END
CASE(1)
	int N = 9; 
	int forbiddenDigits_[] = {1,3,4,5,6,7,8,9};
	  vector <int> forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); 
	string _ = "222...222(9 digits)"; 
END
CASE(2)
	int N = 524; 
	int forbiddenDigits_[] = {5,2,4};
	  vector <int> forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); 
	string _ = "3668"; 
END
CASE(3)
	int N = 10000; 
	int forbiddenDigits_[] = {0};
	  vector <int> forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); 
	string _ = "IMPOSSIBLE"; 
END
CASE(4)
	int N = 1; 
	int forbiddenDigits_[] = {0,1,2,3,4,5,6,7,8,9};
	  vector <int> forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); 
	string _ = "IMPOSSIBLE"; 
END
/*
CASE(5)
	int N = ; 
	int forbiddenDigits_[] = ;
	  vector <int> forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); 
	string _ = ; 
END
CASE(6)
	int N = ; 
	int forbiddenDigits_[] = ;
	  vector <int> forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); 
	string _ = ; 
END
*/
}
// END CUT HERE