#include #include #include #include #include using namespace std; int from(int r,int n) { int res=0; int a=1; do { res+=a*(n%10); a*=r; }while (n/=10); return res; } vector to(int r,int n) { vector help; do { help.push_back(n%r); }while (n/=r); vector res;res.push_back(0);res.push_back(0);res.push_back(0);res.push_back(0); for (int i=help.size()-1;i>=0;i--) res.push_back(help[i]); return res; } vector make(vector temp,int r) { bool sp=0; for (int i=temp.size()-1;i>=1;i--) { sp=1-sp; if (temp[i]>=0 && temp[i]<(-r)) continue; if (temp[i]<0){ temp[i]-=r; if (!sp) temp[i-1]++; else temp[i-1]++; //printf("%d\n",temp[i-1]); continue; } temp[i]=0; if (sp) temp[i-1]--; else temp[i-1]--; } return temp; } void change(vector k) { string a="";bool w=1; for (int i=0;i qfa; string z;vector q,a; cin>>z; while (z!="end") { string help=""; bool qqq=0;int n,r; if (z[0]=='t') qqq=1; else qqq=0; if (qqq) help=z.substr(3); else help=z.substr(5); istringstream iss(help);iss>>r;r=-r; scanf("%d",&n);if (n==0) printf("0\n");else if (qqq==0){printf("%d\n",from(r,n));}else { q=to(r,n); a=make(q,r); bool w=1;int wynik=0;change(a); //for (int i=0;i>z; } return 0; }