Pagini recente » Cod sursa (job #1953521) | Cod sursa (job #857547) | Cod sursa (job #105733) | Cod sursa (job #3125583) | Cod sursa (job #268216)
Cod sursa(job #268216)
#include<algorithm>
using namespace std;
int n;
long long d;
char s[5000001];
void ins(int poz,int x){
switch(x){
case 0:
s[poz]='0';
break;
case 1:
s[poz]='1';
break;
case 2:
s[poz]='2';
break;
case 3:
s[poz]='3';
break;
case 4:
s[poz]='4';
break;
case 5:
s[poz]='5';
break;
case 6:
s[poz]='6';
break;
case 7:
s[poz]='7';
break;
case 8:
s[poz]='8';
break;
case 9:
s[poz]='9';
break;}}
void rev(){
int i,x;
char aux;
for(i=0,x=n-1; i<=x; ++i,--x){
aux=s[i];
s[i]=s[x];
s[x]=aux;}}
void adun(){
int i,t,x;
for(i=1,x=(t=s[0]-'0'+1)%10,ins(0,x),t/=10; i<n||t; t/=10,++i){
x=(t+=s[i]-'0')%10;
ins(i,x);}
n=i;}
long long rest(){
int i,t;
for(i=n-1,t=0; i>=0; --i)
t=(t*10+s[i]-'0')%d;
return t;}
void solve(){
int i;
gets(s);
n=strlen(s);
rev();
scanf("%lld",&d);
if(!rest())
for(i=n-1; i>=0; --i)
printf("%c",s[i]);
else{
for(; ; ){
adun();
if(!rest()){
for(i=n-1; i>=0; --i)
printf("%c",s[i]);
break;}}}}
int main(){
freopen("next.in","r",stdin);
freopen("next.out","w",stdout);
solve();
return 0;}