6828085ccd 2015-09-11 kinaba: #include <iostream> 6828085ccd 2015-09-11 kinaba: #include <sstream> 6828085ccd 2015-09-11 kinaba: #include <iomanip> 6828085ccd 2015-09-11 kinaba: #include <vector> 6828085ccd 2015-09-11 kinaba: #include <string> 6828085ccd 2015-09-11 kinaba: #include <map> 6828085ccd 2015-09-11 kinaba: #include <set> 6828085ccd 2015-09-11 kinaba: #include <algorithm> 6828085ccd 2015-09-11 kinaba: #include <numeric> 6828085ccd 2015-09-11 kinaba: #include <iterator> 6828085ccd 2015-09-11 kinaba: #include <functional> 6828085ccd 2015-09-11 kinaba: #include <complex> 6828085ccd 2015-09-11 kinaba: #include <queue> 6828085ccd 2015-09-11 kinaba: #include <stack> 6828085ccd 2015-09-11 kinaba: #include <cmath> 6828085ccd 2015-09-11 kinaba: #include <cassert> 6828085ccd 2015-09-11 kinaba: #include <tuple> 6828085ccd 2015-09-11 kinaba: using namespace std; 6828085ccd 2015-09-11 kinaba: typedef long long LL; 6828085ccd 2015-09-11 kinaba: typedef complex<double> CMP; 6828085ccd 2015-09-11 kinaba: 6828085ccd 2015-09-11 kinaba: static const unsigned MODVAL = 1000000007; 6828085ccd 2015-09-11 kinaba: struct mint 6828085ccd 2015-09-11 kinaba: { 6828085ccd 2015-09-11 kinaba: unsigned val; 6828085ccd 2015-09-11 kinaba: mint():val(0){} 6828085ccd 2015-09-11 kinaba: mint(int x):val(x%MODVAL) {} 6828085ccd 2015-09-11 kinaba: mint(unsigned x):val(x%MODVAL) {} 6828085ccd 2015-09-11 kinaba: mint(LL x):val(x%MODVAL) {} 6828085ccd 2015-09-11 kinaba: }; 6828085ccd 2015-09-11 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 6828085ccd 2015-09-11 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 6828085ccd 2015-09-11 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 6828085ccd 2015-09-11 kinaba: mint operator+(mint x, mint y) { return x+=y; } 6828085ccd 2015-09-11 kinaba: mint operator-(mint x, mint y) { return x-=y; } 6828085ccd 2015-09-11 kinaba: mint operator*(mint x, mint y) { return x*=y; } 6828085ccd 2015-09-11 kinaba: 6828085ccd 2015-09-11 kinaba: class CountBinarySequences { public: 6828085ccd 2015-09-11 kinaba: int countSequences(int n, int k, vector <int> L, vector <int> R) 6828085ccd 2015-09-11 kinaba: { 6828085ccd 2015-09-11 kinaba: const int N = L.size(); 6828085ccd 2015-09-11 kinaba: vector<pair<int,int>> RNG; 6828085ccd 2015-09-11 kinaba: for(int i=0; i<N; ++i) 6828085ccd 2015-09-11 kinaba: RNG.emplace_back(L[i], R[i]); 6828085ccd 2015-09-11 kinaba: sort(RNG.begin(), RNG.end(), [&](pair<int,int> a, pair<int,int> b){ 6828085ccd 2015-09-11 kinaba: if(a.second-a.first != b.second-b.first) 6828085ccd 2015-09-11 kinaba: return a.second-a.first < b.second-b.first; 6828085ccd 2015-09-11 kinaba: return a<b; 6828085ccd 2015-09-11 kinaba: }); 6828085ccd 2015-09-11 kinaba: 6828085ccd 2015-09-11 kinaba: vector<vector<int>> child(N+1); 6828085ccd 2015-09-11 kinaba: for(int i=0; i<N; ++i) { 6828085ccd 2015-09-11 kinaba: int p = N; 6828085ccd 2015-09-11 kinaba: for(int k=i+1; k<N; ++k) 6828085ccd 2015-09-11 kinaba: if(RNG[k].first<=RNG[i].first && RNG[i].second<=RNG[k].second) 6828085ccd 2015-09-11 kinaba: {p=k; break;} 6828085ccd 2015-09-11 kinaba: child[p].emplace_back(i); 6828085ccd 2015-09-11 kinaba: } 6828085ccd 2015-09-11 kinaba: for(auto& cc: child) 6828085ccd 2015-09-11 kinaba: sort(cc.begin(), cc.end(), [&](int a, int b){ 6828085ccd 2015-09-11 kinaba: return RNG[a].first < RNG[b].first; 6828085ccd 2015-09-11 kinaba: }); 6828085ccd 2015-09-11 kinaba: 6828085ccd 2015-09-11 kinaba: RNG.emplace_back(1, n); 6828085ccd 2015-09-11 kinaba: 6828085ccd 2015-09-11 kinaba: memo.assign((N+1)*(k+1)*(k+1), -1); 6828085ccd 2015-09-11 kinaba: 6828085ccd 2015-09-11 kinaba: mint total = 0; 6828085ccd 2015-09-11 kinaba: for(int l=0; l<=k; ++l) 6828085ccd 2015-09-11 kinaba: for(int r=0; r<=k; ++r) 6828085ccd 2015-09-11 kinaba: total += solve(RNG, child, k, N, l, r); 6828085ccd 2015-09-11 kinaba: return total.val; 6828085ccd 2015-09-11 kinaba: } 6828085ccd 2015-09-11 kinaba: 6828085ccd 2015-09-11 kinaba: vector<int> memo; 6828085ccd 2015-09-11 kinaba: int solve( 6828085ccd 2015-09-11 kinaba: const vector<pair<int,int>>& RNG, 6828085ccd 2015-09-11 kinaba: const vector<vector<int>>& rng_list, 6828085ccd 2015-09-11 kinaba: const int K, 6828085ccd 2015-09-11 kinaba: int idx, int L1, int R1) { 6828085ccd 2015-09-11 kinaba: const bool must_be_even = (idx+1 == RNG.size()); 6828085ccd 2015-09-11 kinaba: const int key = (idx*(K+1)+L1)*(K+1)+R1; 6828085ccd 2015-09-11 kinaba: if(memo[key] != -1) return memo[key]; 6828085ccd 2015-09-11 kinaba: 6828085ccd 2015-09-11 kinaba: // RNG[idx] is the current range you need to consider. 6828085ccd 2015-09-11 kinaba: // rng_list[idx] is left-to-right sorted sub-ranges. 6828085ccd 2015-09-11 kinaba: // the region must be sum-even if must_be_even. 6828085ccd 2015-09-11 kinaba: // more than K consecutive 1's are not allowed. 6828085ccd 2015-09-11 kinaba: // left-end must have exactly L1 consecutive 1's. 6828085ccd 2015-09-11 kinaba: // right-end must have exactly R1 consecutive 1's. 6828085ccd 2015-09-11 kinaba: return memo[key] = 0; 6828085ccd 2015-09-11 kinaba: } 6828085ccd 2015-09-11 kinaba: }; 6828085ccd 2015-09-11 kinaba: 6828085ccd 2015-09-11 kinaba: // BEGIN CUT HERE 6828085ccd 2015-09-11 kinaba: #include <ctime> 6828085ccd 2015-09-11 kinaba: double start_time; string timer() 6828085ccd 2015-09-11 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 6828085ccd 2015-09-11 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 6828085ccd 2015-09-11 kinaba: { os << "{ "; 6828085ccd 2015-09-11 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 6828085ccd 2015-09-11 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 6828085ccd 2015-09-11 kinaba: void verify_case(const int& Expected, const int& Received) { 6828085ccd 2015-09-11 kinaba: bool ok = (Expected == Received); 6828085ccd 2015-09-11 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 6828085ccd 2015-09-11 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 6828085ccd 2015-09-11 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 6828085ccd 2015-09-11 kinaba: #define END verify_case(_, CountBinarySequences().countSequences(n, k, L, R));} 6828085ccd 2015-09-11 kinaba: int main(){ 6828085ccd 2015-09-11 kinaba: 6828085ccd 2015-09-11 kinaba: CASE(0) 6828085ccd 2015-09-11 kinaba: int n = 4; 6828085ccd 2015-09-11 kinaba: int k = 2; 6828085ccd 2015-09-11 kinaba: int L_[] = {2}; 6828085ccd 2015-09-11 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 6828085ccd 2015-09-11 kinaba: int R_[] = {3}; 6828085ccd 2015-09-11 kinaba: vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 6828085ccd 2015-09-11 kinaba: int _ = 5; 6828085ccd 2015-09-11 kinaba: END 6828085ccd 2015-09-11 kinaba: CASE(1) 6828085ccd 2015-09-11 kinaba: int n = 4; 6828085ccd 2015-09-11 kinaba: int k = 1; 6828085ccd 2015-09-11 kinaba: int L_[] = {2}; 6828085ccd 2015-09-11 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 6828085ccd 2015-09-11 kinaba: int R_[] = {2}; 6828085ccd 2015-09-11 kinaba: vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 6828085ccd 2015-09-11 kinaba: int _ = 6; 6828085ccd 2015-09-11 kinaba: END 6828085ccd 2015-09-11 kinaba: CASE(2) 6828085ccd 2015-09-11 kinaba: int n = 6; 6828085ccd 2015-09-11 kinaba: int k = 3; 6828085ccd 2015-09-11 kinaba: int L_[] = {1, 2, 3}; 6828085ccd 2015-09-11 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 6828085ccd 2015-09-11 kinaba: int R_[] = {6, 5, 4}; 6828085ccd 2015-09-11 kinaba: vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 6828085ccd 2015-09-11 kinaba: int _ = 6; 6828085ccd 2015-09-11 kinaba: END 6828085ccd 2015-09-11 kinaba: CASE(3) 6828085ccd 2015-09-11 kinaba: int n = 1000; 6828085ccd 2015-09-11 kinaba: int k = 4; 6828085ccd 2015-09-11 kinaba: int L_[] = {10, 101, 201, 110, 121}; 6828085ccd 2015-09-11 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 6828085ccd 2015-09-11 kinaba: int R_[] = {100, 200, 300, 120, 130}; 6828085ccd 2015-09-11 kinaba: vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 6828085ccd 2015-09-11 kinaba: int _ = 444743885; 6828085ccd 2015-09-11 kinaba: END 6828085ccd 2015-09-11 kinaba: CASE(4) 6828085ccd 2015-09-11 kinaba: int n = 1; 6828085ccd 2015-09-11 kinaba: int k = 5; 6828085ccd 2015-09-11 kinaba: int L_[] = {1}; 6828085ccd 2015-09-11 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 6828085ccd 2015-09-11 kinaba: int R_[] = {1}; 6828085ccd 2015-09-11 kinaba: vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 6828085ccd 2015-09-11 kinaba: int _ = 1; 6828085ccd 2015-09-11 kinaba: END 6828085ccd 2015-09-11 kinaba: CASE(5) 6828085ccd 2015-09-11 kinaba: int n = 998; 6828085ccd 2015-09-11 kinaba: int k = 2; 6828085ccd 2015-09-11 kinaba: int L_[] = {313,365,189,913,334,360,160,931,313,402,389,328,376,47,906,383,381,927,338,178,934,933,162,332,191,188,380,912,970,360,161,179,966,405,971,381}; 6828085ccd 2015-09-11 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 6828085ccd 2015-09-11 kinaba: int R_[] = {313,365,189,913,334,362,160,931,340,405,389,329,398,425,907,383,382,927,338,178,934,933,162,332,191,188,384,912,970,365,161,179,966,405,971,381}; 6828085ccd 2015-09-11 kinaba: vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 6828085ccd 2015-09-11 kinaba: int _ = 420889003; 6828085ccd 2015-09-11 kinaba: END 6828085ccd 2015-09-11 kinaba: /* 6828085ccd 2015-09-11 kinaba: CASE(6) 6828085ccd 2015-09-11 kinaba: int n = ; 6828085ccd 2015-09-11 kinaba: int k = ; 6828085ccd 2015-09-11 kinaba: int L_[] = ; 6828085ccd 2015-09-11 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 6828085ccd 2015-09-11 kinaba: int R_[] = ; 6828085ccd 2015-09-11 kinaba: vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 6828085ccd 2015-09-11 kinaba: int _ = ; 6828085ccd 2015-09-11 kinaba: END 6828085ccd 2015-09-11 kinaba: CASE(7) 6828085ccd 2015-09-11 kinaba: int n = ; 6828085ccd 2015-09-11 kinaba: int k = ; 6828085ccd 2015-09-11 kinaba: int L_[] = ; 6828085ccd 2015-09-11 kinaba: vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 6828085ccd 2015-09-11 kinaba: int R_[] = ; 6828085ccd 2015-09-11 kinaba: vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 6828085ccd 2015-09-11 kinaba: int _ = ; 6828085ccd 2015-09-11 kinaba: END 6828085ccd 2015-09-11 kinaba: */ 6828085ccd 2015-09-11 kinaba: } 6828085ccd 2015-09-11 kinaba: // END CUT HERE