#include <stdio.h>
/*int main(){ //P2084 正进制
int m,n,num=0;
scanf("%d %d",&m,&n);
int i=0;
while(n!=0){
int temp=n%10;
if(temp>=m){
break;
}
for(int j=0;j<i;j++){
temp=temp*m;
}
num+=temp;
i++;
n=n/10;
}
printf("%d",num);
return 0;
}*/
int main(){ //P1017 负进制
int decimal,base; //十进制数、进制基数
scanf("%d %d",&decimal,&base);
getchar();
char num[18]; //用于储存转换结果
int i=0;
while(decimal){ //逐项取余
int remainder=decimal%base; //余数
int quotient=decimal/base; //商
if(remainder<0){ //余数不能小于零。故商++,并取被除数与(自增后的商*基数)的差作为新的余数
remainder=decimal-(quotient+1)*base;
quotient++;
}
if(remainder<=9){
num[i]=remainder+'0';
}
else{
num[i]=remainder-10+'A';
}
i++;
decimal=quotient;
}
num[i]='\0';
for(int j=i-1;j>=0;j--){ //逆序输出
printf("%c",num[j]);
}
printf("(base:%d)",base);
return 0;
}
//负进制与正进制做法大致相同,都是逐项取余。但负进制需要考虑余数小于零的情况。
https://www.luogu.com.cn/problem/P1017