Cod sursa(job #333885)

Utilizator iulia609fara nume iulia609 Data 24 iulie 2009 14:25:57
Problema Koba Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include<stdio.h>
#define dim 10000//100000001
#define di 1000

int t[dim], a[di];

int main()
{ int x;
  long long n, i, j, s,m,sum;
  
	FILE *f = fopen("koba.in", "r");
	FILE *g = fopen("koba.out", "w");
	
	n = t[1] = t[2] = t[3] = 0;
	fscanf(f, "%lld%d%d%d", &n,&t[1],&t[2],&t[3]);
	
	t[1] = t[1]%10;
	t[2] = t[2]%10;
	t[3] = t[3]%10;
	
	x = t[1]*100 + t[2]*10 + t[3];
	a[x] = 3;
	
	s = t[1]+t[2]+t[3];
	for(i = 4; i <= n; i++)
		{
			t[i] = (t[i-1] + (t[i-2]*t[i-3])%10)%10;
			s += t[i];
			x = t[i-2]*100 + t[i-1]*10 +t[i];
			if(a[x] != 0)
				{
					sum = 0;
					for(j = a[x]-2; j <= i-2; j++)
						sum += t[j];
					m = i - a[x]+1;
					n -= i+3; //n+=3;
					s += (n/m)*sum;
					
					for(j = a[x]-4; j <= a[x]+n%m-6; j++)
						s += t[j];
					break;
				}	
			  else a[x] = i;
		}
	
	fprintf(g, "%lld\n", s);
	
	fclose(f);
	fclose(g);
	return 0;
}