Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7.  
  8. ll base,nb,in_dec,in_new;
  9. string d;
  10. vector<ll>dig;
  11. vector<string>res;
  12. ll v[516];
  13. string r[516];
  14.  
  15. void pr()
  16. {
  17. v['1']=1;v['a']=10;v['j']=19;v['s']=28;
  18. v['2']=2;v['b']=11;v['k']=20;v['t']=29;
  19. v['3']=3;v['c']=12;v['l']=21;v['u']=30;
  20. v['4']=4;v['d']=13;v['m']=22;v['v']=31;
  21. v['5']=5;v['e']=14;v['n']=23;v['w']=32;
  22. v['6']=6;v['f']=15;v['o']=24;v['x']=33;
  23. v['7']=7;v['g']=16;v['p']=25;v['y']=34;
  24. v['8']=8;v['h']=17;v['q']=26;v['z']=35;
  25. v['9']=9;v['i']=18;v['r']=27;v['0']=0;
  26. r[1]="1";r[10]="A";r[19]="J";r[28]="S";
  27. r[2]="2";r[11]="B";r[20]="K";r[29]="T";
  28. r[3]="3";r[12]="C";r[21]="L";r[30]="U";
  29. r[4]="4";r[13]="D";r[22]="M";r[31]="V";
  30. r[5]="5";r[14]="E";r[23]="N";r[32]="W";
  31. r[6]="6";r[15]="F";r[24]="O";r[33]="X";
  32. r[7]="7";r[16]="G";r[25]="P";r[34]="Y";
  33. r[8]="8";r[17]="H";r[26]="Q";r[35]="Z";
  34. r[9]="9";r[18]="I";r[27]="R";r[0]="0";
  35. }
  36.  
  37. int check_data() // returns 1-big, 2-small, 0-false
  38. {
  39. ll var=-1;
  40. for(int i=0;i<d.length();++i)
  41. {
  42. if(d[i]>='0'&&d[i]<='9')continue;
  43. else if(var==-1)
  44. {
  45. if(d[i]>='A'&&d[i]<='Z')var=1;
  46. else var=2;
  47. }
  48. else if(var==1&&(d[i]<'A'||d[i]>'Z')) return 0;
  49. else if(var==2&&(d[i]<'a'||d[i]>'z'))return 0;}
  50. return var;
  51. }
  52.  
  53. int count_dec()
  54. {
  55. ll var=1,res=0;
  56. for(int i=dig.size()-1;i>=0;--i)
  57. {
  58. res+=(var*dig[i]);
  59. var*=base;
  60. }
  61. return res;
  62. }
  63.  
  64. void to_new_base()
  65. {
  66. while(in_dec)
  67. {
  68. res.push_back(r[in_dec%nb]);
  69. in_dec/=nb;
  70. }
  71. }
  72.  
  73. bool ch()
  74. {
  75. for(int i=0;i<dig.size();++i)if(dig[i]>=base) return 0;
  76. return 1;
  77. }
  78.  
  79. int main()
  80. {
  81. pr();
  82. cin>>base>>d>>nb;
  83. int var=check_data();
  84.  
  85. if(!var)
  86. {
  87. cout<<"NaN"<<endl;
  88. return 0;
  89. }
  90. if(var==1)for(int i=0;i<d.length();++i)
  91. {
  92. if(d[i]>='0'&&d[i]<='9')continue;
  93. else d[i]+=32;
  94. }
  95.  
  96. for(int i=0;i<d.length();++i)dig.push_back(v[d[i]]);
  97.  
  98. if(!ch())
  99. {
  100. cout<<"NaN"<<endl;
  101. return 0;
  102. }
  103.  
  104. in_dec=count_dec();
  105. if(!in_dec){
  106. cout<<'0'<<endl;
  107. return 0;
  108. }
  109.  
  110. to_new_base();
  111. for(int i=res.size()-1;i>=0;--i)cout<<res[i];
  112. cout<<endl;
  113.  
  114.  
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement