Cod sursa(job #2781372)

Utilizator CristiBota3Cristian Bota Avram CristiBota3 Data 9 octombrie 2021 12:46:27
Problema Cifra Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <fstream>
#include <string>
using namespace std;
int v[101];

int ucif(int x)
{
    if(v[x]!=-1)
    {
        return v[x];
    }
    if(x==1)
    {
        v[1]=1;
        return 1;
    }
    int u1,u2,u3,u4;
    u1=x%10;
    u2=x*x%10;
    u3=x*x*x%10;
    u4=x*x*x*x%10;
    if(x%4==0)
    {
        v[x]=(u4+ucif(x-1))%10;
    }
    else if(x%4==3)
    {
        v[x]=(u3+ucif(x-1))%10;
    }
    else if(x%4==2)
    {
        v[x]=(u2+ucif(x-1))%10;
    }
    else
    {
        v[x]=(u1+ucif(x-1))%10;
    }
    return v[x];
}

int main()
{
    ifstream fin ("cifra.in");
    ofstream fout ("cifra.out");
    int t;
    string x;
    fin>>t;
    for(int i=0;i<=100;i++)
    {
        v[i]=-1;
    }
    ucif(100);
    for(int i=0;i<t;i++)
    {
            fin>>x;
            int y;
            if(x.length()>1)
            {
              y=stoi(x.substr(x.length()-2));
            }
            else
            {
                y=stoi(x);
            }
            fout<<v[y]<<endl;

    }

    fin.close();
    fout.close();
    return 0;
}