Cod sursa(job #1847968)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 15 ianuarie 2017 12:03:37
Problema Koba Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <stdio.h>

using namespace std;

int n, t1, t2, t3;

struct stare
{
    int t1, t2, t3;
    stare(int _t1, int _t2, int _t3)
    {
        t1 = _t1 % 10;
        t2 = _t2 % 10;
        t3 = _t3 % 10;
    }
    bool operator==(const stare &e)
    {
        return t1 == e.t1 && t2 == e.t2 && t3 == e.t3;
    }
};

void shift(stare &e)
{
    int nou = (e.t3 + e.t2 * e.t1) % 10;
    e.t1 = e.t2;
    e.t2 = e.t3;
    e.t3 = nou;
}


int main()
{
    freopen("koba.in", "r", stdin);
    freopen("koba.out", "w", stdout);

    scanf("%d %d %d %d", &n, &t1, &t2, &t3);
    stare init(t1, t2, t3), p1 = init, p2 = init;
    shift(p2);
    int dif = 1, sum = t3%10;
    while (!(p1 == p2)) {
        dif++;
        sum -= p1.t3;
        shift(p1);
        sum += p2.t3;
        shift(p2), sum += p2.t3;
        shift(p2);
    }
    if (n == 1) printf("%d", init.t1);
    else if (n == 2) printf("%d", init.t1 + init.t2);
    else if (n == 3) printf("%d", init.t1 + init.t2 + init.t3);
    else {
        p1 = init;
        int block = 2, bs = p1.t1 + p1.t2;
        while (!(p1 == p2)) {
            bs += p1.t3;
            shift(p1);
            block++;
            if (block == n) {
                printf("%d\n", bs);
                return 0;
            }
        }
        n -= block;
        bs += (sum * (n / dif));
        n %= dif;
        for (int i = 1; i <= n; i++) {
            bs += p1.t3;
            shift(p1);
        }
        printf("%d\n", bs);
    }
    return 0;
}