Cod sursa(job #2240390)

Utilizator theodor.moroianuTheodor Moroianu theodor.moroianu Data 13 septembrie 2018 10:45:26
Problema Pod Scor 100
Compilator cpp Status done
Runda simulare_prega Marime 1.42 kb
#include <bits/stdc++.h>
#define mod 9901
using namespace std;

int DIM;

struct Mat {
	int m[20][20];
	int x, y;
	Mat() {
		x = y = DIM;
		memset(m, 0, sizeof m);
	}
};

Mat mult(Mat & a, Mat & b)
{
	int MOD = mod * mod;
	Mat ans;
	for (int i = 0; i < a.x; i++)
		for (int k = 0; k < a.y; k++)
			for (int j = 0; j < b.y; j++)
				(ans.m[i][j] += a.m[i][k] * b.m[k][j]) >= MOD ? ans.m[i][j] -= mod : 0;
	for (int i = 0; i < a.x; i++)
		for (int j = 0; j < b.y; j++)
			ans.m[i][j] %= mod;
	return ans;
}

Mat put(Mat x, int p)
{
	Mat ans;
	for (int i = 0; i < DIM; i++)
		ans.m[i][i] = 1;
	while (p) {
		if (p & 1)
			ans = mult(x, ans);
		p >>= 1;
		if (p)
			x = mult(x, x);
	}
	return ans;
}

int main()
{
	ifstream in("pod.in");
	ofstream out("pod.out");

	int n, m;
	in >> n >> m >> DIM;

	Mat mult_by;
	for (int i = 0; i < DIM - 1; i++)
		mult_by.m[i + 1][i] = 1;
	mult_by.m[0][DIM - 1] = mult_by.m[DIM - 1][DIM - 1] = 1;

	vector <int> lipsa(m);

	for (auto & i : lipsa)
		in >> i;

	sort(lipsa.begin(), lipsa.end());

	Mat dp;
	dp.x = 1;
	dp.m[0][DIM - 1] = 1;

	int last = 0;
	for (int i = 0; i < lipsa.size(); i++) {
		int pas = lipsa[i] - last;
		Mat q = put(mult_by, pas);
		dp = mult(dp, q);
		dp.m[0][DIM - 1] = 0;
		last = lipsa[i];
	}

	int pas = n - last;
	Mat q = put(mult_by, pas);
	dp = mult(dp, q);

	out << dp.m[0][DIM - 1] << '\n';

	return 0;
}