c9008fc667 2012-08-08 kinaba: #include <iostream> c9008fc667 2012-08-08 kinaba: #include <sstream> c9008fc667 2012-08-08 kinaba: #include <iomanip> c9008fc667 2012-08-08 kinaba: #include <vector> c9008fc667 2012-08-08 kinaba: #include <string> c9008fc667 2012-08-08 kinaba: #include <map> c9008fc667 2012-08-08 kinaba: #include <set> c9008fc667 2012-08-08 kinaba: #include <algorithm> c9008fc667 2012-08-08 kinaba: #include <numeric> c9008fc667 2012-08-08 kinaba: #include <iterator> c9008fc667 2012-08-08 kinaba: #include <functional> c9008fc667 2012-08-08 kinaba: #include <complex> c9008fc667 2012-08-08 kinaba: #include <queue> c9008fc667 2012-08-08 kinaba: #include <stack> c9008fc667 2012-08-08 kinaba: #include <cmath> c9008fc667 2012-08-08 kinaba: #include <cassert> c9008fc667 2012-08-08 kinaba: using namespace std; c9008fc667 2012-08-08 kinaba: typedef long long LL; c9008fc667 2012-08-08 kinaba: typedef long double LD; c9008fc667 2012-08-08 kinaba: typedef complex<LD> CMP; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: template<typename Vert, typename Flow, int NV=16> c9008fc667 2012-08-08 kinaba: class MaxFlow c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: vector<int> G[NV]; c9008fc667 2012-08-08 kinaba: Flow F[NV][NV]; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: public: c9008fc667 2012-08-08 kinaba: void addEdge( Vert s, Vert t, Flow f ) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: G[s].push_back(t); c9008fc667 2012-08-08 kinaba: G[t].push_back(s); c9008fc667 2012-08-08 kinaba: F[s][t] = f; c9008fc667 2012-08-08 kinaba: F[t][s] = 0; c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: Flow calc( Vert S, Vert D ) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: for( Flow total=0 ;; ) { c9008fc667 2012-08-08 kinaba: // Do BFS and compute the level for each node. c9008fc667 2012-08-08 kinaba: int LV[NV] = {0}; c9008fc667 2012-08-08 kinaba: vector<int> Q(1, S); c9008fc667 2012-08-08 kinaba: for(int lv=1; !Q.empty(); ++lv) { c9008fc667 2012-08-08 kinaba: vector<int> Q2; c9008fc667 2012-08-08 kinaba: for(size_t i=0; i!=Q.size(); ++i) { c9008fc667 2012-08-08 kinaba: const vector<int>& ne = G[Q[i]]; c9008fc667 2012-08-08 kinaba: for(size_t j=0; j!=ne.size(); ++j) c9008fc667 2012-08-08 kinaba: if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S ) c9008fc667 2012-08-08 kinaba: LV[ne[j]]=lv, Q2.push_back(ne[j]); c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: Q.swap(Q2); c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: // Destination is now unreachable. Done. c9008fc667 2012-08-08 kinaba: if( !LV[D] ) c9008fc667 2012-08-08 kinaba: return total; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: // Iterating DFS. c9008fc667 2012-08-08 kinaba: bool blocked[NV] = {}; c9008fc667 2012-08-08 kinaba: total += dinic_dfs( S, D, LV, 0x7fffffff, blocked ); c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: private: c9008fc667 2012-08-08 kinaba: Flow dinic_dfs( int v, int D, int LV[], Flow flow_in, bool blocked[] ) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: Flow flow_out = 0; c9008fc667 2012-08-08 kinaba: for(size_t i=0; i!=G[v].size(); ++i) { c9008fc667 2012-08-08 kinaba: int u = G[v][i]; c9008fc667 2012-08-08 kinaba: if( LV[v]+1==LV[u] && F[v][u] ) { c9008fc667 2012-08-08 kinaba: Flow f = min(flow_in-flow_out, F[v][u]); c9008fc667 2012-08-08 kinaba: if( u==D || !blocked[u] && (f=dinic_dfs(u,D,LV,f,blocked))>0 ) { c9008fc667 2012-08-08 kinaba: F[v][u] -= f; c9008fc667 2012-08-08 kinaba: F[u][v] += f; c9008fc667 2012-08-08 kinaba: flow_out += f; c9008fc667 2012-08-08 kinaba: if( flow_in == flow_out ) return flow_out; c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: blocked[v] = (flow_out==0); c9008fc667 2012-08-08 kinaba: return flow_out; c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: }; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: class CosmicBlocks { public: c9008fc667 2012-08-08 kinaba: vector<int> blockTypes; c9008fc667 2012-08-08 kinaba: int N, minWays, maxWays; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: int getNumOrders(vector <int> blockTypes_, int minWays_, int maxWays_) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: N = blockTypes_.size(); c9008fc667 2012-08-08 kinaba: blockTypes = blockTypes_; c9008fc667 2012-08-08 kinaba: minWays = minWays_; c9008fc667 2012-08-08 kinaba: maxWays = maxWays_; c9008fc667 2012-08-08 kinaba: return try_all_latitude(); c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: int try_all_latitude() c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: vector<int> lat; c9008fc667 2012-08-08 kinaba: return rec_assign_latitude(&lat, (1<<N)-1, 0x7fffffff); c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: int rec_assign_latitude(vector<int>* lat, int mask, int prev_lat_blocks) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: if( mask == 0 ) c9008fc667 2012-08-08 kinaba: return try_all_touching_releation(*lat); c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: int total = 0; c9008fc667 2012-08-08 kinaba: for(int m=1; m<=mask; ++m) if( (m&mask)==m ) { c9008fc667 2012-08-08 kinaba: int sum = 0; c9008fc667 2012-08-08 kinaba: for(int i=0; (1<<i)<=m; ++i) if(1<<i & m) c9008fc667 2012-08-08 kinaba: sum+=blockTypes[i]; c9008fc667 2012-08-08 kinaba: if( prev_lat_blocks >= sum ) { c9008fc667 2012-08-08 kinaba: lat->push_back(m); c9008fc667 2012-08-08 kinaba: total += rec_assign_latitude(lat, mask&~m, sum); c9008fc667 2012-08-08 kinaba: lat->pop_back(); c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: return total; c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: int try_all_touching_releation(const vector<int>& lat) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: int total = 0; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: vector<int> fst = elements(lat[0]); c9008fc667 2012-08-08 kinaba: vector< pair<int,int> > adj = adjacent_blocks(lat); c9008fc667 2012-08-08 kinaba: for(int m=0; m<(1<<adj.size()); ++m) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: vector< vector<int> > up(N); c9008fc667 2012-08-08 kinaba: up.push_back(fst); c9008fc667 2012-08-08 kinaba: for(int i=0; (1<<i)<=m; ++i) if(1<<i & m) c9008fc667 2012-08-08 kinaba: up[adj[i].first].push_back(adj[i].second); c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: if( !possible(up) ) c9008fc667 2012-08-08 kinaba: continue; c9008fc667 2012-08-08 kinaba: int ways = calc_ways(up); c9008fc667 2012-08-08 kinaba: if( minWays<=ways && ways<=maxWays ) c9008fc667 2012-08-08 kinaba: ++total; c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: return total; c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: vector<int> elements(int mask) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: vector<int> result; c9008fc667 2012-08-08 kinaba: for(int i=0; (1<<i)<=mask; ++i) if(1<<i & mask) c9008fc667 2012-08-08 kinaba: result.push_back(i); c9008fc667 2012-08-08 kinaba: return result; c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: vector< pair<int,int> > adjacent_blocks(const vector<int>& lat) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: vector< pair<int,int> > result; c9008fc667 2012-08-08 kinaba: for(int i=1; i<lat.size(); ++i) c9008fc667 2012-08-08 kinaba: for(int a=0; (1<<a)<=lat[i-1]; ++a) if(1<<a & lat[i-1]) c9008fc667 2012-08-08 kinaba: for(int b=0; (1<<b)<=lat[i]; ++b) if(1<<b & lat[i]) c9008fc667 2012-08-08 kinaba: result.push_back(make_pair(a,b)); c9008fc667 2012-08-08 kinaba: return result; c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: int calc_ways(const vector< vector<int> >& up) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: vector<int> governed(N); c9008fc667 2012-08-08 kinaba: for(int i=0; i<up.size(); ++i) c9008fc667 2012-08-08 kinaba: for(int k=0; k<up[i].size(); ++k) c9008fc667 2012-08-08 kinaba: governed[up[i][k]] ++; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: // trivial check 2 c9008fc667 2012-08-08 kinaba: for(int i=0; i<governed.size(); ++i) c9008fc667 2012-08-08 kinaba: if(governed[i] == 0) c9008fc667 2012-08-08 kinaba: return 0; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: vector<int> memo(1<<N, -1); c9008fc667 2012-08-08 kinaba: return calc_ways_rec(N, (1<<N)-1, up, governed, memo); c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: int calc_ways_rec(int i, int mask, const vector< vector<int> >& up, vector<int>& governed, c9008fc667 2012-08-08 kinaba: vector<int>& memo) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: if(mask == 0) c9008fc667 2012-08-08 kinaba: return 1; c9008fc667 2012-08-08 kinaba: if(memo[mask] != -1) c9008fc667 2012-08-08 kinaba: return memo[mask]; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: int sum = 0; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: for(int k=0; k<up[i].size(); ++k) governed[up[i][k]]--; c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: for(int k=0; k<governed.size(); ++k) c9008fc667 2012-08-08 kinaba: if(!governed[k] && (mask & 1<<k)) c9008fc667 2012-08-08 kinaba: sum += calc_ways_rec(k, mask &~ (1<<k), up, governed, memo); c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: for(int k=0; k<up[i].size(); ++k) governed[up[i][k]]++; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: return memo[mask] = sum; c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: bool possible(const vector< vector<int> >& up) c9008fc667 2012-08-08 kinaba: { c9008fc667 2012-08-08 kinaba: static const int INF = 0x3fffffff; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: int total = accumulate(blockTypes.begin(), blockTypes.end(), 0); c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: MaxFlow<int, int> mf; c9008fc667 2012-08-08 kinaba: vector<int> out = blockTypes, in = blockTypes; c9008fc667 2012-08-08 kinaba: for(int v=0; v<=N; ++v) { c9008fc667 2012-08-08 kinaba: for(int i=0; i<up[v].size(); ++i) { c9008fc667 2012-08-08 kinaba: int u = up[v][i]; c9008fc667 2012-08-08 kinaba: mf.addEdge(v, N+1+u, INF); c9008fc667 2012-08-08 kinaba: if(v<N) {in[v] -= 1; if(in[v]<0) return false; } c9008fc667 2012-08-08 kinaba: out[u] -= 1; if(out[u]<0) return false; c9008fc667 2012-08-08 kinaba: total -= 1; c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: for(int v=0; v<=N; ++v) c9008fc667 2012-08-08 kinaba: if(v == N) { c9008fc667 2012-08-08 kinaba: mf.addEdge(N+1+N, v, INF); c9008fc667 2012-08-08 kinaba: } else { c9008fc667 2012-08-08 kinaba: mf.addEdge(N+1+N, v, in[v]); c9008fc667 2012-08-08 kinaba: mf.addEdge(N+1+v, N+1+N+1, out[v]); c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: return mf.calc(N+1+N, N+1+N+1) == total; c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: }; c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: // BEGIN CUT HERE c9008fc667 2012-08-08 kinaba: #include <ctime> c9008fc667 2012-08-08 kinaba: double start_time; string timer() c9008fc667 2012-08-08 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } c9008fc667 2012-08-08 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) c9008fc667 2012-08-08 kinaba: { os << "{ "; c9008fc667 2012-08-08 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) c9008fc667 2012-08-08 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } c9008fc667 2012-08-08 kinaba: void verify_case(const int& Expected, const int& Received) { c9008fc667 2012-08-08 kinaba: bool ok = (Expected == Received); c9008fc667 2012-08-08 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; c9008fc667 2012-08-08 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } c9008fc667 2012-08-08 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); c9008fc667 2012-08-08 kinaba: #define END verify_case(_, CosmicBlocks().getNumOrders(blockTypes, minWays, maxWays));} c9008fc667 2012-08-08 kinaba: int main(){ c9008fc667 2012-08-08 kinaba: c9008fc667 2012-08-08 kinaba: CASE(0) c9008fc667 2012-08-08 kinaba: int blockTypes_[] = {2,2,2}; c9008fc667 2012-08-08 kinaba: vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); c9008fc667 2012-08-08 kinaba: int minWays = 1; c9008fc667 2012-08-08 kinaba: int maxWays = 1; c9008fc667 2012-08-08 kinaba: int _ = 6; c9008fc667 2012-08-08 kinaba: END c9008fc667 2012-08-08 kinaba: CASE(1) c9008fc667 2012-08-08 kinaba: int blockTypes_[] = {1,1,1,1,1,1}; c9008fc667 2012-08-08 kinaba: vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); c9008fc667 2012-08-08 kinaba: int minWays = 720; c9008fc667 2012-08-08 kinaba: int maxWays = 720; c9008fc667 2012-08-08 kinaba: int _ = 1; c9008fc667 2012-08-08 kinaba: END c9008fc667 2012-08-08 kinaba: CASE(2) c9008fc667 2012-08-08 kinaba: int blockTypes_[] = {2,2}; c9008fc667 2012-08-08 kinaba: vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); c9008fc667 2012-08-08 kinaba: int minWays = 1; c9008fc667 2012-08-08 kinaba: int maxWays = 2; c9008fc667 2012-08-08 kinaba: int _ = 3; c9008fc667 2012-08-08 kinaba: END c9008fc667 2012-08-08 kinaba: CASE(3) c9008fc667 2012-08-08 kinaba: int blockTypes_[] = {1,2}; c9008fc667 2012-08-08 kinaba: vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); c9008fc667 2012-08-08 kinaba: int minWays = 1; c9008fc667 2012-08-08 kinaba: int maxWays = 2; c9008fc667 2012-08-08 kinaba: int _ = 2; c9008fc667 2012-08-08 kinaba: END c9008fc667 2012-08-08 kinaba: CASE(4) c9008fc667 2012-08-08 kinaba: int blockTypes_[] = {1}; c9008fc667 2012-08-08 kinaba: vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); c9008fc667 2012-08-08 kinaba: int minWays = 1; c9008fc667 2012-08-08 kinaba: int maxWays = 1; c9008fc667 2012-08-08 kinaba: int _ = 1; c9008fc667 2012-08-08 kinaba: END c9008fc667 2012-08-08 kinaba: CASE(5) c9008fc667 2012-08-08 kinaba: int blockTypes_[] = {1,2,4,8}; c9008fc667 2012-08-08 kinaba: vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); c9008fc667 2012-08-08 kinaba: int minWays = 5; c9008fc667 2012-08-08 kinaba: int maxWays = 30; c9008fc667 2012-08-08 kinaba: int _ = 27; c9008fc667 2012-08-08 kinaba: END c9008fc667 2012-08-08 kinaba: CASE(6) c9008fc667 2012-08-08 kinaba: int blockTypes_[] = {1,2,3,4,5,6}; c9008fc667 2012-08-08 kinaba: vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); c9008fc667 2012-08-08 kinaba: int minWays = 1; c9008fc667 2012-08-08 kinaba: int maxWays = 720; c9008fc667 2012-08-08 kinaba: int _ = 4445; c9008fc667 2012-08-08 kinaba: END c9008fc667 2012-08-08 kinaba: CASE(7) c9008fc667 2012-08-08 kinaba: int blockTypes_[] = {7500,1000,7500,1000,7500}; c9008fc667 2012-08-08 kinaba: vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); c9008fc667 2012-08-08 kinaba: int minWays = 8; c9008fc667 2012-08-08 kinaba: int maxWays = 88; c9008fc667 2012-08-08 kinaba: int _ = 448; c9008fc667 2012-08-08 kinaba: END c9008fc667 2012-08-08 kinaba: /* c9008fc667 2012-08-08 kinaba: CASE(8) c9008fc667 2012-08-08 kinaba: int blockTypes_[] = ; c9008fc667 2012-08-08 kinaba: vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); c9008fc667 2012-08-08 kinaba: int minWays = ; c9008fc667 2012-08-08 kinaba: int maxWays = ; c9008fc667 2012-08-08 kinaba: int _ = ; c9008fc667 2012-08-08 kinaba: END c9008fc667 2012-08-08 kinaba: CASE(9) c9008fc667 2012-08-08 kinaba: int blockTypes_[] = ; c9008fc667 2012-08-08 kinaba: vector <int> blockTypes(blockTypes_, blockTypes_+sizeof(blockTypes_)/sizeof(*blockTypes_)); c9008fc667 2012-08-08 kinaba: int minWays = ; c9008fc667 2012-08-08 kinaba: int maxWays = ; c9008fc667 2012-08-08 kinaba: int _ = ; c9008fc667 2012-08-08 kinaba: END c9008fc667 2012-08-08 kinaba: */ c9008fc667 2012-08-08 kinaba: } c9008fc667 2012-08-08 kinaba: // END CUT HERE