Cod sursa(job #165593)

Utilizator gabitzish1Gabriel Bitis gabitzish1 Data 26 martie 2008 12:56:39
Problema Koba Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <stdio.h>

int viz[1000], n, t1, t2, t3, t4, s, cif, nr, suma;

int main()
{
	freopen("koba.in","r",stdin);
	freopen("koba.out","w",stdout);
	int i;
	scanf("%d %d %d %d",&n,&t1,&t2,&t3);

	cif = ((t1%10) * 100) + ((t2%10) * 10) + (t3%10);
	s = (t1%10)+(t2%10)+(t3%10);
	t4 = t3 + (t1 % 10) * (t2 % 10);
	nr = 3;

	while (!viz[cif] && nr < n)
	{
		viz[cif] = 1;
		s += t4 % 10; nr++;
		t1 = t2; t2 = t3; t3 = t4;
		t4 = (t3 + (t1 % 10) * (t2 % 10)) % 10;
		cif = ((t1%10) * 100) + ((t2%10) * 10) + (t3%10);
	}

	for (i = 1; (i * nr) <= n; i++) suma += s;

	while (n >= nr) n -= nr;
	
	while (n--)
	{
		suma += t4 % 10;
		t4 = (t3 + (t1 % 10) * (t2 % 10)) % 10;
		t1 = t2; t2 = t3; t3 = t4;
	}
	printf("%d\n",suma);
	return 0;
}