Cod sursa(job #2187037)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 26 martie 2018 10:07:26
Problema Next Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <fstream>
#include <cstring>

using namespace std;

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

const int cifmax=1000000;
short int x[cifmax+5];
short int y[cifmax+5];
short int sol[cifmax+5];
char s[cifmax+5];
int n;
long long d;

long long rest(short int a[],long long val)
{
    long long ans=0;
    for(int i=a[0];i>=1;i--)
        ans=((long long)10*ans+(long long)a[i])%val;
    return ans;
}

void add(short int a[],short int b[],short int s[])
{
    int aux=0,rezid=0,i;
    for(i=1;i<=a[0] || i<=b[0] || rezid>0;i++)
    {
        aux=rezid;
        if(i<=a[0])
            aux+=a[i];
        if(i<=b[0])
            aux+=b[i];
        s[i]=aux%10;
        rezid=aux/10;
    }
    s[0]=i-1;
}

void pune(short int a[],long long val)
{
    a[0]=0;
    while(val>0)
    {
        a[++a[0]]=val%10;
        val/=10;
    }
}

void afis(short int a[])
{
    for(int i=a[0];i>=1;i--)
        fout<<a[i];
}
int main()
{
    fin.getline(s,cifmax+5);
    n=strlen(s);
    for(int i=n-1;i>=0;i--)
        x[++x[0]]=s[i]-'0';
    fin>>d;
    long long key=rest(x,d);
    if(key==0)
    {
        afis(x);
        return 0;
    }
    key=d-key;
    pune(y,key);
    add(x,y,sol);
    afis(sol);
    return 0;
}
/**



**/