Cod sursa(job #2487043)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 3 noiembrie 2019 20:13:31
Problema Pod Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <bits/stdc++.h>
	
using namespace std;
	
ifstream fin("pod.in");
ofstream fout("pod.out");
	
const int mod = 9901;
const int DIM = 100;
	
bitset <DIM> vis;
	
struct Matrix
{
	int n, m;
	vector <vector <long long> > v;
	
	Matrix(int _n, int _m)
	{
		n = _n;
		m = _m;
	
		v.resize(n, vector <long long> (m, 0LL));
	}
	
	Matrix multiply(Matrix aux)
	{
		Matrix res(n, aux.m);
	
		for(int i = 0; i < n; i++)
			for(int t = 0; t < m; t++)
				for(int j = 0; j < aux.m; j++)
					res.v[i][j] = res.v[i][j] + v[i][t] * aux.v[t][j];
	
		for(int i = 0; i < n; i++)
			for(int j = 0; j < aux.m; j++)
				res.v[i][j] %= mod;
	
		return res;
	}
};
	
Matrix logpow(Matrix x, int n)
{
	Matrix res(x.n, x.n);
	
	for(int i = 0; i < x.n; i++)
		res.v[i][i] = 1;
	
	while(n)
	{
		if(n & 1)
		{
			res = res.multiply(x);
		}
	
		n >>= 1;
	
		x = x.multiply(x);
	}
	
	return res;
}
	
void make_matrix(int k, Matrix &magic)
{
	for(int i = 0; i < k - 1; i++)
		magic.v[i + 1][i] = 1;
	
	magic.v[k - 1][k - 1] = 1;
	magic.v[0][k - 1] = 1;
}
	
main()
{
	int n, m, k;
	fin >> n >> m >> k;
	
	Matrix vals(1, k);
	
	vector <int> t;
	
	for(int i = 1; i <= m; i++)
	{
		int x;
		fin >> x;
	
		if(x <= k)
			vis[x] = true;
		else
			t.push_back(x);
	}
	
	sort(t.begin(), t.end());
	
	if(vis[1] == false)
		vals.v[0][0] = 1;
	
	for(int i = 1; i < k; i++)
		if(vis[i + 1] == false)
			vals.v[0][i] += vals.v[0][i - 1];
	
	if(vis[k] == false)
		vals.v[0][k - 1]++;
	
	Matrix magic(k, k);	
	
	make_matrix(k, magic);
	
	int r = k;
	
	t.push_back(n);
	
	for(auto i : t)
	{
		int dif = i - r;
		r = i;
	
		vals = vals.multiply(logpow(magic, dif));
	
		if(i != n)
			vals.v[0][k - 1] = 0;
	}
	
	fout << vals.v[0][k - 1];
}