Cod sursa(job #935746)

Utilizator apopeid14Apopei Daniel apopeid14 Data 4 aprilie 2013 17:17:23
Problema Next Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <fstream>
#include <cstring>
#define MAXCIF 1000005
#define In "next.in"
#define Out "next.out"
using namespace std;
int a[MAXCIF];
char s[MAXCIF];
long long rest;
ofstream fout(Out);
void Afisare()
{
    int i;
    for(i=a[0];i>0;i--)
        fout<<a[i];
    fout<<"\n";
}
 
void Citire()
{
    int i,n;
    ifstream fin(In);
    fin>>(s+1)>>rest;
    n = strlen(s+1);
    a[0] = n;
    for(i=1;i<=n;i++)
        a[i] = s[n-i+1]-48;
    fin.close();
}
 
long long Rest()
{
    int i;
    long long mod = 0;
    for(i=a[0];i;i--)
        mod = (mod*10+a[i])%rest;
    return mod;
}
 
void Aduna()
{
    int i = 1;
    long long aux;
    aux = a[i] + rest;
    a[i] = aux%10;
    aux/=10;
    while(aux)
    {
        i++;
        aux += a[i];
        a[i] = aux%10;
        aux/=10;
    }
    if(i>a[0])
        a[0] = i;
}
 
void Rezolvare()
{
    long long r;
    r = Rest();
    if(r==0)
        r = rest;
    rest = rest-r;
    Aduna();
    Afisare();
}
 
int main()
{
    Citire();
    Rezolvare();
    return 0;
}