Cod sursa(job #137821)

Utilizator savimSerban Andrei Stan savim Data 17 februarie 2008 15:02:56
Problema Koba Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <stdio.h>
int k,ok,i,j,p1,p2,n,t1,t2,t3,x;
int a[2010];
long long s=0,sum=0;
int main()
{
	freopen("koba.in","r",stdin);
	freopen("koba.out","w",stdout);

	scanf("%d%d%d%d",&n,&t1,&t2,&t3);

	a[1]=t1%10;a[2]=t2%10;a[3]=t3%10;
	t1%=10;t2%=10;t3%=10;

	for (i=4; i<=2000; i++)
	{
		x=t3;
		t3=(t3+t2*t1)%10;
		t1=t2;t2=x;
		a[i]=t3;
	}
	ok=0;
	for (i=1; i<=2000-5; i++)
	{
		for (j=i+3; j<=200-2; j++)
		if (a[i]==a[j] && a[i+1]==a[j+1] && a[i+2]==a[j+2])
		{
			p1=i;
			p2=j-1;
			ok=1;
			break;
		}
		if (ok) break;
	}
	sum=0;
	if (p1-1>n)
	{
		for (i=1; i<=n; i++)
			sum+=a[i];
	}
	else
	{
		for (i=1; i<=p1-1; i++)
			sum+=a[i];

		k=(n-(p1-1))%(p2-p1+1);

		for (i=p1; i<=p1+k-1; i++)
			sum+=a[i];

		k=(n-(p1-1))/(p2-p1+1);

		s=0;
		for (i=p1; i<=p2; i++)
			s+=a[i];

		sum+=s*k;
	}
	printf("%lld\n",sum);

	return 0;
}