Cod sursa(job #1253567)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 1 noiembrie 2014 14:40:38
Problema Koba Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <fstream>
#include <algorithm>
using namespace std;

ifstream fin("koba.in");
ofstream fout("koba.out");

int n, m, i, j, k, ok, x1, x2, x3;
int j1, j2, j3, sum, jc, nr;

int main(){
    fin >> n >> x1 >> x2 >> x3;
    x1 %= 10;
    x2 %= 10;
    x3 %= 10;
    if(n == 1){
        fout << x1;
        return 0;
    }
    if(n == 2){
        fout << (x1 + x2) % 10;
        return 0;
    }
    if(n == 3){
        fout << (x1 + x2 + x3) % 10;
        return 0;
    }//
    j1 = x2;
    j2 = x3;
    j3 = (x3 + x2 * x1) % 10;
    sum = x1 + x2 + x3 + j3;
    i = 5;
    while(i <= n && (j1 != x1 || j2 != x2 || j3 != x3)){
        jc = (j3 + j2 * j1) % 10;
        j1 = j2;
        j2 = j3;
        j3 = jc;
        sum += jc;
        nr ++;
        i ++;
    }
    if(i > n)//cand a atins n;
        fout << sum;
    else{
        i -= 4;
        sum *= n / i;
        n %= i;
        i = 4;
        if(n == 0){
            fout << sum;
            return 0;
        }
        if(n == 1){
            fout << sum + x1 % 10;
            return 0;
        }
        if(n == 2){
            fout << sum + (x1 + x2) % 10;
            return 0;
        }
        if(n == 3){
            fout << sum + (x1 + x2 + x3) % 10;
            return 0;
        }
        j1 = x2;
        j2 = x3;
        j3 = (x3 + x2 * x1) % 10;
        sum = sum + x1 + x2 + x3 + j3 % 10;
        i = 5;
        while(i <= n && (j1 != x1 || j2 != x2 || j3 != x3)){
            jc = (j3 + j2 * j1) % 10;
            j1 = j2;
            j2 = j3;
            j3 = jc;
            sum += jc;
            nr ++;
            i ++;
        }
        fout << sum;
    }
    return 0;
}