257929a5e9 2013-01-21 kinaba: #include <iostream> 257929a5e9 2013-01-21 kinaba: #include <sstream> 257929a5e9 2013-01-21 kinaba: #include <iomanip> 257929a5e9 2013-01-21 kinaba: #include <vector> 257929a5e9 2013-01-21 kinaba: #include <string> 257929a5e9 2013-01-21 kinaba: #include <map> 257929a5e9 2013-01-21 kinaba: #include <set> 257929a5e9 2013-01-21 kinaba: #include <algorithm> 257929a5e9 2013-01-21 kinaba: #include <numeric> 257929a5e9 2013-01-21 kinaba: #include <iterator> 257929a5e9 2013-01-21 kinaba: #include <functional> 257929a5e9 2013-01-21 kinaba: #include <complex> 257929a5e9 2013-01-21 kinaba: #include <queue> 257929a5e9 2013-01-21 kinaba: #include <stack> 257929a5e9 2013-01-21 kinaba: #include <cmath> 257929a5e9 2013-01-21 kinaba: #include <cassert> 257929a5e9 2013-01-21 kinaba: using namespace std; 257929a5e9 2013-01-21 kinaba: typedef long long LL; 257929a5e9 2013-01-21 kinaba: typedef long double LD; 257929a5e9 2013-01-21 kinaba: typedef complex<LD> CMP; 257929a5e9 2013-01-21 kinaba: 257929a5e9 2013-01-21 kinaba: LL comb(LL n, LL k) 257929a5e9 2013-01-21 kinaba: { 257929a5e9 2013-01-21 kinaba: k = min(k, n-k); 257929a5e9 2013-01-21 kinaba: 257929a5e9 2013-01-21 kinaba: LL c = 1; 257929a5e9 2013-01-21 kinaba: for(LL i=0; i<k; ++i) 257929a5e9 2013-01-21 kinaba: c *= n-i, c /= i+1; 257929a5e9 2013-01-21 kinaba: return c; 257929a5e9 2013-01-21 kinaba: } 257929a5e9 2013-01-21 kinaba: 257929a5e9 2013-01-21 kinaba: class PenguinSledding { public: 257929a5e9 2013-01-21 kinaba: long long countDesigns(int numCheckpoints, vector <int> checkpoint1, vector <int> checkpoint2) 257929a5e9 2013-01-21 kinaba: { 257929a5e9 2013-01-21 kinaba: LL zero = 1; 257929a5e9 2013-01-21 kinaba: LL one = checkpoint1.size(); 257929a5e9 2013-01-21 kinaba: LL star = 0; 257929a5e9 2013-01-21 kinaba: LL triangle = 0; 257929a5e9 2013-01-21 kinaba: 257929a5e9 2013-01-21 kinaba: for(int center=1; center<=numCheckpoints; ++center) { 257929a5e9 2013-01-21 kinaba: int leaf = 0; 257929a5e9 2013-01-21 kinaba: for(int i=0; i<checkpoint1.size(); ++i) 257929a5e9 2013-01-21 kinaba: if(checkpoint1[i] == center) 257929a5e9 2013-01-21 kinaba: ++leaf; 257929a5e9 2013-01-21 kinaba: else if(checkpoint2[i] == center) 257929a5e9 2013-01-21 kinaba: ++leaf; 257929a5e9 2013-01-21 kinaba: for(int k=2; k<=leaf; ++k) 257929a5e9 2013-01-21 kinaba: star += comb(leaf, k); 257929a5e9 2013-01-21 kinaba: } 257929a5e9 2013-01-21 kinaba: 257929a5e9 2013-01-21 kinaba: set< pair<int,int> > conn; 257929a5e9 2013-01-21 kinaba: for(int i=0; i<checkpoint1.size(); ++i) { 257929a5e9 2013-01-21 kinaba: conn.insert(make_pair(checkpoint1[i], checkpoint2[i])); 257929a5e9 2013-01-21 kinaba: conn.insert(make_pair(checkpoint2[i], checkpoint1[i])); 257929a5e9 2013-01-21 kinaba: } 257929a5e9 2013-01-21 kinaba: for(int a=1; a<=numCheckpoints; ++a) 257929a5e9 2013-01-21 kinaba: for(int b=a+1; b<=numCheckpoints; ++b) 257929a5e9 2013-01-21 kinaba: for(int c=b+1; c<=numCheckpoints; ++c) 257929a5e9 2013-01-21 kinaba: if(conn.count(make_pair(a,b)) && conn.count(make_pair(b,c)) && conn.count(make_pair(c,a))) 257929a5e9 2013-01-21 kinaba: ++triangle; 257929a5e9 2013-01-21 kinaba: 257929a5e9 2013-01-21 kinaba: return zero + one + star + triangle; 257929a5e9 2013-01-21 kinaba: } 257929a5e9 2013-01-21 kinaba: }; 257929a5e9 2013-01-21 kinaba: 257929a5e9 2013-01-21 kinaba: // BEGIN CUT HERE 257929a5e9 2013-01-21 kinaba: #include <ctime> 257929a5e9 2013-01-21 kinaba: double start_time; string timer() 257929a5e9 2013-01-21 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 257929a5e9 2013-01-21 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 257929a5e9 2013-01-21 kinaba: { os << "{ "; 257929a5e9 2013-01-21 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 257929a5e9 2013-01-21 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 257929a5e9 2013-01-21 kinaba: void verify_case(const long long& Expected, const long long& Received) { 257929a5e9 2013-01-21 kinaba: bool ok = (Expected == Received); 257929a5e9 2013-01-21 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 257929a5e9 2013-01-21 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 257929a5e9 2013-01-21 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 257929a5e9 2013-01-21 kinaba: #define END verify_case(_, PenguinSledding().countDesigns(numCheckpoints, checkpoint1, checkpoint2));} 257929a5e9 2013-01-21 kinaba: int main(){ 257929a5e9 2013-01-21 kinaba: 257929a5e9 2013-01-21 kinaba: CASE(0) 257929a5e9 2013-01-21 kinaba: int numCheckpoints = 2; 257929a5e9 2013-01-21 kinaba: int checkpoint1_[] = {1}; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint1(checkpoint1_, checkpoint1_+sizeof(checkpoint1_)/sizeof(*checkpoint1_)); 257929a5e9 2013-01-21 kinaba: int checkpoint2_[] = {2}; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint2(checkpoint2_, checkpoint2_+sizeof(checkpoint2_)/sizeof(*checkpoint2_)); 257929a5e9 2013-01-21 kinaba: long long _ = 2LL; 257929a5e9 2013-01-21 kinaba: END 257929a5e9 2013-01-21 kinaba: CASE(1) 257929a5e9 2013-01-21 kinaba: int numCheckpoints = 4; 257929a5e9 2013-01-21 kinaba: int checkpoint1_[] = {1,2,3}; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint1(checkpoint1_, checkpoint1_+sizeof(checkpoint1_)/sizeof(*checkpoint1_)); 257929a5e9 2013-01-21 kinaba: int checkpoint2_[] = {2,3,4}; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint2(checkpoint2_, checkpoint2_+sizeof(checkpoint2_)/sizeof(*checkpoint2_)); 257929a5e9 2013-01-21 kinaba: long long _ = 6LL; 257929a5e9 2013-01-21 kinaba: END 257929a5e9 2013-01-21 kinaba: CASE(2) 257929a5e9 2013-01-21 kinaba: int numCheckpoints = 6; 257929a5e9 2013-01-21 kinaba: int checkpoint1_[] = {1,3,6}; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint1(checkpoint1_, checkpoint1_+sizeof(checkpoint1_)/sizeof(*checkpoint1_)); 257929a5e9 2013-01-21 kinaba: int checkpoint2_[] = {2,4,4}; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint2(checkpoint2_, checkpoint2_+sizeof(checkpoint2_)/sizeof(*checkpoint2_)); 257929a5e9 2013-01-21 kinaba: long long _ = 5LL; 257929a5e9 2013-01-21 kinaba: END 257929a5e9 2013-01-21 kinaba: CASE(3) 257929a5e9 2013-01-21 kinaba: int numCheckpoints = 50; 257929a5e9 2013-01-21 kinaba: int checkpoint1_[] = {40, 40, 40, 40, 40, 40, 40, 40, 257929a5e9 2013-01-21 kinaba: 40, 40, 40, 40, 50, 40, 40, 40, 257929a5e9 2013-01-21 kinaba: 23, 4, 24, 40, 22, 40, 8, 40, 40, 257929a5e9 2013-01-21 kinaba: 40, 34, 21, 40, 40, 38, 40, 40, 257929a5e9 2013-01-21 kinaba: 40, 40, 40, 28, 40, 40, 40, 40, 257929a5e9 2013-01-21 kinaba: 46, 13, 40, 40, 40, 47, 40, 40}; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint1(checkpoint1_, checkpoint1_+sizeof(checkpoint1_)/sizeof(*checkpoint1_)); 257929a5e9 2013-01-21 kinaba: int checkpoint2_[] = {45, 42, 20, 48, 12, 32, 25, 10, 257929a5e9 2013-01-21 kinaba: 26, 39, 16, 2, 19, 43, 37, 17, 257929a5e9 2013-01-21 kinaba: 19, 19, 19, 18, 19, 27, 19, 29, 257929a5e9 2013-01-21 kinaba: 11, 36, 19, 19, 1, 41, 19, 35, 257929a5e9 2013-01-21 kinaba: 14, 33, 49, 3, 19, 7, 5, 19, 31, 257929a5e9 2013-01-21 kinaba: 19, 19, 6, 9, 15, 19, 44, 30}; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint2(checkpoint2_, checkpoint2_+sizeof(checkpoint2_)/sizeof(*checkpoint2_)); 257929a5e9 2013-01-21 kinaba: long long _ = 68719493118LL; 257929a5e9 2013-01-21 kinaba: END 257929a5e9 2013-01-21 kinaba: CASE(4) 257929a5e9 2013-01-21 kinaba: int numCheckpoints = 36; 257929a5e9 2013-01-21 kinaba: int checkpoint1_[] = {13, 24, 24, 20, 31, 16, 10, 36, 34, 32, 257929a5e9 2013-01-21 kinaba: 28, 5, 20, 29, 23, 2, 14, 4, 9, 5, 19, 257929a5e9 2013-01-21 kinaba: 21, 8, 1, 26, 27, 25, 15, 22, 30, 30, 257929a5e9 2013-01-21 kinaba: 6, 11, 7, 2, 35, 13, 29, 4, 12, 33, 18, 257929a5e9 2013-01-21 kinaba: 13, 14, 17, 35, 3}; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint1(checkpoint1_, checkpoint1_+sizeof(checkpoint1_)/sizeof(*checkpoint1_)); 257929a5e9 2013-01-21 kinaba: int checkpoint2_[] = {10, 15, 27, 1, 29, 11, 5, 18, 33, 1, 9, 257929a5e9 2013-01-21 kinaba: 2, 31, 6, 19, 10, 33, 18, 6, 27, 3, 22, 257929a5e9 2013-01-21 kinaba: 4, 12, 31, 30, 34, 16, 7, 24, 3, 28, 15, 257929a5e9 2013-01-21 kinaba: 21, 22, 8, 26, 20, 14, 32, 25, 17, 35, 257929a5e9 2013-01-21 kinaba: 8, 36, 26, 23}; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint2(checkpoint2_, checkpoint2_+sizeof(checkpoint2_)/sizeof(*checkpoint2_)); 257929a5e9 2013-01-21 kinaba: long long _ = 162LL; 257929a5e9 2013-01-21 kinaba: END 257929a5e9 2013-01-21 kinaba: /* 257929a5e9 2013-01-21 kinaba: CASE(5) 257929a5e9 2013-01-21 kinaba: int numCheckpoints = ; 257929a5e9 2013-01-21 kinaba: int checkpoint1_[] = ; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint1(checkpoint1_, checkpoint1_+sizeof(checkpoint1_)/sizeof(*checkpoint1_)); 257929a5e9 2013-01-21 kinaba: int checkpoint2_[] = ; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint2(checkpoint2_, checkpoint2_+sizeof(checkpoint2_)/sizeof(*checkpoint2_)); 257929a5e9 2013-01-21 kinaba: long long _ = LL; 257929a5e9 2013-01-21 kinaba: END 257929a5e9 2013-01-21 kinaba: CASE(6) 257929a5e9 2013-01-21 kinaba: int numCheckpoints = ; 257929a5e9 2013-01-21 kinaba: int checkpoint1_[] = ; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint1(checkpoint1_, checkpoint1_+sizeof(checkpoint1_)/sizeof(*checkpoint1_)); 257929a5e9 2013-01-21 kinaba: int checkpoint2_[] = ; 257929a5e9 2013-01-21 kinaba: vector <int> checkpoint2(checkpoint2_, checkpoint2_+sizeof(checkpoint2_)/sizeof(*checkpoint2_)); 257929a5e9 2013-01-21 kinaba: long long _ = LL; 257929a5e9 2013-01-21 kinaba: END 257929a5e9 2013-01-21 kinaba: */ 257929a5e9 2013-01-21 kinaba: } 257929a5e9 2013-01-21 kinaba: // END CUT HERE