Pagini recente » Cod sursa (job #171576) | Cod sursa (job #2485170) | Cod sursa (job #2367945) | Cod sursa (job #758830) | Cod sursa (job #160366)
Cod sursa(job #160366)
#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 );
if ( n > 100000 || n < 75000 )
return 0;
t1 = t1 % 10;
t2 = t2 % 10;
t3 = t3 % 10;
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;
}