Cod sursa(job #3249105)

Utilizator prares06Papacioc Rares-Ioan prares06 Data 14 octombrie 2024 19:39:50
Problema Lista lui Andrei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include<bits/stdc++.h>
using namespace std;

ifstream fin("nrcuv.in");
ofstream fout("nrcuv.out");

const int MOD = 104659;
const int SIGMA = 26;
int n, m, s, adj[SIGMA], dp[1005][SIGMA]; //dp[i][j] = nr de cuvinte de lungime i cu ultima litera j care se pot forma
char a, b;

int main(){
    fin >> n >> m;

    for(;m--;){
        fin >> a >> b;
        a -= 'a';
        b -= 'a';
        adj[a] |= (1 << b);
        adj[b] |= (1 << a);
    }

    for(int i = 0; i < SIGMA; ++i)
        dp[1][i] = 1;

    for(int i = 2; i <= n; ++i)
        for(int j = 0; j < SIGMA; ++j)
            for(int bit = 0; bit < SIGMA; ++bit)
            if(!(adj[j] & (1 << bit))){
                dp[i][j] += dp[i - 1][bit];
                if(dp[i][j] >= MOD)
                    dp[i][j] -= MOD;
            }

    for(int i = 0; i < SIGMA; ++i)
        s += dp[n][i];

    fout << s % MOD;
}