Cod sursa(job #2345476)

Utilizator NicolaalexandraNicola Alexandra Mihaela Nicolaalexandra Data 16 februarie 2019 13:23:13
Problema Koba Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>

using namespace std;

ifstream fin ("koba.in");
ofstream fout ("koba.out");
long long n,t1,t2,t3,nr,d,i,a,b,c,x,y,poz,sol;
long long f[20000],v[10000];
int main (){

    fin>>n>>t1>>t2>>t3;
    if (n == 1){
        fout<<t1%10;
        return 0;
    }
    if (n == 2){
        fout<<t1%10+t2%10;
        return 0;
    }
    if (n == 3){
        fout<<t1%10+t2%10+t3%10;
        return 0;
    }
    a = t1%10, b = t2%10, c = t3%10;
    v[1] = a, v[2] = a+b, v[3] = a+b+c;

    for (i=4;i<=n;i++){
        d = (c + b*a) % 10;
        a = b, b = c, c = d;
        v[i] = v[i-1]+d;
        nr = a*100+b*10+c;
        if (f[nr])
            break;
        f[nr] = i;
    }
    if (i > n){
        fout<<v[n];
        return 0;
    }

    poz = f[a*100+b*10+c];
    x = (n-poz)/(i-poz);
    y = (n-poz)%(i-poz);
    sol = v[poz] + (v[i]-v[poz])*x + v[poz+y] - v[poz];
    fout<<sol;

    return 0;
}