Cod sursa(job #3377765)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 24 noiembrie 2026 11:27:25
Problema Pedefe Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <fstream>

#include <numeric>

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[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;
    for(int it = 0, bit = 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 state ///
                
                for(int xx = 0; xx < i; xx++){
                    for(int yy = 0; yy < j; yy++){
                        if(a[xx] != b[yy] || a[xx] > a[i]){ continue; }
                        
                        if(a[i] == subseq[it]){
                            dp[bit][i][j] = addmod(dp[bit][i][j], dp[bit ^ 1][xx][yy]);
                        }else{
                            dp[bit][i][j] = addmod(dp[bit][i][j], dp[bit][xx][yy]);
                        }
                    }
                }
            }
        }
        
        bit ^= 1; /// clear next matrix ///
        for(int i = 0; i <= n; i++){
            fill(dp[bit][i], dp[bit][i] + 1 + m, 0);
        }
    }

    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 & 1][i][j]);
        }
    }
    out<<all_ways<<"\n";

    return 0;
}