Cod sursa(job #955195)

Utilizator vld7Campeanu Vlad vld7 Data 31 mai 2013 02:28:55
Problema Lista lui Andrei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <fstream>

using namespace std;

ifstream f("nrcuv.in");
ofstream g("nrcuv.out");

const int MAX_N = 1005;
const int SIGMA = 26;
const int MOD = 104659;

int n, m, D[MAX_N][SIGMA + 5];
int possible[SIGMA + 5][SIGMA + 5];

void init() {
	for (int i = 0; i < SIGMA + 5; i++)
		for (int j = 0; j < SIGMA + 5; j++)
			possible[i][j] = 1;
}

void read() {
	f >> n >> m;
	for (int i = 1; i <= m; i++) {
		char c1, c2;
		f >> c1 >> c2;
		
		int a = c1 - 'a' + 1;
		int b = c2 - 'a' + 1;
		
		possible[a][b] = possible[b][a] = 0;
	}
}

void solve() {
	for (int i = 1; i <= SIGMA; i++)
		D[1][i] = 1;
	
	for (int i = 2; i <= n; i++)
		for (int j = 1; j <= SIGMA; j++)
			for (int k = 1; k <= SIGMA; k++)
				if (possible[j][k]) {
					D[i][j] = (D[i][j] + D[i - 1][k]) % MOD;
					if (D[i][j] < 0)
						D[i][j] += MOD;
				}
}

void write() {
	int rez = 0;
	
	for (int i = 1; i <= SIGMA; i++) {
		rez = (rez + D[n][i]) % MOD;
		if (rez < 0)
			rez += MOD;
	}
	
	g << rez << '\n';
}

int main() {
	init();
	read();
	solve();
	write();
	
	f.close();
	g.close();
	
	return 0;
}