Cod sursa(job #729449)

Utilizator robertpoeRobert Poenaru robertpoe Data 29 martie 2012 16:39:49
Problema Koba Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.58 kb
#include<fstream>
#define dim 10001
#define AT x*100+y*10+z
using namespace std;
ifstream f("koba.in");
ofstream g("koba.out");
long long n,i,j,s;
int a[dim],arb[dim];
int x,y,z;
int main()
{
	f>>n>>x>>y>>z;
	x%=10;
	y%=10;
	z%=10;
	a[AT]=3;
	arb[1]=x;
	arb[2]=x+y;
	arb[3]=x+y+x;
	for(i=4;i<=n;i++)
	{
		j=(z+x*y)%10;
		x=y;
		y=z;
		z=j;
		arb[i]=arb[i-1]+z;
		if(a[AT])
			break;
		else
			a[AT]=i;
	}
	if(i>n)
		g<<arb[n];
	else
	{
		j=a[AT];
		s=arb[j];
		s+=(arb[i]-arb[j])*((n-j)/(i-j));
		s+=arb[j+(n-j)%(i-j)]-arb[j];
		g<<s;
	}
	g<<"\n";
	return 0;
}