Cod sursa(job #3377459)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 23 noiembrie 2026 13:23:37
Problema Pedefe Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <fstream>

using namespace std;

ifstream in("pedefe.in");
ofstream out("pedefe.out");

const int nmax = 500, szmax = 100, mod = 666013;
int n, m, sz, a[nmax + 2], b[nmax + 2], subseq[szmax + 2];

int dp[szmax + 2][nmax + 2][nmax + 2];

int addmod(int xx, int yy){
    xx += yy;
    if(xx >= mod){
        xx -= mod;
    }
    return xx;
}

int main(){

    in>>n>>m>>sz;
    for(int i = 1; i <= n; i++) in>>a[i];
    for(int i = 1; i <= m; i++) in>>b[i];
    for(int i = 1; i <= sz; i++) in>>subseq[i];

    dp[0][0][0] = 1; /// init ///

    for(int it = 0; it <= sz; it++){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(a[i] != b[j]){ continue; } /// invalid ///
                
                for(int xx = 0; xx < i; xx++){
                    for(int yy = 0; yy < j; yy++){
                        if(a[xx] != b[yy] || a[xx] > a[i]){ continue; }
                        
                        dp[it][i][j] = addmod(dp[it][i][j], dp[it][xx][yy]);

                        if(it && a[i] == subseq[it]){
                            dp[it][i][j] = addmod(dp[it][i][j], dp[it - 1][xx][yy]);
                        }
                    }
                }
            }
        }
    }

    int all_ways = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            all_ways = addmod(all_ways, dp[sz][i][j]);
        }
    }
    out<<all_ways<<"\n";

    return 0;
}