USACO: Longest Prefix



/*
 ID: tausiq11
 PROG: prefix
 LANG: C++
 */

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 200000 + 10
#define P 200 + 5
#define LL long long

inline LL Power(int b, int p) { LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b; return ret; }
const int dr [] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dc [] = {0, 1, 1, 1, 0, -1, -1, -1};

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(i, x) for(typeof (x.begin()) i = x.begin(); i != x.end (); i++)
#define Set(a, s) memset(a, s, sizeof (a))
#define max(a, b)  (a < b ? b : a)
#define min(a, b)  (a > b ? b : a)


using namespace std;

char input [N];

char sequence [N];

char primitives [P] [10 + 2];

int maxi;

int primitivesSize [P];

int lenPrimitives = 0;

int main ()
{
    freopen ("prefix.in", "r", stdin);
    freopen ("prefix.out", "w", stdout);
    
    while ( scanf ("%s", input) && strcmp (input, ".") != 0 ) {
        
        strcpy (primitives [lenPrimitives], input);
            
        primitivesSize [lenPrimitives++] = (int) strlen (input);
        
    }
    
    strcpy(sequence, "");
    
    while ( scanf ("%s", input) != EOF ) {
        
        strcat(sequence, input);
    }

    bool isAvailable [N];

    Set(isAvailable, false);

    isAvailable [0] = true;

    int sequenceLength = strlen(sequence);

    for ( int i = 0; i < sequenceLength; i++ ) {

        if ( isAvailable [i]) {

            for ( int j = 0; j < lenPrimitives; j++ ) {
                if (strncmp (sequence + i, primitives [j], (size_t) primitivesSize[j]) == 0) {
                    maxi = max (maxi, i + primitivesSize[j]);
                    isAvailable [i + primitivesSize [j]] = true;
                }

            }
        }
    }
    
    printf ("%d\n", maxi);
    
    return 0;
}

Response:


USER: Shadow King [tausiq11]
TASK: prefix
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3832 KB]
   Test 2: TEST OK [0.000 secs, 3836 KB]
   Test 3: TEST OK [0.000 secs, 3828 KB]
   Test 4: TEST OK [0.003 secs, 3836 KB]
   Test 5: TEST OK [0.000 secs, 3832 KB]
   Test 6: TEST OK [0.581 secs, 3832 KB]

All tests OK.
Your program ('prefix') produced all correct answers!  This is your
submission #9 for this problem.  Congratulations!

USACO: Party Lamps



/*
ID: tausiq11
PROG: lamps
LANG: C++
*/

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 1000000
#define LL long long

inline LL Power(int b, int p) { LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b; return ret; }
const int dr [] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dc [] = {0, 1, 1, 1, 0, -1, -1, -1};

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(i, x) for(typeof (x.begin()) i = x.begin(); i != x.end (); i++)
#define Set(a, s) memset(a, s, sizeof (a))
#define max(a, b)  (a < b ? b : a)
#define min(a, b)  (a > b ? b : a)

using namespace std;

vector <int> onElements;
vector <int> offElements;

vector <bool> applyButtonEffect(int button, vector<bool> v){
	switch (button) {
	case 0:
		for ( int i = 1; i < v.size(); i++ ) v [i] = !v [i];
		break;
	case 1:
		for ( int i = 1; i < v.size(); i += 2 ) v [i] = !v [i];
		break;
	case 2:
		for ( int i = 2; i < v.size(); i += 2 ) v [i] = !v [i];
		break;
	case 3:
		for ( int i = 1; i < v.size(); i += 3 ) v [i] = !v [i];
		break;
	default:
		break;
	}

	return v;
}

bool isMatchWithFinal(vector <bool> v){
	for ( int i = 0; i < onElements.size(); i++ ) {
		if (v [onElements [i]] == false) return false;
	}

	for ( int i = 0; i < offElements.size(); i++ ) {
		if (v [offElements [i]]) return false;
	}

	return true;
}

int main () {

	freopen ("lamps.in", "r", stdin);
	freopen ("lamps.out", "w", stdout);

	int n; scanf ("%d", &n);

	queue < vector<bool> > q;
	vector <bool> firstElement;

	// all lights are on 
	for ( int i = 0; i <= n; i++ ) firstElement.push_back(true);

	// we will try with BFS
	q.push(firstElement);

	int c; scanf ("%d", &c);
	// using a switch more than once in unnecessary 
	c = min (c, 4);

	int elem;
	while ( scanf ("%d", &elem) && elem != -1 ) {
		onElements.push_back(elem);
	}

	while ( scanf ("%d", &elem) && elem != -1 ) {
		offElements.push_back(elem);
	}

	// marking, if an element is visited 
	map< vector<bool>, bool> visited;
	visited [firstElement] = true;

	// we will get the whole output sorted, and without duplicacy 
	set< vector<bool> > output;

	if(isMatchWithFinal(firstElement)) output.insert(firstElement);

	int loop = 1; // how many steps we will make for a certain 'c' value 
	for ( int i = 0; i < c; i++ ) loop *= (4 - i);
	if(c == 0) loop = 0;	// if c = 0, there is not steps 

	while ( !q.empty() ) {
		vector <bool> popElem;
		popElem = q.front();
		q.pop();

		if(isMatchWithFinal(popElem))output.insert(popElem);

		if(loop) {
			vector <bool> newState;
			for ( int i = 0; i < 4; i++ ) {
				newState = applyButtonEffect (i, popElem);
				if(!visited [newState]) {
					visited [newState] = true;
					q.push(newState);
				}
				loop--;
			}
		}

	}

	if(output.size() == 0){
		printf ("IMPOSSIBLE\n");
	} else {
		set< vector<bool> >::iterator it;
		for(it = output.begin(); it != output.end(); it++) {
			for (int i = 1; i < (*it).size(); i++ ) {
				if ( (*it) [i] ) printf ("%d", 1);
				else printf ("%d", 0);
			}
			printf ("\n");
		}
	}

	return 0;
}

// @END_OF_SOURCE_CODE

USACO Training Grader Results


USER: Shadow King [tausiq11]
TASK: lamps
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3384 KB]
   Test 2: TEST OK [0.000 secs, 3384 KB]
   Test 3: TEST OK [0.000 secs, 3384 KB]
   Test 4: TEST OK [0.000 secs, 3384 KB]
   Test 5: TEST OK [0.000 secs, 3384 KB]
   Test 6: TEST OK [0.000 secs, 3384 KB]
   Test 7: TEST OK [0.000 secs, 3384 KB]
   Test 8: TEST OK [0.000 secs, 3384 KB]

All tests OK.
Your program ('lamps') produced all correct answers!  This is your
submission #2 for this problem.  Congratulations!

USACO: Runaround Numbers



/*
ID: tausiq11
PROG: runround
LANG: C++
 */

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 1000000
#define LL long long

inline LL Power(int b, int p) { LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b; return ret; }
const int dr [] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dc [] = {0, 1, 1, 1, 0, -1, -1, -1};

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(i, x) for(typeof (x.begin()) i = x.begin(); i != x.end (); i++)
#define Set(a, s) memset(a, s, sizeof (a))
#define max(a, b)  (a < b ? b : a)
#define min(a, b)  (a > b ? b : a)

using namespace std;

bool isRunRound (int num) {
	char str [9 + 3];
	sprintf (str, "%d", num);

	int frequency [10];
	Set(frequency, 0);	// all digits (0 to 9) frequency 0

	int len = strlen(str);
	int currNumber = str [0] - '0';
	int currIndex = 0;

	for ( int i = 0; i < len; i++ ) {
		currIndex = (currIndex + currNumber) % len;
		currNumber = str [currIndex] - '0';
		frequency [currNumber]++;

		// checking if currNumber = 0 or currNumber frequency greater than 1
		if(!currNumber || frequency [currNumber] > 1) return false;
	}

	return true;
}

int main () {

	freopen ("runround.in", "r", stdin);
	freopen ("runround.out", "w", stdout);

	int M; scanf ("%d", &M);

	while ( true ) {
		if ( isRunRound(++M) ) { 
			printf ("%d\n", M); 
			break; 
		} 
	}

	return 0;
}

// @END_OF_SOURCE_CODE

USACO Training Grader Results


USER: Shadow King [tausiq11]
TASK: runround
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3232 KB]
   Test 2: TEST OK [0.011 secs, 3232 KB]
   Test 3: TEST OK [0.000 secs, 3232 KB]
   Test 4: TEST OK [0.000 secs, 3232 KB]
   Test 5: TEST OK [0.119 secs, 3232 KB]
   Test 6: TEST OK [0.065 secs, 3232 KB]
   Test 7: TEST OK [0.205 secs, 3232 KB]

All tests OK.
YOUR PROGRAM ('runround') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.

USACO: Subset Sums



/*
ID: tausiq11
PROG: subset
LANG: C++
 */

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 1000000
#define LL long long

inline LL Power(int b, int p) { LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b; return ret; }
const int dr [] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dc [] = {0, 1, 1, 1, 0, -1, -1, -1};

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(i, x) for(typeof (x.begin()) i = x.begin(); i != x.end (); i++)
#define Set(a, s) memset(a, s, sizeof (a))
#define max(a, b)  (a < b ? b : a)
#define min(a, b)  (a > b ? b : a)

using namespace std;

long long dp [1600];

int main () {

	freopen ("subset.in", "r", stdin);
	freopen ("subset.out", "w", stdout);

	int n; scanf ("%d", &n);

	if ( ((n * (n + 1)) / 2) % 2 ) {
        printf ("0\n"); return 0;
	}

	Set (dp, 0);

	dp [0] = 1;		// we can make zero in 1 ways

	for ( int i = 1; i <= n; i++ ) {
		for ( int j = 1599; j >= 0; j-- ) {
			if ( dp [j] && j + i < 1600 )
				dp [j + i] += dp [j];
		}
	}

	//printf ("%d\n", ((n * (n + 1)) / 2));


	cout << dp [(n * (n + 1) / 2) / 2] / 2 << endl;

	return 0;
}

// @END_OF_SOURCE_CODE

Response


USER: Shadow King [tausiq11]
TASK: subset
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3356 KB]
   Test 2: TEST OK [0.000 secs, 3356 KB]
   Test 3: TEST OK [0.000 secs, 3356 KB]
   Test 4: TEST OK [0.000 secs, 3356 KB]
   Test 5: TEST OK [0.000 secs, 3356 KB]
   Test 6: TEST OK [0.000 secs, 3356 KB]
   Test 7: TEST OK [0.000 secs, 3356 KB]

All tests OK.
Your program ('subset') produced all correct answers!  This is your
submission #3 for this problem.  Congratulations!

USACO: Preface Numbering



/*
ID: tausiq11
PROG: preface
LANG: C++
 */

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 1000000
#define LL long long

inline LL Power(int b, int p) { LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b; return ret; }
const int dr [] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dc [] = {0, 1, 1, 1, 0, -1, -1, -1};

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(i, x) for(typeof (x.begin()) i = x.begin(); i != x.end (); i++)
#define Set(a, s) memset(a, s, sizeof (a))
#define max(a, b)  (a < b ? b : a)
#define min(a, b)  (a > b ? b : a)

using namespace std;

int cnt [7 + 3];

string romanLetter (int p) {

	if ( p == 0 ) return "";

	if ( p < 10 ) {
		if ( p == 1 ) return "I";
		if ( p == 2 ) return "II";
		if ( p == 3 ) return "III";
		if ( p == 4 ) return "IV";
		if ( p == 5 ) return "V";
		if ( p == 6 ) return "VI";
		if ( p == 7 ) return "VII";
		if ( p == 8 ) return "VIII";
		if ( p == 9 ) return "IX";
	} else if ( p < 100 ) {
		if ( p / 10 == 1 ) return "X" + romanLetter (p % 10);
		if ( p / 10 == 2 ) return "XX" + romanLetter (p % 10);
		if ( p / 10 == 3 ) return "XXX" + romanLetter (p % 10);
		if ( p / 10 == 4 ) return "XL" + romanLetter (p % 10);
		if ( p / 10 == 5 ) return "L" + romanLetter (p % 10);
		if ( p / 10 == 6 ) return "LX" + romanLetter (p % 10);
		if ( p / 10 == 7 ) return "LXX" + romanLetter (p % 10);
		if ( p / 10 == 8 ) return "LXXX" + romanLetter (p % 10);
		if ( p / 10 == 9 ) return "XC" + romanLetter (p % 10);
	} else if ( p < 1000 ) {
		if ( p / 100 == 1 ) return "C" + romanLetter (p % 100);
		if ( p / 100 == 2 ) return "CC" + romanLetter (p % 100);
		if ( p / 100 == 3 ) return "CCC" + romanLetter (p % 100);
		if ( p / 100 == 4 ) return "CD" + romanLetter (p % 100);
		if ( p / 100 == 5 ) return "D" + romanLetter (p % 100);
		if ( p / 100 == 6 ) return "DC" + romanLetter (p % 100);
		if ( p / 100 == 7 ) return "DCC" + romanLetter (p % 100);
		if ( p / 100 == 8 ) return "DCCC" + romanLetter (p % 100);
		if ( p / 100 == 9 ) return "CM" + romanLetter (p % 100);
	} else {
		if ( p / 1000 == 1 ) return "M" + romanLetter (p % 1000);
		if ( p / 1000 == 2 ) return "MM" + romanLetter (p % 1000);
		if ( p / 1000 == 3 ) return "MMM" + romanLetter (p % 1000);
		return "";
	}


}

string convertToRoman (int n) {
	string roman = "";

	if ( n > 999 ) {
		roman += romanLetter (n / 1000 * 1000); n %= 1000;
		roman += romanLetter (n / 100 * 100); n %= 100;
		roman += romanLetter (n / 10 * 10); n %= 10;
		roman += romanLetter (n);

	} else if ( n > 99 ) {
		roman += romanLetter (n / 100 * 100); n %= 100;
		roman += romanLetter (n / 10 * 10); n %= 10;
		roman += romanLetter (n);

	} else if ( n > 9 ) {
		roman += romanLetter (n / 10 * 10); n %= 10;
		roman += romanLetter (n);

	} else roman += romanLetter (n);

	return roman;
}

void cntRomanLetters (string str) {
	Fs (i, str) {
		if ( str [i] == 'I' ) cnt [0]++;
		else if ( str [i] == 'V' ) cnt [1]++;
		else if ( str [i] == 'X' ) cnt [2]++;
		else if ( str [i] == 'L' ) cnt [3]++;
		else if ( str [i] == 'C' ) cnt [4]++;
		else if ( str [i] == 'D' ) cnt [5]++;
		else cnt [6]++;
	}
}

int main ()
{
	freopen ("preface.in", "r", stdin);
	freopen ("preface.out", "w", stdout);

	int n; scanf ("%d", &n);

	Set (cnt, 0);

	for ( int i = 1; i <= n; i++ ) {
		string roman = convertToRoman (i);
		//cout << i << ": " << roman << endl;
		cntRomanLetters (roman);
	}

	if ( cnt [0] ) printf ("I %d\n", cnt [0]);
	if ( cnt [1] ) printf ("V %d\n", cnt [1]);
	if ( cnt [2] ) printf ("X %d\n", cnt [2]);
	if ( cnt [3] ) printf ("L %d\n", cnt [3]);
	if ( cnt [4] ) printf ("C %d\n", cnt [4]);
	if ( cnt [5] ) printf ("D %d\n", cnt [5]);
	if ( cnt [6] ) printf ("M %d\n", cnt [6]);

	return 0;
}

// @END_OF_SOURCE_CODE

Judge response

USER: [tausiq11]
TASK: preface
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.011 secs, 3352 KB]
   Test 2: TEST OK [0.000 secs, 3352 KB]
   Test 3: TEST OK [0.011 secs, 3352 KB]
   Test 4: TEST OK [0.011 secs, 3352 KB]
   Test 5: TEST OK [0.011 secs, 3352 KB]
   Test 6: TEST OK [0.022 secs, 3352 KB]
   Test 7: TEST OK [0.022 secs, 3352 KB]
   Test 8: TEST OK [0.022 secs, 3352 KB]

All tests OK.
YOUR PROGRAM ('preface') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.

USACO : Hamming Codes


/*
ID: tausiq11
PROG: hamming
LANG: C++
 */

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 100000
#define LL long long

const double EPS = 1e-9;
inline bool Equal(double a, double b) { return abs(a-b) < EPS; }
inline LL Power(int b, int p) { LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b; return ret; }
const int dr [] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dc [] = {0, 1, 1, 1, 0, -1, -1, -1};

#define F(i, b) for( int i = 0; i < (b); i++ )
#define Fs(i, x) for( size_t i = 0; i < x.size (); i++ )
#define Fe(i, x) for(typeof (x.begin()) i = x.begin(); i != x.end (); i++)
#define Set(a, s) memset(a, s, sizeof (a))
#define max(a, b)  (a < b ? b : a)
#define min(a, b)  (a > b ? b : a)
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

using namespace std;

int hammingDistance [255 + 3] [255 + 3];
int n, b, d;
vector <int> output;

void preGenerateValue ()
{
	int cnt;

	for ( int i = 0; i < 256; i++ ) {
		for ( int j = i; j < 256; j++ ) {
			cnt = 0;
			for ( int k = 0; k < 8; k++ ) {
				if ( (i & (1 << k)) ^ (j & (1 << k)) ) cnt++;
			}

			hammingDistance [i] [j] = hammingDistance [j] [i] = cnt;
		}
	}
}

bool findNextNumber (int p)
{
	Fs (i, output) {
		if ( hammingDistance [output [i]] [p] < d ) return false;
	}

	return true;
}

int main ()
{
	Rd ("hamming.in");
	Wt ("hamming.out");

	preGenerateValue ();

	scanf ("%d %d %d", &n, &b, &d);

	output.clear();
	output.push_back(0);

	int num = 1;

	while ( output.size() < n ) {
		while ( !findNextNumber (num) ) num++;
		output.push_back(num);
	}

	Fs (i, output) {
		if ( i % 10 != 0 ) printf (" ");
		printf ("%d", output [i]);
		if ( (i + 1) % 10 == 0 ) printf ("\n");
	}

	if ( output.size() % 10 != 0 ) printf ("\n");

	return 0;
}

// @END_OF_SOURCE_CODE

Judge Response

USER: [tausiq11]
TASK: hamming
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3444 KB]
   Test 2: TEST OK [0.000 secs, 3444 KB]
   Test 3: TEST OK [0.000 secs, 3444 KB]
   Test 4: TEST OK [0.000 secs, 3444 KB]
   Test 5: TEST OK [0.000 secs, 3444 KB]
   Test 6: TEST OK [0.000 secs, 3444 KB]
   Test 7: TEST OK [0.000 secs, 3444 KB]
   Test 8: TEST OK [0.000 secs, 3444 KB]
   Test 9: TEST OK [0.000 secs, 3444 KB]
   Test 10: TEST OK [0.000 secs, 3444 KB]
   Test 11: TEST OK [0.000 secs, 3444 KB]

All tests OK.
Your program ('hamming') produced all correct answers!  This is your
submission #3 for this problem.  Congratulations!

USACO : Healthy Holsteins


/*
ID: tausiq11
PROG: holstein
LANG: C++
 */

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 100000
#define LL long long

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(it, x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define Pr(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++) cout << *it << " "; cout << endl;
#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

using namespace std;

struct state {
	int arr [25 + 3];
	int step;
	int bitmask;
} a [32768 + 100];

int len;
int finalBitmask;
int finalBitCnt;

void checkBestSolution (int p)
{
	if ( finalBitCnt < a [p].step ) return;
	if ( finalBitCnt > a [p].step ) {
		finalBitCnt = a [p].step;
		finalBitmask = a [p].bitmask;
		return;
	}

	for ( int i = 0; i < 15; i++ ) {
		if ( (finalBitmask & (1 << i)) && !(a [p].bitmask & (1 << i)) ) return;
		if ( !(finalBitmask & (1 << i)) && (a [p].bitmask & (1 << i)) ) {
			finalBitCnt = a [p].step;
			finalBitmask = a [p].bitmask;
			return;
		}
	}
}

int main ()
{
	Rd ("holstein.in");
	Wt ("holstein.out");

	int v; scanf ("%d", &v);

	for ( int i = 0; i < v; i++ ) scanf ("%d", &a [0].arr [i]);
	a [0].step = 0;
	a [0].bitmask = 0;

	int g; scanf ("%d", &g);
	int temp [25 + 3];

	len = 1;
	finalBitCnt = g;
	finalBitmask = (1 << g) - 1;

	for ( int i = 0; i < g; i++ ) {
		for ( int j = 0; j < v; j++ ) scanf ("%d", &temp [j]);

		for ( int j = 0; j < len; j++ ) {

			bool allSet = true;
			a [len + j].step = a [j].step + 1;
			a [len + j].bitmask = a [j].bitmask | (1 << i);

			for ( int k = 0; k < v; k++ ) {
				a [len + j].arr [k] = a [j].arr [k] - temp [k];

				if ( a [len + j].arr [k] > 0 ) allSet = false;
			}

			if ( allSet ) {
				checkBestSolution (len + j);
			}
		}

		len *= 2;
	}

	printf ("%d", finalBitCnt);

	for ( int i = 0; i < g; i++ ) {
		if ( finalBitmask & (1 << i) ) printf (" %d", i + 1);
	}

	printf ("\n");

	return 0;
}

// @END_OF_SOURCE_CODE

Judge Response

USER: Shadow King [tausiq11]
TASK: holstein
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 6896 KB]
   Test 2: TEST OK [0.000 secs, 6896 KB]
   Test 3: TEST OK [0.000 secs, 6896 KB]
   Test 4: TEST OK [0.000 secs, 6896 KB]
   Test 5: TEST OK [0.000 secs, 6896 KB]
   Test 6: TEST OK [0.000 secs, 6896 KB]
   Test 7: TEST OK [0.000 secs, 6896 KB]
   Test 8: TEST OK [0.000 secs, 6896 KB]
   Test 9: TEST OK [0.011 secs, 6896 KB]
   Test 10: TEST OK [0.011 secs, 6896 KB]

All tests OK.
YOUR PROGRAM ('holstein') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.