Cod sursa(job #3300043)

Utilizator Cristian_NegoitaCristian Negoita Cristian_Negoita Data 12 iunie 2025 14:41:17
Problema Pod Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.6 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("pod.in");
ofstream fout("pod.out");
const int KMAX = 21, MOD = 9901;
int n, m, k;
bool vis[KMAX];
vector<int> missing;

struct Matrix
{
    int n, m;
    vector<vector<int>> mat;
    Matrix()
    {
        n = 0, m = 0;
        mat = vector<vector<int>>(0, vector<int>(0, 0));
    }
    Matrix(int lin, int col)
    {
        n = lin, m = col;
        mat = vector<vector<int>>(n, vector<int>(m, 0));
    }
    Matrix &operator = (const Matrix &other)
    {
        n = other.n;
        m = other.m;
        mat = other.mat;
        return *this;
    }
    static Matrix identity(int dim)
    {
        Matrix I(dim, dim);
        for(int i = 0; i < dim; i++)
            I.mat[i][i] = 1;
        return I;
    }
    Matrix multiply(const Matrix &other) const
    {
        Matrix ans(n, other.m);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < other.m; j++)
                for(int k = 0; k < m; k++)
                    ans.mat[i][j] = (ans.mat[i][j] + mat[i][k] * other.mat[k][j]) % MOD;
        return ans;
    }
    Matrix power(int exp) const
    {
        Matrix ans = Matrix::identity(n), base = *this;
        while(exp != 0)
        {
            if(exp % 2 == 1)
                ans = ans.multiply(base);
            base = base.multiply(base);
            exp /= 2;
        }
        return ans;
    }
};

bool citire()
{
    fin >> n >> m >> k;
    for(int i = 0; i < m; i++)
    {
        int poz;
        fin >> poz;
        if(poz == n)
            return true;
        if(poz <= k)
            vis[poz] = true;
        else
            missing.push_back(poz);
    }
    return false;
}

int main()
{
    if(citire())
        fout << 0;
    else
    {
        sort(missing.begin(), missing.end());
        Matrix dp(k, k), suport(1, k);
        for(int i = 0; i < k - 1; i++)
            dp.mat[i + 1][i] = 1;
        dp.mat[0][k - 1] = 1;
        dp.mat[k - 1][k - 1] = 1;
        suport.mat[0][0] = (!vis[1]);
        for(int i = 1; i < k; i++)
            if(!vis[i + 1])
                suport.mat[0][i] += suport.mat[0][i - 1];
        suport.mat[0][k - 1] += (!vis[k]);
        missing.push_back(n);

        int last = k;
        for(int poz : missing)
        {
            int dist = poz - last;
            last = poz;
            suport = suport.multiply(dp.power(dist));
            if(poz != n)
                suport.mat[0][k - 1] = 0;
        }
        fout << suport.mat[0][k - 1];
    }

    fin.close();
    fout.close();
    return 0;
}