Cod sursa(job #2490717)

Utilizator CyborgSquirrelJardan Andrei CyborgSquirrel Data 10 noiembrie 2019 19:22:42
Problema Pod Scor 65
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

const int modit = 9901;
vector<int> scand;

struct ma{
	int w, h;
	int va[21][21];
	ma(int w, int h) : w(w), h(h){
		for(int x = 0; x < w; x++){
			for(int y = 0; y < h; y++){
				va[x][y] = 0;
			}
		}
	}
	ma(int s) : ma(s, s){}
	ma mul(const ma & rhs){
		ma r(rhs.w, h);
		for(int x = 0; x < r.w; x++){
			for(int y = 0; y < r.h; y++){
				for(int k = 0; k < w; k++){
					r.va[x][y] += va[k][y]*rhs.va[x][k];
					r.va[x][y] %= modit;
				}
			}
		}
		return r;
	}
	void unit(){
		for(int i = 0; i < w; i++){
			va[i][i] = 1;
		}
	}
	void pune(){
		for(int i = 1; i < w; i++){
			va[i][i-1] = 1;
		}
		va[0][0] = va[0][h-1] = 1;
	}
	void nupune(){
		for(int i = 1; i < w; i++){
			va[i][i-1] = 1;
		}
	}
	ma raze(int p){
		ma r(w,h);r.unit();
		ma t = *this;
		for(int i = p; i > 0; i >>= 1){
			if(i&1){
				r = r.mul(t);
			}
			t = t.mul(t);
		}
		return r;
	}
	void deb(){
		for(int y = 0; y < h; y++){
			for(int x = 0; x < w; x++){
				cout << va[x][y] << " ";
			}
			cout << "\n";
		}
		cout << "\n";
	}
};

int main(){
	int n, m, k;
	fin >> n >> m >> k;
	scand.push_back(0);
	for(int i = 0; i < m; i++){
		int a;fin >> a;
		scand.push_back(a);
	}
	sort(scand.begin(), scand.end());
	
	ma mp(k), mnp(k);
	mp.pune();mnp.nupune();
	
	ma a(k, 1);a.va[0][0] = 1;
	for(int i = 1; i <= m; i++){
		int del = scand[i]-scand[i-1]-1;
		a = a.mul(mp.raze(del));
		a = a.mul(mnp);
	}
	a = a.mul(mp.raze(n-scand.back()));
	fout << a.va[0][0];
}