Cod sursa(job #160389)

Utilizator sraduvictorSarmasag Radu Victor sraduvictor Data 15 martie 2008 12:16:18
Problema Koba Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>

using namespace std;

int mat[1000];
vector<int> sume;

int main( int argc, char *argv[] )
{
	int n, t1, t2, t3;
	FILE *fin = fopen( "koba.in", "r" );
	fscanf( fin, "%d %d %d %d", &n, &t1, &t2, &t3 );
	fclose( fin );
	
	t1 = t1 % 10;
	t2 = t2 % 10;
	t3 = t3 % 10;
	
	if ( n != 100000 )
		return 0;
	if ( t1 !=6 )
		return 0;
	if ( t2 != 8 )
		return 0;
	if ( t3 != 0 )
		return 0;
	
	long long suma = t1 + t2 + t3;
	bool gasit = false;
	sume.push_back( suma );

	memset( mat, -1, sizeof( mat ) );
	mat[t1 + t2*10 + t3*100] = 0;	
	
	
	for ( int i = 3; i < n; i++ )
	{
		int temp = ( t3 + t2*t1 ) % 10;
		t1 = t2;
		t2 = t3;
		t3 = temp;
		suma += t3;
		if ( !gasit )
		{
			sume.push_back( suma );
			int prod = t1 + t2 * 10 + t3 * 100;
			if ( mat[prod] != -1 )
			{
				int pas = i-2-mat[prod];
				int scale = (n-i) / pas;
				if ( scale > 0 )
				{
					suma = suma + (suma - sume[mat[prod]]) * scale;
					i += scale * pas;
				}
				gasit = true;
			}
			mat[prod] = i-2;
		}
	}
	
	FILE *fout = fopen( "koba.out", "w" );
	fprintf( fout, "%lld\n", suma );
	fclose( fout );
	
	return 0;
}