Cod sursa(job #877114)

Utilizator AndreiRSStatescu Andrei Rares AndreiRS Data 12 februarie 2013 16:12:59
Problema Koba Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>
using namespace std;

ifstream fi ("koba.in");
ofstream fo ("koba.out");

const int dim = 10005;
int N, T1, T2, T3, S[dim], viz[10][10][10];

int main ()
{
    fi >> N >> T1 >> T2 >> T3;

    S[1] = (T1 %= 10);
    S[2] = S[1] + (T2 %= 10);
    S[3] = S[2] + (T3 %= 10);

    for (int i = 3, aux; i <= N; i++)
    {
        if (viz[T1][T2][T3])
        {
            int pozc = viz[T1][T2][T3];
            int sumac = S[i] - S[pozc];
            int lungc = i - pozc;

            int s1 = ((N - pozc) / lungc) * sumac;
            int s2 = S[pozc + ((N - pozc) % lungc)];

            S[0] = s1 + s2;

            break;
        }

        viz[T1][T2][T3] = i;

        S[i + 1] = S[i] + (aux = (T3 + T2 * T1) % 10);
        T1 = T2;
        T2 = T3;
        T3 = aux;
    }

    if (S[0] == 0)
        S[0] = S[N];
    fo << S[0] << '\n';

    return 0;
}