Cod sursa(job #152266)

Utilizator MariusMarius Stroe Marius Data 9 martie 2008 12:04:23
Problema Pedefe Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <fstream>
#include <algorithm>

using namespace std;

const char iname[] = "pedefe.in";
const char oname[] = "pedefe.out";

#define FOR(i, a, b) for (int i = (a); i <= (b); ++ i)
#define MAXN  505
#define MAXV  505
#define modulo 666013

int A[MAXN], B[MAXN], C[MAXN];

int Cnt[2][MAXN][MAXN], CntV[2][MAXV];

int main(void)
{
	ifstream fin(iname);

	int n, m, p;

	fin >> n >> m >> p;
	FOR (i, 1, n) 
		fin >> A[i];
	FOR (i, 1, m) 
		fin >> B[i];
	FOR (i, 1, p) 
		fin >> C[i];

	fin.close();

	int s = 0;

	Cnt[s][0][0] = CntV[s][0] = 1;

	FOR (i, 1, n) 
	{
		FOR (j, 1, m) if (A[i] == B[j]) 
			FOR (p, 0, j-1) if (B[p] <= B[j])
			{
				int &temp = Cnt[s][i][j];
				if ((temp += CntV[s][p]) >= modulo)
					temp -= modulo;
			}

		FOR (j, 1, m) if (A[i] == B[j]) 
		{
			int &temp = CntV[s][j];
			if ((temp += Cnt[s][i][j]) >= modulo)
				temp -= modulo;
		}
	}

	FOR (k, 1, p) 
	{
		s = k & 1;
		memset(Cnt[s], 0, sizeof(Cnt[s]));
		memset(CntV[s], 0, sizeof(CntV[s]));
		
		FOR (i, 1, n)
		{
			FOR (j, 1, m) if (A[i] == B[j]) 
			{
				if (A[i] == C[k]) 
				{
					FOR (p, 0, i-1) FOR (q, 0, j-1)
						if (A[p] == B[q] && A[p] <= A[i])
						{
							int &temp = Cnt[s][i][j];
							if ((temp += Cnt[s ^ 1][p][q]) >= modulo)
								temp -= modulo;
						}
				} else {
					FOR (p, 0, j-1) if (B[p] <= B[j])
					{
						int &temp = Cnt[s][i][j];
						if ((temp += CntV[s][p]) >= modulo)
							temp -= modulo;
					}
				}
			}
			FOR (j, 1, m) if (A[i] == B[j])
			{
				int &temp = CntV[s][j];
				if ((temp += Cnt[s][i][j]) >= modulo)
					temp -= modulo;
			}
		}
	}

	int ret = 0;

	FOR (i, 1, n) FOR (j, 1, m) 
		if (A[i] == B[j] && A[i] == C[p]) 
			if ((ret += Cnt[p & 1][i][j]) >= modulo)
				ret -= modulo;

	ofstream fout(oname);

	fout << ret << '\n';

	fout.close();

	return 0;
}