Cod sursa(job #1066719)

Utilizator andreiiiiPopa Andrei andreiiii Data 25 decembrie 2013 14:55:53
Problema Koba Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>

using namespace std;

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

int v[1005];

int main()
{
    int n, i, t1, t2, t3, ok=0, aux, st;
    long long s=0, s1=0;
    fin>>n>>t1>>t2>>t3;
    s+=(t1%=10);
    s+=(t2%=10);
    s+=(t3%=10);
    v[t1*100+t2*10+t3]=3;
    for(i=4;i<=n;i++)
    {
        aux=(t3+t1*t2)%10;
        t1=t2;
        t2=t3;
        t3=aux;
        s+=t3;
        if(v[t1*100+t2*10+t3])
        {
            ok=1;
            break;
        }
        v[t1*100+t2*10+t3]=i;
    }
    if(!ok) fout<<s;
    else
    {
        st=v[t1*100+t2*10+t3]+1;
        n-=i;
        for(int j=st;j<=i;j++)
        {
            aux=(t3+t1*t2)%10;
            t1=t2;
            t2=t3;
            t3=aux;
            s1+=t3;
            if(j-st+1<=n%(i-st+1)) s+=t3;
        }
        s+=(n/(i-st+1))*s1;
        fout<<s;
    }
}