Cod sursa(job #1687794)

Utilizator alex202Alex Abrudan alex202 Data 13 aprilie 2016 08:41:41
Problema Next Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream in("next.in");
ofstream out("next.out");
const int nmax=1000000;
int a[nmax + 5];
int resth(int k)
{
    int i;
    long long r;
    r=0;
    for(i=a[0]; i>=1; i--)
    {
        r=(r*10)+a[i];
        r=r%k;
    }
    return r;
}
int main()
{
    long long k,rez,ck;
    int n,i,tr,aux,nr=0;

    char sir[nmax];
    in.getline(sir,nmax);
    n=strlen(sir);
    for(i=n-1; i>=0; --i)
        a[n-i]=sir[i]-'0';
    a[0]=n;
    in>>k;
    ck=k;
    rez=k-resth(k);
    while(ck>0)
    {
        ck/=10;
        nr++;
    }
    /*if(nr>n)
    {
        out<<k;
        return 0;
    }*/
    if(resth(k)==0)
    {
        for(i=a[0]; i>=1; i--)
            out<<a[i];
        return 0;
    }

    else
    {
        tr=0;
        for(i=1; i<=a[0]; ++i)
        {
            aux=a[i]+(rez%10)+tr;
            a[i]=aux%10;
            tr=aux/10;
            rez/=10;

        }
        if(tr==1)
        {
            a[++a[0]]=1;
        }
        for(i=a[0]; i>=1; --i)
            out<<a[i];
    }
    return 0;
}