Cod sursa(job #3216809)

Utilizator nicushor21Pirlog Marian Nicolae nicushor21 Data 19 martie 2024 20:39:50
Problema Sarpe Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <bits/stdc++.h>
using namespace std;
int n[2005],a[2005],b[2005],r[2005],i,j,t;
char c;
ifstream fin("sarpe.in");
ofstream fout("sarpe.out");
int main()
{
    i=1;
    while(fin>>c){
        n[i]=c-'0';
        i++;
    }
    n[0]=i-1;
    for(i=1;i<=n[0]/2;i++)
        swap(n[i],n[n[0]-i+1]);
    for(i=0;i<=n[0];i++)
        a[i]=b[i]=n[i];
    i=1;
    while(i<=a[0]&&a[i]==0) i++;
    a[i--]--;
    for(;i>0;i--)
        a[i]=9;
    while(a[a[0]]==0)   a[0]--;
    i=1;
    while(i<=b[0]&&b[i]==0) i++;
    if(i>1){
        b[i--]--;
        for(;i>1;i--)
            b[i]=9;
        b[1]=8;
    }else{
        if(b[1]>1)  b[1]-=2;
        else{
            i=2;
            while(i<=b[0]&&b[i]==0) i++;
            b[i--]--;
            for(;i>0;i--)
                b[i]=9;
        }
    }
    while(b[b[0]]==0)   b[0]--;
    for(i=1;i<=n[0];i++){
        n[i]*=4;
    }
    t=n[1]/10; i=2; n[1]=n[1]%10; n[0]=1;
    while(t>0){
        t+=n[i];
        n[i]=t%10;
        t/=10;
        n[0]++;
    }
    for(i=1;i<=a[0];i++){
        a[i]*=2;
    }
    t=a[1]/10; a[1]=a[1]%10; a[0]=1;
    while(t>0){
        a[0]++;
        t+=a[a[0]];
        a[a[0]]=t%10;
        t/=10;
    }
    for(i=1;i<=a[0];i++){
        for(j=1;j<=b[0];j++){
            r[i+j-1]+=a[i]*b[j];
        }
    }
    t=r[1]/10; r[1]=r[1]%10; r[0]=1;
    while(t>0){
        r[0]++;
        t+=r[r[0]];
        r[r[0]]=t%10;
        t/=10;
    }
    for(i=0;i<=r[0];i++)
        a[i]=r[i];
    r[0]=1; t=a[1]+n[1]; r[1]=t%10;
    t/=10;
    while(t>0){
        r[0]++;
        t+=a[r[0]]+n[r[0]];
        r[r[0]]=t%10;
        t/=10;
    }
    for(i=r[0];i>0;i--)
        fout<<r[i];
    return 0;
}