• + 0 comments

    O(m^2 * r)

    long twoSubsequences(vector<int> X, int r, int s) {
        vector<vector<vector<long>>> cache(X.size()+1, vector<vector<long>>(X.size()+1, vector<long>(r+1)));
        for (int i=0; i <= X.size(); i++) cache[i][0][0] = 1;
        for (int i=1; i <= X.size(); i++) {
            for (int L=1; L <= i; L++) {
                for (int val=1; val <= r; val++) {
                    cache[i][L][val] = (val >= X[i-1]) ? (cache[i-1][L][val] + cache[i-1][L-1][val-X[i-1]]) % 1000000007 : cache[i-1][L][val];
                }
            }
        }
        long total = 0;
        for (int x=1; x < r; x++) {
            if (2*x == s+r) {
                for (int L=1; L <= X.size(); L++) total = (total + cache[X.size()][L][x] * cache[X.size()][L][r-x]) % 1000000007;
            }
        }
        return total;
    }