Cod sursa(job #230459)

Utilizator vlad_DVlad Dumitriu vlad_D Data 13 decembrie 2008 23:37:19
Problema 12-Perm Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.54 kb
#include <fstream>
#include <cmath>

using namespace std;

unsigned long total = 0;
int main() {
	// 1, 2, 6, 12
	ifstream fin("12perm.in");
	ofstream fout("12perm.out");
	int n;

	fin >> n;
	if (n <= 4) {
		if (n <= 2) fout << n << '\n';
	        else fout << n * (n-1) << '\n';	
		return 0;
	}
	unsigned long a_1, a_3, a, a_2;
	a_1 = 12; a_3 = 2; a_2 = 6;
	for (unsigned long i = 5; i <= n; ++i) {
		a = a_1 + a_3 + 2*(i-2);
		a_3 = a_2; a_2=a_1; a_1=a;
	}
	unsigned long M = 1048576;
	fout << a % M << '\n';
	return 0;
}