Artifact 8458c6f3c3bb106e6ed964b90ab84e200462b7ee
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
typedef long long LL;
class FairTournament
{
public:
vector<string> memo;
string add(const string& a, const string& b)
{
int n = max(a.size(), b.size()), carry=0;
string c(n, '0');
for(int i=0; i<n; ++i) {
int x = (a.size()<=i ? 0 : a[a.size()-1-i]-'0')
+ (b.size()<=i ? 0 : b[b.size()-1-i]-'0') + carry;
c[n-1-i] = char('0'+x%10);
carry = x/10;
}
if( carry ) c = char('0'+carry)+c;
return c;
}
string countPermutations(int n, int k)
{
memo.clear();
memo.resize(n<<k+1+k, "X");
int pending = (1<<k+1) - 1; // 0b00111 (case k=2)
return rec(pending, 0, n, k);
}
string rec(int pending, int i, int n, int k)
{
if( i == n )
return "1";
int key = (i<<k+1+k) | pending;
if( memo[key]!="X" )
return memo[key];
string sum = "";
for(int m=1,j=k; m<=pending; m<<=1,j--)
if( (pending & m) && i+j<n )
{
int p2 = ((pending^m)<<1) | 1;
if( p2 >= (1<<k+1+k) ) // 0b100000
continue;
sum = add(sum, rec(p2, i+1, n, k));
}
return memo[key]=sum;
}
// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
private:
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(); }
void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arg0 = 3; int Arg1 = 1; string Arg2 = "3"; verify_case(0, Arg2, countPermutations(Arg0, Arg1)); }
void test_case_1() { int Arg0 = 3; int Arg1 = 2; string Arg2 = "6"; verify_case(1, Arg2, countPermutations(Arg0, Arg1)); }
void test_case_2() { int Arg0 = 10; int Arg1 = 3; string Arg2 = "19708"; verify_case(2, Arg2, countPermutations(Arg0, Arg1)); }
void test_case_3() { int Arg0 = 100; int Arg1 = 1; string Arg2 = "573147844013817084101"; verify_case(3, Arg2, countPermutations(Arg0, Arg1)); }
// END CUT HERE
};
// BEGIN CUT HERE
int main() {
FairTournament().run_test(-1);
}
// END CUT HERE