Cod sursa(job #2483594)

Utilizator bostanlucastefanBostan Luca-Stefan bostanlucastefan Data 29 octombrie 2019 22:18:00
Problema Next Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream cin("next.in");
ofstream cout("next.out");

long long v[1000020];
long long d,r;

int main()
{
    char ch;
    int l=0,ok=1;
    cin.get(ch);
    do
    {
        l++;
        if(ch>='0' && ch<='9')
        v[l]=ch-'0';
        cin.get(ch);
    }
    while(ch!='\n');
        reverse(v+1,v+1+l);
    cin>>d;
    for(int i=l;i>=1;i--)
    {
        r=r*10+v[i];
        if(r>=d)
        {
            ok=0;
            r%=d;
        }
    }
    if(ok)
    {
        cout<<d;
        return 0;
    }
    if(r!=0)
    {
        d-=r;
        v[1]+=d;
        int j=1;
        while(v[j]>9)
        {
            v[j+1]+=v[j]/10;
            v[j]%=10;
            j++;
        }
        j--;
        if(v[l+1]!=0)
            l++;
    }
    for(int i=l;i>=1;i--)
        cout<<v[i];
    return 0;
}