1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include<iostream> #include<cstdio> #define ll long long using namespace std;
const ll N=1e3;
ll R,tmp,m,n;
ll a[N+5];
char s[22]={'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F','G','H','I','J','K','L'};
inline ll read() { ll ret=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-f;ch=getchar();} while(ch>='0'&&ch<='9') {ret=(ret<<3)+(ret<<1)+ch-'0';ch=getchar();} return ret*f; }
void write(ll x) { static char buf[22];static ll len=-1; if(x>=0) { do{buf[++len]=x%10+48;x/=10;}while(x); } else { putchar('-'); do{buf[++len]=-(x%10)+48;x/=10;}while(x); } while(len>=0) putchar(buf[len--]); }
int main() {
n=read();R=read();tmp=n;
write(n);putchar('=');
if(n==0) { write(n); printf("(base%lld)",R); } if(n>0) { R=-R;m=-1; while(tmp) { a[++m]=tmp%R;tmp/=R; } for(ll i=0;i<=m;i++) { if(a[i]!=0&&(i&1)) { a[i]=R-a[i];a[i+1]++; } a[i+1]+=a[i]/R;a[i]%=R; } while(a[m+1]>0) { m++; if(m&1) { a[m]=R-a[m];a[m+1]++; } a[m+1]+=a[m]/R;a[m]%=R; } for(ll i=m;i>=0;i--) putchar(s[a[i]]); R=-R; printf("(base%lld)",R); } if(n<0) { R=-R;m=-1;tmp=-tmp; while(tmp) { a[++m]=tmp%R;tmp/=R; } for(ll i=0;i<=m;i++) { if(a[i]!=0&&!(i&1)) { a[i]=R-a[i];a[i+1]++; } a[i+1]+=a[i]/R;a[i]%=R; } while(a[m+1]>0) { m++; if(!(m&1)) { a[m]=R-a[m];a[m+1]++; } a[m+1]+=a[m]/R;a[m]%=R; } for(ll i=m;i>=0;i--) putchar(s[a[i]]); R=-R; printf("(base%lld)",R); }
return 0; }
|