Cod sursa(job #3378460)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 26 noiembrie 2026 14:21:30
Problema Pedefe Scor 30
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 2.34 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;
}

inline int f(int xx){ return (xx & (-xx)); }
struct fenwicktree2d{
    int tree[nmax + 2][nmax + 2];

    void update(int xx, int yy, int vall){
        for(int i = xx; i <= nmax; i += f(i)){
            for(int j = yy; j <= nmax; j += f(j)){
                tree[i][j] = addmod(tree[i][j], vall);
            }
        }
    }

    int query2d(int xx, int yy){
        int summ = 0;
        for(int i = xx; i >= 1; i -= f(i)){
            for(int j = yy; j >= 1; j -= f(j)){
                summ = addmod(summ, tree[i][j]);
            }
        }
        return summ;
    }
} aib[2];

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];

    subseq[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]);
                        }
                    }
                }

                if(it == 0 || (it == 1 && a[i] == subseq[it])){ 
                    dp[bit][i][j] = addmod(dp[bit][i][j], +1); 
                }
            }
        }
        
        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;
}