Cod sursa(job #3367240)

Utilizator Coman_DianaComan Diana Coman_Diana Data 6 octombrie 2026 21:44:06
Problema Next Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <iostream>
#include <stdio.h>

using namespace std;

#define CIFMAX 1000000

char vec[CIFMAX + 1];
int lung;
long long num_d;

long long restul_impartirii( long long num_d ) {
  long long t = 0;
  int ind;

  for ( ind = lung - 1; ind >= 0; ind-- ) {
    t = t * 10 + vec[ind];
    t %= num_d;
  }

  return t;
}

void adunare( long long a ) {
  int ind = 0;

  while( a > 0 ) {
    a += vec[ind];
    vec[ind] = a % 10;
    a /= 10;
    ind++;
  }

  if ( ind > lung )
    lung = ind;
}

int main()
{
    FILE *fin, *fout;
    long long rest, term_adun;
    int ind, aux;
    char ch;

    fin = fopen( "next.in", "r" );
    ch = fgetc( fin );
    while ( '0' <= ch && ch <= '9' ) {
      vec[lung++] = ch - '0';
      ch = fgetc( fin );
    }
    fscanf( fin, "%lld", &num_d );
    fclose( fin );

    for ( ind = 0; ind < lung / 2; ind++ ) {
      aux = vec[ind];
      vec[ind] = vec[lung - 1 - ind];
      vec[lung - 1 - ind] = aux;
    }

    rest = restul_impartirii( num_d );
    if ( rest > 0 ) {
      term_adun = num_d - rest;
      adunare( term_adun );
    }

    fout = fopen( "next.out", "w" );
    for ( ind = lung - 1; ind >= 0; ind-- )
      fprintf( fout, "%d", vec[ind] );
    fclose( fout );

    return 0;
}