Cod sursa(job #2458398)

Utilizator bori2000Fazakas Borbala bori2000 Data 20 septembrie 2019 13:45:28
Problema Cifra Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

int utszj(int x)
{
    if(x%10==1) return 1;

    if(x%10==2 and x%4==2) return 4;
    if(x%10==2 and x%4==0) return 6;

    if(x%10==3 and x%4==1) return 3;
    if(x%10==3 and x%4==3) return 7;

    if(x%10==4) return 6;

    if(x%10==5) return 5;

    if(x%10==6) return 6;

    if(x%10==7 and x%4==1) return 7;
    if(x%10==7 and x%4==3) return 3;

    if(x%10==8 and x%4==2) return 4;
    if(x%10==8 and x%4==0) return 6;

    if(x%10==9) return 9;

    if(x%10==0) return 0;
}

int utszj_2(int a)
{
    if(a==1) return 1;
    long long akt = 0;
    akt = pow(a, a);
    akt%=10;

    return (akt + utszj_2(a-1));
}

int main()
{
    ifstream f("cifra.in");
    ofstream g("cifra.out");

    int t;
    f>>t;
    //cout<<t<<endl;

    string s;
    getline(f, s);
    int n, m, ered;
    for(int i=1; i<=t; i++)
    {
        getline(f, s);
        //cout<<s<<endl;
        if(s.length()>=2){
            s=s.substr(s.length()-2, 2);
            n=(s[0]-48)*10+(s[1]-48);
        }
        else n=s[0]-48;

        //brut force
        m=n;
        ered = 0;
        for(int i=m; i>=1; i--)
        {
            ered += utszj(i);
            ered%=10;
        }
        //g<<"brut force "<<ered<<" ";

        //cout<<"szam = "<<n<<endl;

        ered = 0;
        ered+=n/20*4;
        while(n%20!=0)
        {
            ered+=utszj(n);
            //cout<<n<<" "<<utszj(n)<<endl;
            //cout<<"ered = "<<ered<<endl;
            ered%=10;
            n--;
        }
        g<<ered<<endl;

        //brut force

    }


    return 0;
}