Cod sursa(job #1509426)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 23 octombrie 2015 20:47:40
Problema Next Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <array>
#include <algorithm>
#include <iostream>
using namespace std;

constexpr int maxn = 1000100;
using bignum = pair<int, array<char, maxn> >;

void find_size(bignum& b){
	for(b.first = maxn-1; !b.second[b.first]; --b.first);
	++b.first; }

long long find_rest(const bignum& b, const long long x){
	long long rest = 0;
	for(int i = b.first-1; i >= 0; --i){
		rest = (10 * rest + b.second[i] - '0') % x; }
	return rest; }

void add(bignum& b, long long x){
	for(int i = 0; i < b.first && x; ++i){
		x += b.second[i] - '0';
		b.second[i] = x%10 + '0';
		x /= 10; }
	while(x){
		b.second[b.first++] = x%10 + '0';
		x /= 10; } }

int main(){
	ifstream f("next.in");
	ofstream g("next.out");
	bignum b = {};
	f.getline(&b.second[0], maxn, '\n');
	find_size(b);
	reverse(begin(b.second), begin(b.second) + b.first);
	long long d;
	f >> d;
	const long long rest = find_rest(b, d);

	add(b, (d-rest)%d);

	for(int i = b.first-1; i >= 0; --i){
		g << b.second[i]; }
	
	return 0; }