Cod sursa(job #3378461)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 26 noiembrie 2026 14:30:14
Problema Pedefe Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.87 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 init(){
        for(int i = 1; i <= nmax; i++){
            fill(tree[i] + 1, tree[i] + 1 + nmax, 0);
        }
        return;
    }

    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 query(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++){
        aib[0].init(); aib[1].init(); 
        for(int i = 1; i <= n; i++){
            fill(dp[bit][i], dp[bit][i] + 1 + m, 0);
        }
        
        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]);
                //         }
                //     }
                // }

                dp[bit][i][j] = aib[bit ^ (a[i] == subseq[it])].query(a[i], j - 1);

                if(it == 0 || (it == 1 && a[i] == subseq[it])){ 
                    dp[bit][i][j] = addmod(dp[bit][i][j], +1); 
                }
            }

            for(int j = 1; j <= m; j++){
                if(a[i] != b[j]){ continue; }
                
                aib[bit].update(a[i], j, dp[bit][i][j]);
                aib[bit ^ 1].update(a[i], j, dp[bit ^ 1][i][j]);
            }
        }
        
        bit ^= 1; /// clear next matrix ///
    }

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