Cod sursa(job #3261574)

Utilizator BledeaAlexBledea Alexandru BledeaAlex Data 6 decembrie 2024 20:07:38
Problema Lista lui Andrei Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;

const int N_MAX = 1001, M_MAX = 2001, NR_LIT = 26;
const long long MOD = 104659;
int N, M;
bool CanPlace[NR_LIT][NR_LIT]; /// If the two letters are allowed together
long long dp[NR_LIT][N_MAX]; /// dp[i][j] = The nr of possibilities to make a 'word' of len j ending with (i + 'a')

void SetInput(string name)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    (void)!freopen((name + ".in").c_str(), "r", stdin);
    (void)!freopen((name + ".out").c_str(), "w", stdout);
}

void ReadInput()
{
    /// Initialise C

    for(int i = 0, j; i < NR_LIT; i++)
        for(j = 0; j < NR_LIT; j++)
            CanPlace[i][j] = true;

    /// Read Input

    cin >> N >> M;

    char c1, c2;
    while(M--)
    {
        cin >> c1 >> c2;
        CanPlace[c1 - 'a'][c2 - 'a'] = false;
        CanPlace[c2 - 'a'][c1 - 'a'] = false;
    }
}

void Solve()
{
    for(int i = 0; i < NR_LIT; i++)
        dp[1][i] = 1;
    for(int i = 2; i <= N; i++)
        for(int j = 0; j < NR_LIT; j++)
            for(int k = 0; k < NR_LIT; k++)
                if(CanPlace[j][k])
                {
                    dp[i][j] = (dp[i][j] + dp[i-1][k]) % MOD;
                    //dp[i][k] = (dp[i][k] + dp[i-1][j]) % MOD;
                }

    long long ans = 0;
    for(int i = 0; i < NR_LIT; i++)
        ans = (ans + dp[N][i]) % MOD;

    cout << ans;
}

int main()
{
    SetInput("nrcuv");

    ReadInput();
    Solve();

    return 0;
}