Artifact Content
Not logged in

Artifact fede1c18ad40c4e302bad9d908b24d007100b274


#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>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

template<typename Vert, typename Flow, int NV=16>
class MaxFlow
{
	vector<int> G[NV];
	Flow F[NV][NV];

public:
	void addEdge( Vert s, Vert t, Flow f )
	{
		G[s].push_back(t);
		G[t].push_back(s);
		F[s][t] = f;
		F[t][s] = 0;
	}

	Flow calc( Vert S, Vert D )
	{
		for( Flow total=0 ;; ) {
			// Do BFS and compute the level for each node.
			int LV[NV] = {0};
			vector<int> Q(1, S);
			for(int lv=1; !Q.empty(); ++lv) {
				vector<int> Q2;
				for(size_t i=0; i!=Q.size(); ++i) {
					const vector<int>& ne = G[Q[i]];
					for(size_t j=0; j!=ne.size(); ++j)
						if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S )
							LV[ne[j]]=lv, Q2.push_back(ne[j]);
				}
				Q.swap(Q2);
			}

			// Destination is now unreachable. Done.
			if( !LV[D] )
				return total;

			// Iterating DFS.
			bool blocked[NV] = {};
			total += dinic_dfs( S, D, LV, 0x7fffffff, blocked );
		}
	}

private:
	Flow dinic_dfs( int v, int D, int LV[], Flow flow_in, bool blocked[] )
	{
		Flow flow_out = 0;
		for(size_t i=0; i!=G[v].size(); ++i) {
			int u = G[v][i];
			if( LV[v]+1==LV[u] && F[v][u] ) {
				Flow f = min(flow_in-flow_out, F[v][u]);
				if( u==D || !blocked[u] && (f=dinic_dfs(u,D,LV,f,blocked))>0 ) {
					F[v][u]  -= f;
					F[u][v]  += f;
					flow_out += f;
					if( flow_in == flow_out ) return flow_out;
				}
			}
		}
		blocked[v] = (flow_out==0);
		return flow_out;
	}
};

class CosmicBlocks { public:
	vector<int> blockTypes;
	int N, minWays, maxWays;

	int getNumOrders(vector <int> blockTypes_, int minWays_, int maxWays_)
	{
		N          = blockTypes_.size();
		blockTypes = blockTypes_;
		minWays    = minWays_;
		maxWays    = maxWays_;
		return try_all_latitude();
	}

	int try_all_latitude()
	{
		vector<int> lat;
		return rec_assign_latitude(&lat, (1<<N)-1, 0x7fffffff);
	}

	int rec_assign_latitude(vector<int>* lat, int mask, int prev_lat_blocks)
	{
		if( mask == 0 )
			return try_all_touching_releation(*lat);

		int total = 0;
		for(int m=1; m<=mask; ++m) if( (m&mask)==m ) {
			int sum = 0;
			for(int i=0; (1<<i)<=m; ++i) if(1<<i & m)
				sum+=blockTypes[i];
			if( prev_lat_blocks >= sum ) {
				lat->push_back(m);
				total += rec_assign_latitude(lat, mask&~m, sum);
				lat->pop_back();
			}
		}
		return total;
	}

	int try_all_touching_releation(const vector<int>& lat)
	{
		int total = 0;

		vector<int> fst = elements(lat[0]);
		vector< pair<int,int> > adj = adjacent_blocks(lat);
		for(int m=0; m<(1<<adj.size()); ++m)
		{
			vector< vector<int> > up(N);
			up.push_back(fst);
			for(int i=0; (1<<i)<=m; ++i) if(1<<i & m)
				up[adj[i].first].push_back(adj[i].second);

			if( !possible(up) )
				continue;
			int ways = calc_ways(up);
			if( minWays<=ways && ways<=maxWays )
				++total;
		}
		return total;
	}

	vector<int> elements(int mask)
	{
		vector<int> result;
		for(int i=0; (1<<i)<=mask; ++i) if(1<<i & mask)
			result.push_back(i);
		return result;
	}

	vector< pair<int,int> > adjacent_blocks(const vector<int>& lat)
	{
		vector< pair<int,int> > result;
		for(int i=1; i<lat.size(); ++i)
			for(int a=0; (1<<a)<=lat[i-1]; ++a) if(1<<a & lat[i-1])
			for(int b=0; (1<<b)<=lat[i];   ++b) if(1<<b & lat[i])
				result.push_back(make_pair(a,b));
		return result;
	}

	int calc_ways(const vector< vector<int> >& up)
	{
		vector<int> governed(N);
		for(int i=0; i<up.size(); ++i)
			for(int k=0; k<up[i].size(); ++k)
				governed[up[i][k]] ++;

		// trivial check 2
		for(int i=0; i<governed.size(); ++i)
			if(governed[i] == 0)
				return 0;

		vector<int> memo(1<<N, -1);
		return calc_ways_rec(N, (1<<N)-1, up, governed, memo);
	}

	int calc_ways_rec(int i, int mask, const vector< vector<int> >& up, vector<int>& governed,
		vector<int>& memo)
	{
		if(mask == 0)
			return 1;
		if(memo[mask] != -1)
			return memo[mask];

		int sum = 0;

		for(int k=0; k<up[i].size(); ++k) governed[up[i][k]]--;
		{
			for(int k=0; k<governed.size(); ++k)
				if(!governed[k] && (mask & 1<<k))
					sum += calc_ways_rec(k, mask &~ (1<<k), up, governed, memo);
		}
		for(int k=0; k<up[i].size(); ++k) governed[up[i][k]]++;

		return memo[mask] = sum;
	}

	bool possible(const vector< vector<int> >& up)
	{
		static const int INF = 0x3fffffff;

		int total = accumulate(blockTypes.begin(), blockTypes.end(), 0);

		MaxFlow<int, int> mf;
		vector<int> out = blockTypes, in = blockTypes;
		for(int v=0; v<=N; ++v) {
			for(int i=0; i<up[v].size(); ++i) {
				int u = up[v][i];
				mf.addEdge(v, N+1+u, INF);
				if(v<N) {in[v] -= 1; if(in[v]<0) return false; }
				out[u] -= 1; if(out[u]<0) return false;
				total -= 1;
			}
		}
		for(int v=0; v<=N; ++v)
			if(v == N) {
				mf.addEdge(N+1+N, v, INF);
			} else {
				mf.addEdge(N+1+N, v, in[v]);
				mf.addEdge(N+1+v, N+1+N+1, out[v]);
			}
		return mf.calc(N+1+N, N+1+N+1) == total;
	}
};

// 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 int& Expected, const int& 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(_, CosmicBlocks().getNumOrders(blockTypes, minWays, maxWays));}
int main(){

CASE(0)
	int blockTypes_[] = {2,2,2};
	  vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); 
	int minWays = 1; 
	int maxWays = 1; 
	int _ = 6; 
END
CASE(1)
	int blockTypes_[] = {1,1,1,1,1,1};
	  vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); 
	int minWays = 720; 
	int maxWays = 720; 
	int _ = 1; 
END
CASE(2)
	int blockTypes_[] = {2,2};
	  vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); 
	int minWays = 1; 
	int maxWays = 2; 
	int _ = 3; 
END
CASE(3)
	int blockTypes_[] = {1,2};
	  vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); 
	int minWays = 1; 
	int maxWays = 2; 
	int _ = 2; 
END
CASE(4)
	int blockTypes_[] = {1};
	  vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); 
	int minWays = 1; 
	int maxWays = 1; 
	int _ = 1; 
END
CASE(5)
	int blockTypes_[] = {1,2,4,8};
	  vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); 
	int minWays = 5; 
	int maxWays = 30; 
	int _ = 27; 
END
CASE(6)
	int blockTypes_[] = {1,2,3,4,5,6};
	  vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); 
	int minWays = 1; 
	int maxWays = 720; 
	int _ = 4445; 
END
CASE(7)
	int blockTypes_[] = {7500,1000,7500,1000,7500};
	  vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); 
	int minWays = 8; 
	int maxWays = 88; 
	int _ = 448; 
END
/*
CASE(8)
	int blockTypes_[] = ;
	  vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); 
	int minWays = ; 
	int maxWays = ; 
	int _ = ; 
END
CASE(9)
	int blockTypes_[] = ;
	  vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); 
	int minWays = ; 
	int maxWays = ; 
	int _ = ; 
END
*/
}
// END CUT HERE