Cod sursa(job #791604)

Utilizator ana.zetg wea tw ana.z Data 24 septembrie 2012 17:44:40
Problema Koba Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include<stdio.h>
using namespace std;
int t1,t2,t3,c,s,i,n,f,f2,nr,v[10005],poz[10005],sum[10005];
int main()
{
	freopen("koba.in","r",stdin);
	freopen("koba.out","w",stdout);
	scanf("%d%d%d%d",&n,&t1,&t2,&t3);
	t1%=10;
	t2%=10;
	t3%=10;
	s=t1%10+t2%10+t3%10;
	v[t1*100+t2*10+t3]=1;
	sum[t1*100+t2*10+t3]=s;
	poz[t1*100+t2*10+t3]=3;
	for(i=4;i<=n;i++)
	{
		c=t3;
		t3=t3+t1*t2;
		s+=t3%10;
		t3%=10;
		t1=t2;
		t2=c;
		if(v[t1*100+t2*10+t3]>=1)
		{
			f=s-sum[t1*100+t2*10+t3];
			f2=i-poz[t1*100+t2*10+t3];
			nr=(n-i)/f2;
			s+=nr*f;
			i+=nr*f2;
		}
		else
			{
			v[t1*100+t2*10+t3]++;
			sum[t1*100+t2*10+t3]=s;
			poz[t1*100+t2*10+t3]=i;
			}
	}
	printf("%d\n",s);
	return 0;
}