Pagini recente » Cod sursa (job #17297) | Cod sursa (job #2102338) | Cod sursa (job #848700) | Cod sursa (job #319628) | Cod sursa (job #160359)
Cod sursa(job #160359)
#include <cstdio>
#include <cstdlib>
#include <cstring>
int mat[1000];
int sume[1000];
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;
long long suma = t1 + t2 + t3;
bool gasit = false;
sume[0] = 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;
sume[i-2] = suma;
if ( !gasit )
{
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;
}