fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define FR0(i,N) for(i=0;i<(N);i++)
  4. #define FR1(i,N) for(i=1;i<=(N);i++)
  5. #define FRN(i,k,N) for(i=k;i<(N);i++)
  6. #define pf printf
  7. #define db double
  8. #define max3(a,b,c) max(max(a,b),c)
  9. #define min3(a,b,c) min(min(a,b),c)
  10. #define sci(n) scanf("%d",&n)
  11. #define scl(n) scanf("%lld",&n)
  12. #define scf(n) scanf("%f",&n)
  13. #define scd(n) scanf("%lf",&n)
  14. #define scs(s) scanf("%s",&s)
  15. #define scll(n) scanf("%%I64d",&n)
  16. #define PI acos(-1.0)
  17. #define LL long long
  18. #define MX 1000005
  19. #define MOD 1000000007
  20. typedef long long int ll;
  21. bool status[1100002];
  22.  
  23. int main()
  24. {
  25. char s1[20],s2[20];
  26. ll base1,base2,m1,m2;
  27. while(scanf("%s%s",&s1,&s2)!=EOF)
  28. {
  29. //int first=atoi(s1);
  30. //int second=atoi(s2);
  31. int flag=0;
  32. for(base1=2; base1<=36; base1++)
  33. {
  34.  
  35. for(base2=2; base2<=36; base2++)
  36. {
  37. //1st base1 Calculation start here;
  38. ll n1=0,n2=0,cnt=0;
  39. ll sum1=0,sum2=0;
  40. for(int i=strlen(s1)-1; i>=0; i--)
  41. {
  42. if(s1[i]>47 && s1[i]<58)
  43. {
  44. m1=s1[i]-48;
  45. }
  46. else
  47. {
  48. m1=s1[i]-55;
  49. }
  50. if(m1>=base1)//if digit greater than or equal this base1.It isn't taken .Skip this base1 and searching another base1;
  51. {
  52. cnt=1;
  53. }
  54. sum1+=m1*pow(base1,n1++);
  55. }
  56. //cout<<"sum1 = "<<sum1<<endl;
  57. //2nd base2 calculation start here;
  58. for(int j=strlen(s2)-1; j>=0; j--)
  59. {
  60. if(s2[j]>47 && s2[j]<58)//Here, ASECII value 0,1,2,3,4,5,6,7,8,9 is 48,49,50,51,52,53,54,55,56,57;
  61. {
  62. m2=s2[j]-48;//converting string to integer;
  63. }
  64. else
  65. {
  66. m2=s2[j]-55;//converting string to integer;
  67. }
  68. if(m2>=base2)//if digit greater than or equal this base1.It isn't taken .Skip this base1 and searching another base1;
  69. {
  70. cnt=1;
  71. }
  72. sum2+=m2*pow(base2,n2++);
  73. }
  74. if(cnt)
  75. {
  76. continue;
  77. }
  78. //cout<<"sum2 = "<<sum2<<endl;
  79. if(sum1==sum2)
  80. {
  81. pf("%s (base %lld) = %s (base %lld)\n",s1,base1,s2,base2);
  82. flag=1;
  83. break;
  84. }
  85. }
  86. if(flag)//if find my result this time i break this loop;
  87. {
  88. break;
  89. }
  90.  
  91.  
  92. }
  93. if(flag==0)//if i don't find any solution according to input then execute this statement;
  94. {
  95. pf("%s is not equal to %s in any base 2..36\n",s1,s2);
  96. }
  97. }
  98. }
  99.  
Success #stdin #stdout 0s 4488KB
stdin
12   5
    10     A
12 34
  123   456
  1    2
  10   2
stdout
12 (base 3) = 5 (base 6)
10 (base 10) = A (base 11)
12 (base 17) = 34 (base 5)
123 is not equal to 456 in any base 2..36
1 is not equal to 2 in any base 2..36
10 (base 2) = 2 (base 3)