Cod sursa(job #1840532)

Utilizator tziplea_stefanTiplea Stefan tziplea_stefan Data 4 ianuarie 2017 15:24:11
Problema Sarpe Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.94 kb
#include <fstream>
#define VAL 1005
#define CIF 1000005

using namespace std;

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

int N[VAL], i, j;
int N1[CIF], N2[VAL];
int x[VAL];
string s;

void ADD_NUM()
{
    int t=0, i, L, cif;
    L=max(N1[0], N[0]);
    for (i=1; i<=L; i++)
    {
        cif=t;
        if (i<=N1[0])
          cif+=N1[i];
        if (i<=N[0])
          cif+=N[i];
        N1[i]=cif % 10;
        t=cif / 10;
    }
    N1[0]=L;
    if (t==1)
      N1[++N1[0]]=1;
}

void SUBSTRACT_1()
{
    int i, l, t=0, cif;
    for (i=1; i<=N[0]; i++)
    {
        cif=N[i];
        if (i<=x[0])
          cif-=x[i];
        cif-=t;
        if (cif<0)
        {
            t=1;
            cif+=10;
        }
        else
          t=0;
        N1[i]=cif;
        if (cif!=0)
          l=i;
    }
    N1[0]=l;
}

void SUBSTRACT_2()
{
    int i, l, t=0, cif;
    for (i=1; i<=N[0]; i++)
    {
        cif=N[i];
        if (i<=x[0])
          cif-=x[i];
        cif-=t;
        if (cif<0)
        {
            t=1;
            cif+=10;
        }
        else
          t=0;
        N2[i]=cif;
        if (cif!=0)
          l=i;
    }
    if (s!="2")
      N2[0]=l;
    if (s=="2")
      N2[0]=1;
}

void MULTIPLY_N_4()
{
    int i, j, t=0, l;
    int c[CIF], cif;
    c[0]=0;
    for (i=1; i<=N[0]+x[0]; i++)
      c[i]=0;
    for (i=1; i<=x[0]; i++)
      for (j=1; j<=N[0]; j++)
        c[i+j-1]+=N[j]*x[i];
    for (i=1; i<=N[0]+x[0]; i++)
    {
        cif=c[i]+t;
        c[i]=cif % 10;
        if (c[i]!=0)
          l=i;
        t=cif / 10;
    }
    N[0]=l;
    for (i=1; i<=l; i++)
      N[i]=c[i];
}

void MULTIPLY_N1_2()
{
    int i, j, t=0, l;
    int c[CIF], cif;
    c[0]=0;
    for (i=1; i<=N1[0]+x[0]; i++)
      c[i]=0;
    for (i=1; i<=x[0]; i++)
      for (j=1; j<=N1[0]; j++)
        c[i+j-1]+=N1[j]*x[i];
    for (i=1; i<=N1[0]+x[0]; i++)
    {
        cif=c[i]+t;
        c[i]=cif % 10;
        if (c[i]!=0)
          l=i;
        t=cif / 10;
    }
    N1[0]=l;
    for (i=1; i<=l; i++)
      N1[i]=c[i];
}

void MULTIPLY_N1_N2()
{
    int i, j, t=0, l;
    int c[CIF], cif;
    c[0]=0;
    for (i=1; i<=N1[0]+N2[0]; i++)
      c[i]=0;
    for (i=1; i<=N2[0]; i++)
      for (j=1; j<=N1[0]; j++)
        c[i+j-1]+=N1[j]*N2[i];
    for (i=1; i<=N1[0]+N2[0]; i++)
    {
        cif=c[i]+t;
        c[i]=cif % 10;
        if (c[i]!=0)
          l=i;
        t=cif / 10;
    }
    N1[0]=l;
    for (i=1; i<=l; i++)
      N1[i]=c[i];
}

int main()
{
    fin >> s;
    N[0]=s.size();
    for (i=0; i<s.size(); i++)
      N[N[0]-i]=s[i]-'0';
    if (N[0]==N[1] && N[0]==1)
      fout << 2 << '\n';
    else
    {
        x[0]=1;
        x[1]=1;
        SUBSTRACT_1();
        x[0]=1;
        x[1]=2;
        SUBSTRACT_2();
        x[0]=1;
        x[1]=4;
        MULTIPLY_N_4();
        x[1]=2;
        MULTIPLY_N1_2();
        MULTIPLY_N1_N2();
        ADD_NUM();
        for (i=N1[0]; i>0; i--)
          fout << N1[i];
    }
    fin.close();
    fout.close();
    return 0;
}