Cod sursa(job #304560)

Utilizator alex23alexandru andronache alex23 Data 13 aprilie 2009 21:47:42
Problema Koba Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <iostream>
#define NMAX 1050

FILE *f = fopen("koba.in", "r"), *g = fopen("koba.out", "w");

int *a, *b, i, s = 0, s1 = 0, s2 = 0, N;

using namespace std;

int main()
{
	a = new int[NMAX];
	b = new int[NMAX];
	for (int j = 1; j < NMAX; ++j)
		a[j] = b[j] = 0;
	fscanf(f, "%d %d %d %d", &N, &a[1], &a[2], &a[3]);
	fclose(f);
	a[1] = a[1] % 10;
	a[2] = a[2] % 10;
	a[3] = a[3] % 10;
	b[a[1] * 100 + a[2] * 10 + a[3]] = 3;
	i = 4;

	while (b[a[i - 3] * 100 + a[i - 2] * 10 + a[i - 1]] == (i - 1))
	{
		a[i] = (a[i - 1] + a[i - 2] * a[i - 3]) % 10;
		if (!b[a[i - 2] * 100 + a[i - 1] * 10 + a[i]])
		{
			b[a[i - 2] * 100 + a[i - 1] * 10 + a[i]] = i;
		}
		i++;
	}
	if (N <= i)
	{
		for (int j = 1; j <= N; ++j)
			s += a[j];
	}
	else
	{
		int y = i - 4, x = b[a[i - 3] * 100 + a[i - 2] * 10 + a[i - 1]] - 3; 
		for (int j = 1; j <= x; ++j)
			s1 += a[j];
		for (int j = x + 1; j <= y; ++j)
			s2 += a[j];
		s = s1 + ((N - x) / (y - x)) * s2;
		int l = ((N - x) % (y - x));
		for (int j = 1; j <= l; ++j)
			s = s + a[x + j];
	}
	fprintf(g, "%d", s);
	fclose(g);
	

	delete[] a;
	delete[] b;
	return 0;
}