3ec8b9143e 2013-07-19 kinaba: #include <iostream> 3ec8b9143e 2013-07-19 kinaba: #include <sstream> 3ec8b9143e 2013-07-19 kinaba: #include <iomanip> 3ec8b9143e 2013-07-19 kinaba: #include <vector> 3ec8b9143e 2013-07-19 kinaba: #include <string> 3ec8b9143e 2013-07-19 kinaba: #include <map> 3ec8b9143e 2013-07-19 kinaba: #include <set> 3ec8b9143e 2013-07-19 kinaba: #include <algorithm> 3ec8b9143e 2013-07-19 kinaba: #include <numeric> 3ec8b9143e 2013-07-19 kinaba: #include <iterator> 3ec8b9143e 2013-07-19 kinaba: #include <functional> 3ec8b9143e 2013-07-19 kinaba: #include <complex> 3ec8b9143e 2013-07-19 kinaba: #include <queue> 3ec8b9143e 2013-07-19 kinaba: #include <stack> 3ec8b9143e 2013-07-19 kinaba: #include <cmath> 3ec8b9143e 2013-07-19 kinaba: #include <cassert> 3ec8b9143e 2013-07-19 kinaba: using namespace std; 3ec8b9143e 2013-07-19 kinaba: typedef long long LL; 3ec8b9143e 2013-07-19 kinaba: typedef long double LD; 3ec8b9143e 2013-07-19 kinaba: typedef complex<double> CMP; 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: template<typename T> 3ec8b9143e 2013-07-19 kinaba: vector<int> compress(const vector<T>& xs, int origin = 0) 3ec8b9143e 2013-07-19 kinaba: { 3ec8b9143e 2013-07-19 kinaba: vector< pair<T,int> > xi; 3ec8b9143e 2013-07-19 kinaba: for(int i=0; i<xs.size(); ++i) 3ec8b9143e 2013-07-19 kinaba: xi.push_back( make_pair(xs[i], i) ); 3ec8b9143e 2013-07-19 kinaba: sort(xi.begin(), xi.end()); 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: vector<int> result(xs.size()); 3ec8b9143e 2013-07-19 kinaba: for(int k=0,id=origin; k<xi.size(); ++k) { 3ec8b9143e 2013-07-19 kinaba: if(0<=k-1 && xi[k-1].first<xi[k].first) 3ec8b9143e 2013-07-19 kinaba: ++id; 3ec8b9143e 2013-07-19 kinaba: result[xi[k].second] = id; 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: return result; 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: std::vector<int> compress(const std::vector<int>& xs) // convert to 1-origin rank 3ec8b9143e 2013-07-19 kinaba: { 3ec8b9143e 2013-07-19 kinaba: set<int> s(xs.begin(), xs.end()); 3ec8b9143e 2013-07-19 kinaba: map<int,int> mp; 3ec8b9143e 2013-07-19 kinaba: int ID = 1; 3ec8b9143e 2013-07-19 kinaba: for(set<int>::iterator it=s.begin(); it!=s.end(); ++it) 3ec8b9143e 2013-07-19 kinaba: mp[*it] = ID++; 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: std::vector<int> result(xs.size()); 3ec8b9143e 2013-07-19 kinaba: for(int k=0; k<xs.size(); ++k) 3ec8b9143e 2013-07-19 kinaba: result[k] = mp[xs[k]]; 3ec8b9143e 2013-07-19 kinaba: return result; 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: LL C(LL n, LL k) 3ec8b9143e 2013-07-19 kinaba: { 3ec8b9143e 2013-07-19 kinaba: if(n<k) 3ec8b9143e 2013-07-19 kinaba: return 0; 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: k = min(k, n-k); 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: LL c = 1; 3ec8b9143e 2013-07-19 kinaba: for(LL i=0; i<k; ++i) 3ec8b9143e 2013-07-19 kinaba: c *= n-i, c /= i+1; 3ec8b9143e 2013-07-19 kinaba: return c; 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: class Excavations { public: 3ec8b9143e 2013-07-19 kinaba: long long count(vector <int> kind, vector <int> depth, vector <int> found, int K) 3ec8b9143e 2013-07-19 kinaba: { 3ec8b9143e 2013-07-19 kinaba: depth = compress(depth, 1); 3ec8b9143e 2013-07-19 kinaba: int MD = *max_element(depth.begin(), depth.end()); 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: map< int, vector<int> > kind_to_depth; 3ec8b9143e 2013-07-19 kinaba: set<int> found_set(found.begin(), found.end()); 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: for(int i=0; i<kind.size(); ++i) 3ec8b9143e 2013-07-19 kinaba: kind_to_depth[kind[i]].push_back(depth[i]); 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: vector< vector<LL> > tbl_f(K+1, vector<LL>(MD+1)); 3ec8b9143e 2013-07-19 kinaba: vector< vector<LL> > tbl_nf(K+1, vector<LL>(MD+1)); 3ec8b9143e 2013-07-19 kinaba: for(int d=0; d<=MD; ++d) 3ec8b9143e 2013-07-19 kinaba: tbl_f[0][d] = tbl_nf[0][d] = 1; 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: for(map<int,vector<int> >::iterator it=kind_to_depth.begin(); it != kind_to_depth.end(); ++it) { 3ec8b9143e 2013-07-19 kinaba: bool f = !!found_set.count(it->first); 3ec8b9143e 2013-07-19 kinaba: vector<int> ds = it->second; 3ec8b9143e 2013-07-19 kinaba: sort(ds.begin(), ds.end()); 3ec8b9143e 2013-07-19 kinaba: vector< vector<LL> > tbl2 = calc_table(K, MD, ds, f); 3ec8b9143e 2013-07-19 kinaba: vector< vector<LL> >& tbl = (f ? tbl_f : tbl_nf); 3ec8b9143e 2013-07-19 kinaba: tbl = combine(K, MD, tbl, tbl2); 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: LL total = 0; 3ec8b9143e 2013-07-19 kinaba: for(int k=0; k<=K; ++k) 3ec8b9143e 2013-07-19 kinaba: total += cross(tbl_f[k], tbl_nf[K-k]); 3ec8b9143e 2013-07-19 kinaba: return total; 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: LL cross(const vector<LL>& f, const vector<LL>& n) 3ec8b9143e 2013-07-19 kinaba: { 3ec8b9143e 2013-07-19 kinaba: vector< pair<int,LL> > pf; 3ec8b9143e 2013-07-19 kinaba: for(int d=0; d<f.size(); ++d) { 3ec8b9143e 2013-07-19 kinaba: LL cnt = (d==0 ? f[d] : f[d]-f[d-1]); 3ec8b9143e 2013-07-19 kinaba: if(cnt) { 3ec8b9143e 2013-07-19 kinaba: pf.push_back(make_pair(d, cnt)); 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: vector< pair<int,LL> > nf; 3ec8b9143e 2013-07-19 kinaba: for(int d=0; d<n.size(); ++d) { 3ec8b9143e 2013-07-19 kinaba: LL cnt = (d==n.size()-1 ? n[d] : n[d]-n[d+1]); 3ec8b9143e 2013-07-19 kinaba: if(cnt) { 3ec8b9143e 2013-07-19 kinaba: nf.push_back(make_pair(d, cnt)); 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: LL total = 0; 3ec8b9143e 2013-07-19 kinaba: for(int i=0; i<pf.size(); ++i) 3ec8b9143e 2013-07-19 kinaba: for(int k=0; k<nf.size(); ++k) 3ec8b9143e 2013-07-19 kinaba: { 3ec8b9143e 2013-07-19 kinaba: if(pf[i].first <= nf[k].first) 3ec8b9143e 2013-07-19 kinaba: total += pf[i].second * nf[k].second; 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: return total; 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: vector< vector<LL> > calc_table(int K, int D, const vector<int>& ds, bool found) 3ec8b9143e 2013-07-19 kinaba: { 3ec8b9143e 2013-07-19 kinaba: vector< vector<LL> > tbl(K+1, vector<LL>(D+1)); 3ec8b9143e 2013-07-19 kinaba: for(int d=0; d<=D; ++d) 3ec8b9143e 2013-07-19 kinaba: { 3ec8b9143e 2013-07-19 kinaba: int num_discover = upper_bound(ds.begin(), ds.end(), d) - ds.begin(); 3ec8b9143e 2013-07-19 kinaba: for(int k=0; k<=K; ++k) 3ec8b9143e 2013-07-19 kinaba: tbl[k][d] = (found ? (k==0 ? 0 : C(num_discover, k)) : C(ds.size()-num_discover, k)); 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: return tbl; 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: vector< vector<LL> > combine(int K, int D, const vector<vector<LL> >& s, const vector<vector<LL> >& t) 3ec8b9143e 2013-07-19 kinaba: { 3ec8b9143e 2013-07-19 kinaba: vector< vector<LL> > r(K+1, vector<LL>(D+1)); 3ec8b9143e 2013-07-19 kinaba: for(int ks=0; ks<=K; ++ks) 3ec8b9143e 2013-07-19 kinaba: for(int kt=0; kt<=K; ++kt) if(ks+kt<=K) 3ec8b9143e 2013-07-19 kinaba: for(int d=0; d<=D; ++d) 3ec8b9143e 2013-07-19 kinaba: r[ks+kt][d] += s[ks][d] * t[kt][d]; 3ec8b9143e 2013-07-19 kinaba: return r; 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: }; 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: // BEGIN CUT HERE 3ec8b9143e 2013-07-19 kinaba: #include <ctime> 3ec8b9143e 2013-07-19 kinaba: double start_time; string timer() 3ec8b9143e 2013-07-19 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 3ec8b9143e 2013-07-19 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 3ec8b9143e 2013-07-19 kinaba: { os << "{ "; 3ec8b9143e 2013-07-19 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 3ec8b9143e 2013-07-19 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 3ec8b9143e 2013-07-19 kinaba: void verify_case(const long long& Expected, const long long& Received) { 3ec8b9143e 2013-07-19 kinaba: bool ok = (Expected == Received); 3ec8b9143e 2013-07-19 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 3ec8b9143e 2013-07-19 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 3ec8b9143e 2013-07-19 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 3ec8b9143e 2013-07-19 kinaba: #define END verify_case(_, Excavations().count(kind, depth, found, K));} 3ec8b9143e 2013-07-19 kinaba: int main(){ 3ec8b9143e 2013-07-19 kinaba: 3ec8b9143e 2013-07-19 kinaba: CASE(0) 3ec8b9143e 2013-07-19 kinaba: int kind_[] = {1, 1, 2, 2}; 3ec8b9143e 2013-07-19 kinaba: vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 3ec8b9143e 2013-07-19 kinaba: int depth_[] = {10, 15, 10, 20}; 3ec8b9143e 2013-07-19 kinaba: vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 3ec8b9143e 2013-07-19 kinaba: int found_[] = {1}; 3ec8b9143e 2013-07-19 kinaba: vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 3ec8b9143e 2013-07-19 kinaba: int K = 2; 3ec8b9143e 2013-07-19 kinaba: long long _ = 3LL; 3ec8b9143e 2013-07-19 kinaba: END 3ec8b9143e 2013-07-19 kinaba: CASE(1) 3ec8b9143e 2013-07-19 kinaba: int kind_[] = {1, 1, 2, 2}; 3ec8b9143e 2013-07-19 kinaba: vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 3ec8b9143e 2013-07-19 kinaba: int depth_[] = {10, 15, 10, 20}; 3ec8b9143e 2013-07-19 kinaba: vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 3ec8b9143e 2013-07-19 kinaba: int found_[] = {1, 2}; 3ec8b9143e 2013-07-19 kinaba: vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 3ec8b9143e 2013-07-19 kinaba: int K = 2; 3ec8b9143e 2013-07-19 kinaba: long long _ = 4LL; 3ec8b9143e 2013-07-19 kinaba: END 3ec8b9143e 2013-07-19 kinaba: CASE(2) 3ec8b9143e 2013-07-19 kinaba: int kind_[] = {1, 2, 3, 4}; 3ec8b9143e 2013-07-19 kinaba: vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 3ec8b9143e 2013-07-19 kinaba: int depth_[] = {10, 10, 10, 10}; 3ec8b9143e 2013-07-19 kinaba: vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 3ec8b9143e 2013-07-19 kinaba: int found_[] = {1, 2}; 3ec8b9143e 2013-07-19 kinaba: vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 3ec8b9143e 2013-07-19 kinaba: int K = 3; 3ec8b9143e 2013-07-19 kinaba: long long _ = 0LL; 3ec8b9143e 2013-07-19 kinaba: END 3ec8b9143e 2013-07-19 kinaba: CASE(3) 3ec8b9143e 2013-07-19 kinaba: int kind_[] = {1, 2, 2, 3, 1, 3, 2, 1, 2}; 3ec8b9143e 2013-07-19 kinaba: vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 3ec8b9143e 2013-07-19 kinaba: int depth_[] = {12512, 12859, 125, 1000, 99, 114, 125, 125, 114}; 3ec8b9143e 2013-07-19 kinaba: vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 3ec8b9143e 2013-07-19 kinaba: int found_[] = {1, 2, 3}; 3ec8b9143e 2013-07-19 kinaba: vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 3ec8b9143e 2013-07-19 kinaba: int K = 7; 3ec8b9143e 2013-07-19 kinaba: long long _ = 35LL; 3ec8b9143e 2013-07-19 kinaba: END 3ec8b9143e 2013-07-19 kinaba: CASE(4) 3ec8b9143e 2013-07-19 kinaba: int kind_[] = {50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50}; 3ec8b9143e 2013-07-19 kinaba: vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 3ec8b9143e 2013-07-19 kinaba: int depth_[] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}; 3ec8b9143e 2013-07-19 kinaba: vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 3ec8b9143e 2013-07-19 kinaba: int found_[] = {50}; 3ec8b9143e 2013-07-19 kinaba: vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 3ec8b9143e 2013-07-19 kinaba: int K = 18; 3ec8b9143e 2013-07-19 kinaba: long long _ = 9075135300LL; 3ec8b9143e 2013-07-19 kinaba: END 3ec8b9143e 2013-07-19 kinaba: /* 3ec8b9143e 2013-07-19 kinaba: CASE(5) 3ec8b9143e 2013-07-19 kinaba: int kind_[] = ; 3ec8b9143e 2013-07-19 kinaba: vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 3ec8b9143e 2013-07-19 kinaba: int depth_[] = ; 3ec8b9143e 2013-07-19 kinaba: vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 3ec8b9143e 2013-07-19 kinaba: int found_[] = ; 3ec8b9143e 2013-07-19 kinaba: vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 3ec8b9143e 2013-07-19 kinaba: int K = ; 3ec8b9143e 2013-07-19 kinaba: long long _ = LL; 3ec8b9143e 2013-07-19 kinaba: END 3ec8b9143e 2013-07-19 kinaba: CASE(6) 3ec8b9143e 2013-07-19 kinaba: int kind_[] = ; 3ec8b9143e 2013-07-19 kinaba: vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 3ec8b9143e 2013-07-19 kinaba: int depth_[] = ; 3ec8b9143e 2013-07-19 kinaba: vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 3ec8b9143e 2013-07-19 kinaba: int found_[] = ; 3ec8b9143e 2013-07-19 kinaba: vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 3ec8b9143e 2013-07-19 kinaba: int K = ; 3ec8b9143e 2013-07-19 kinaba: long long _ = LL; 3ec8b9143e 2013-07-19 kinaba: END 3ec8b9143e 2013-07-19 kinaba: */ 3ec8b9143e 2013-07-19 kinaba: } 3ec8b9143e 2013-07-19 kinaba: // END CUT HERE