Cod sursa(job #56405)

Utilizator anoukAnca Dumitrache anouk Data 29 aprilie 2007 15:29:31
Problema Next Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <cstring>
using namespace std;

short int N[1000001], R[16];
char X[1000001];
long long D;

void Suma ()
{

	while ( N[0] > R[0] ) R[++R[0]] = 0;
	while ( N[0] < R[0] ) N[++N[0]] = 0;

	char t = 0;
	for ( int i = 1; i <= N[0]; i++ )
	{
		int aux = ( t + N[i] + R[i] ) % 10;
		t    = ( t + N[i] + R[i] ) / 10;
		N[i] = aux;
	}
	if ( t > 0 )
	{
		N[0] = N[0] + 1;
		N[N[0]] = t;
	}
}

int main()
{
    ifstream fin("next.in");
    ofstream fout("next.out");
    
    long long rest = 0; int i, t = 0;
    fin >> X >> D;
    N[0] = strlen(X);
    for (i = 0; i < N[0] && '0' <= X[i] && '9' >= X[i]; i++)
        N[N[0] - i] = X[i] - '0';

    for (i = N[0]; i > 0; i--)  
        rest = (rest * 10 + N[i]) % D;
    
    if (rest) { rest = (D - rest) % D;
    while (rest)
    {
          R[++R[0]] = rest % 10;
          rest /= 10;
    }
    if (!R[0]) R[0] = 1;
    
    Suma(); }

    for (i = N[0]; i > 0; i--)
        fout << N[i];
    fout << "\n";
    
    fin.close();
    fout.close();
    return 0;
}