Pagini recente » Cod sursa (job #1930827) | Cod sursa (job #2823070) | Cod sursa (job #111115) | Cod sursa (job #891685) | Cod sursa (job #960630)
Cod sursa(job #960630)
#include <cstring>
#include <fstream>
#include <algorithm>
using namespace std;
const int MOD = 666013;
int N, M, P;
int A[502], B[502], C[102];
int D[2][502][502];
int AIB[502], S[502];
void update(int pos, int val)
{
++pos;
for (; pos <= 501; pos += pos & -pos)
{
AIB[pos] += val;
if (AIB[pos] >= MOD) AIB[pos] -= MOD;
}
}
int query(int pos)
{
++pos;
int sum = 0;
for (; pos >= 1; pos -= pos & -pos)
{
sum += AIB[pos];
if (sum >= MOD) sum -= MOD;
}
return sum;
}
int main()
{
ifstream fin("pedefe.in");
ofstream fout("pedefe.out");
fin >> N >> M >> P;
for (int i = 1; i <= N; ++i)
fin >> A[i];
for (int i = 1; i <= M; ++i)
fin >> B[i];
for (int i = 1; i <= P; ++i)
fin >> C[i];
int act = 0;
D[act][0][0] = 1;
for (int k = 0; k <= P; ++k)
{
if (k != 0) act = !act;
memset(S, 0, sizeof(S));
for (int i = 1; i <= N; ++i)
{
memset(AIB, 0, sizeof(AIB));
if (k == 0) update(0, 1);
for (int j = 1; j <= M; ++j)
{
D[!act][i][j] = 0;
if (A[i] == B[j])
{
int resultnow = query(B[j]);
if (k < P && A[i] == C[k + 1])
D[!act][i][j] += resultnow;
else
D[act][i][j] += resultnow;
}
else
D[act][i][j] = 0;
update(B[j], S[j]);
S[j] += D[act][i][j];
if (S[j] >= MOD) S[j] -= MOD;
}
}
}
int result = 0;
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= M; ++j)
{
result += D[act][i][j];
if (result >= MOD) result -= MOD;
}
fout << result << '\n';
fin.close();
fout.close();
}