Cod sursa(job #333804)

Utilizator iulia609fara nume iulia609 Data 23 iulie 2009 22:19:09
Problema Koba Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include<stdio.h>
#define dim 100000000
using namespace std;

int a[dim];

int main()
{ 
  long long n,i,s,j,poz,t1,t2,t3,t4;

	FILE *f = fopen("koba.in", "r");
	FILE *g = fopen("koba.out", "w");
	
	n = t1 = t2 = t3 = 0;
	fscanf(f, "%lld", &n);
	fscanf(f, "%d%d%d",&t1,&t2, &t3);
	
	t1 = t1%10;
	t2 = t2%10;
	t3 = t3%10;
	s = t1+t2+t3;
	//s = 0;
	a[1] = t1*100+t2*10+t3;
	for(i = 2; i < n; i++)
		{
			t4 = (t3+(t2*t1)%10)%10;
			a[i] = (a[i-1]%100)*10 + t4;
			s += t4;
			t1 = t2;
			t2 = t3;
			t3 = t4;
			if(a[i] == a[1]) break;
		}
	s-=t4;
	
	if(i!=n)
	{
	 n -= i-1;
	s += s*((n-2)/i);
	 s += t1+t2+t3;
	 for(i = 1; i <= (n-2)%i; i++)
		{
			t4 = (t3+(t2*t1)%10)%10;
			s += t4;
			t1 = t2;
			t2 = t3;
			t3 = t4;
		}
	}
	fprintf(g, "%lld\n", s);
	
	fclose(f);
	fclose(g);
	return 0;
}