4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL gcd(LL a, LL b) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: while(a) swap(a, b%=a); return b; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: LL lcm(LL a, LL b) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: return a/gcd(a,b)*b; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class IngredientProportions 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: vector <int> getMasses(vector <string> proportions) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N = proportions.size() + 1; 4fd800b3a8 2011-02-23 kinaba: vector<LL> a(N-1),b(N-1),p(N-1),q(N-1); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<proportions.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: stringstream sin(proportions[i]); 4fd800b3a8 2011-02-23 kinaba: sin.get();//# 4fd800b3a8 2011-02-23 kinaba: sin>>a[i]; 4fd800b3a8 2011-02-23 kinaba: sin.get();sin.get();sin.get();sin.get();sin.get();sin.get();//" and #" 4fd800b3a8 2011-02-23 kinaba: sin>>b[i]; 4fd800b3a8 2011-02-23 kinaba: sin.get();sin.get();sin.get();sin.get();//" as " 4fd800b3a8 2011-02-23 kinaba: sin>>p[i]; 4fd800b3a8 2011-02-23 kinaba: sin.get(); //: 4fd800b3a8 2011-02-23 kinaba: sin>>q[i]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<LL> g(N, -1); g[0]=1; 4fd800b3a8 2011-02-23 kinaba: for(int _=0; _<N; ++_) 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N-1; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL A=a[i],B=b[i],P=p[i],Q=q[i]; 4fd800b3a8 2011-02-23 kinaba: if( g[A]==-1 && g[B]!=-1 ) 4fd800b3a8 2011-02-23 kinaba: swap(A,B), swap(P,Q); 4fd800b3a8 2011-02-23 kinaba: if( g[A]!=-1 && g[B]==-1 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL f = lcm(g[A],P) / g[A]; 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<N; ++j) 4fd800b3a8 2011-02-23 kinaba: if( g[j] != -1 ) 4fd800b3a8 2011-02-23 kinaba: g[j] *= f; 4fd800b3a8 2011-02-23 kinaba: g[B] = g[A]/P*Q; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: LL gg = accumulate( g.begin(), g.end(), g.back(), gcd ); 4fd800b3a8 2011-02-23 kinaba: vector<int> ans(N); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: ans[i] = g[i]/gg; 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const vector <int> &Expected, const vector <int> &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: string proportions_[] = {"#0 and #1 as 6:4"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {3, 2 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IngredientProportions().getMasses(proportions)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string proportions_[] = {"#0 and #1 as 9:8","#1 and #2 as 9:8"} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {81, 72, 64 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IngredientProportions().getMasses(proportions)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string proportions_[] = {"#4 and #0 as 1:1", "#4 and #1 as 3:1", "#4 and #2 as 5:1", "#4 and #3 as 7:1"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {105, 35, 21, 15, 105 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IngredientProportions().getMasses(proportions)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string proportions_[] = {"#2 and #3 as 6:8", "#0 and #1 as 9:3", "#3 and #0 as 7:5"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {60, 20, 63, 84 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, IngredientProportions().getMasses(proportions)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: