Cod sursa(job #2399223)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 7 aprilie 2019 10:11:11
Problema Pod Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <bits/stdc++.h>
using namespace std;

const int DIM = 21;
const int MMAX = 1005;
const int MOD = 9901;

int pos[MMAX];
int mat1[DIM][DIM], mat2[DIM][DIM], ini[DIM][DIM];
int aux1[DIM][DIM], aux2[DIM][DIM], aux3[DIM][DIM], ans[DIM][DIM];

void multiply(int a[DIM][DIM], int b[DIM][DIM], int n) {
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			aux3[i][j] = 0;
			for (int k = 1; k <= n; ++k) {
				aux3[i][j] += a[i][k] * b[k][j]; } } }
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			a[i][j] = aux3[i][j] % MOD; } } }

void calculate(int x, int n) {
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			aux1[i][j] = ini[i][j];
			aux2[i][j] = mat1[i][j]; } } 
	for (; x; x >>= 1) {
		if (x & 1) {
			multiply(aux1, aux2, n); }
		multiply(aux2, aux2, n); } }	

int main(void) {
	freopen("pod.in", "r", stdin);
	freopen("pod.out", "w", stdout);
	int n, m, k; cin >> n >> m >> k;
	for (int i = 1; i < k; ++i) {
		mat1[i + 1][i] = mat2[i + 1][i] = 1; }
	mat1[1][k] = mat1[k][k] = 1; 
	for (int i = 1; i <= k; ++i) {
		ini[i][i] = 1; }
	for (int i = 1; i <= m; ++i) {
		cin >> pos[i]; }
	sort(pos + 1, pos + m + 1);
	ans[1][k] = 1;
	for (int i = 1; i <= m; ++i) {
		calculate(pos[i] - pos[i - 1] - 1, k);
		multiply(ans, aux1, k); multiply(ans, mat2, k); }
	calculate(n - pos[m], k); multiply(ans, aux1, k);
	cout << ans[1][k] << endl;
	return 0; }