Cod sursa(job #386078)

Utilizator indestructiblecont de teste indestructible Data 23 ianuarie 2010 23:13:44
Problema Koba Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <stdio.h>
#define NMAX 10001
int n,v[NMAX],A[NMAX],sum[NMAX],rez;
char marc[NMAX];
int main()
{
	freopen("koba.in","r",stdin);
	freopen("koba.out","w",stdout);
	scanf("%d%d%d%d",&n,&v[1],&v[2],&v[3]);
	v[1]=v[1]%10; v[2]=v[2]%10; v[3]=v[3]%10;
	sum[1]=v[1]; sum[2]=sum[1]+v[2]; sum[3]=sum[2]+v[3];
	marc[v[1]*100+v[2]*10+v[3]]=1;
	int i,poz=4,nr,act;
	if (n<=3)
	{
		printf("%d\n",sum[n]);
		return 0;
	}
	A[v[1]*100+v[2]*10+v[3]]=1;
	while (poz<=n)
	{
		v[poz]=v[poz-1]+v[poz-2]*v[poz-3];
		v[poz]=v[poz]%10;
		sum[poz]=sum[poz-1]+v[poz];
		nr=v[poz-2]*100+v[poz-1]*10+v[poz];
		if (poz==n)
		{
			printf("%d\n",sum[n]);
			return 0;
		}
		if (!marc[nr])
			marc[nr]=1;
		else
		{
			act=A[nr];
			break;
		}
		A[nr]=poz-2;
		poz++;
	}
	rez+=sum[act-1];
	rez+=(sum[poz-3]-sum[act-1])*((n-act)/(poz-act-2));
	rez+=sum[n-act-(n-act)/(poz-act-2)*(poz-act-2)+act]-sum[act-1];
	printf("%d\n",rez);
	return 0;
}