Artifact Content
Not logged in

Artifact 6384de8b9346d7d3bb3fec91b861acec06f4deac


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const unsigned MODVAL = 1000000007;
struct mint
{
	unsigned val;
	mint():val(0){}
	mint(int      x):val(x%MODVAL) {}
	mint(unsigned x):val(x%MODVAL) {}
	mint(LL       x):val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint operator+(mint x, mint y) { return x+=y; }
mint operator-(mint x, mint y) { return x-=y; }
mint operator*(mint x, mint y) { return x*=y; }

class CountBinarySequences { public:
	int countSequences(int n, int k, vector <int> L, vector <int> R)
	{
		const int N = L.size();
		vector<pair<int,int>> RNG;
		for(int i=0; i<N; ++i)
			RNG.emplace_back(L[i], R[i]);
		sort(RNG.begin(), RNG.end(), [&](pair<int,int> a, pair<int,int> b){
			if(a.second-a.first != b.second-b.first)
				return a.second-a.first < b.second-b.first;
			return a<b;
		});

		vector<vector<int>> child(N+1);
		for(int i=0; i<N; ++i) {
			int p = N;
			for(int k=i+1; k<N; ++k)
				if(RNG[k].first<=RNG[i].first && RNG[i].second<=RNG[k].second)
					{p=k; break;}
			child[p].emplace_back(i);
		}
		for(auto& cc: child)
			sort(cc.begin(), cc.end(), [&](int a, int b){
				return RNG[a].first < RNG[b].first;
			});

		RNG.emplace_back(1, n);

		memo.assign((N+1)*(k+1)*(k+1), -1);

		mint total = 0;
		for(int l=0; l<=k; ++l)
		for(int r=0; r<=k; ++r)
			total += solve(RNG, child, k, N, l, r);
		return total.val;
	}

	vector<int> memo;
	int solve(
		const vector<pair<int,int>>& RNG,
		const vector<vector<int>>& rng_list,
		const int K,
		int idx, int L1, int R1) {
		const bool must_be_even = (idx+1 == RNG.size());
		const int key = (idx*(K+1)+L1)*(K+1)+R1;
		if(memo[key] != -1) return memo[key];

		// RNG[idx] is the current range you need to consider.
		// rng_list[idx] is left-to-right sorted sub-ranges.
		// the region must be sum-even if must_be_even.
		// more than K consecutive 1's are not allowed.
		// left-end must have exactly L1 consecutive 1's.
		// right-end must have exactly R1 consecutive 1's.
		return memo[key] = 0;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, CountBinarySequences().countSequences(n, k, L, R));}
int main(){

CASE(0)
	int n = 4; 
	int k = 2; 
	int L_[] = {2};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	int R_[] = {3};
	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 
	int _ = 5; 
END
CASE(1)
	int n = 4; 
	int k = 1; 
	int L_[] = {2};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	int R_[] = {2};
	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 
	int _ = 6; 
END
CASE(2)
	int n = 6; 
	int k = 3; 
	int L_[] = {1, 2, 3};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	int R_[] = {6, 5, 4};
	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 
	int _ = 6; 
END
CASE(3)
	int n = 1000; 
	int k = 4; 
	int L_[] = {10, 101, 201, 110, 121};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	int R_[] = {100, 200, 300, 120, 130};
	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 
	int _ = 444743885; 
END
CASE(4)
	int n = 1; 
	int k = 5; 
	int L_[] = {1};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	int R_[] = {1};
	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 
	int _ = 1; 
END
CASE(5)
	int n = 998; 
	int k = 2; 
	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};
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	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};
	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 
	int _ = 420889003; 
END
/*
CASE(6)
	int n = ; 
	int k = ; 
	int L_[] = ;
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	int R_[] = ;
	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 
	int _ = ; 
END
CASE(7)
	int n = ; 
	int k = ; 
	int L_[] = ;
	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); 
	int R_[] = ;
	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); 
	int _ = ; 
END
*/
}
// END CUT HERE