Cod sursa(job #270908)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 4 martie 2009 18:18:11
Problema Next Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
//#include<algorithm>
//using namespace std;
#include<stdio.h>
int a[1000001],b[1000001];
long long d;
void adun(){
	int i;
	long long t;
	for(i=1,t=0; i<=a[0]||i<=b[0]||t; t/=10,++i)
		a[i]=(t+=a[i]+b[i])%10;
	a[0]=i-1;}
void trans(long long x){
	for(; x; b[++b[0]]=x%10,x/=10);}
long long rest(){
	int i;
	long long t;
	for(i=a[0],t=0; i>0; --i)
		t=(t*10+a[i])%d;
	return t;}
void solve(){
	int i,j,aux;
	char ch;
	for(scanf("%c",&ch); ch!='\n'; a[++a[0]]=ch-'0',scanf("%c",&ch));
	for(i=1,j=a[0]; i<j; ++i,--j){
		aux=a[i];
		a[i]=a[j];
		a[j]=aux;}
	scanf("%lld",&d);
	trans((d-rest())%d);
	adun();
	for(i=a[0]; i>0; --i)
		printf("%d",a[i]);}
int main(){
    freopen("next.in","r",stdin);
    freopen("next.out","w",stdout);
    solve();
    return 0;}