fork download
  1. #include <iostream>
  2. #include <cmath> // pow() and log10()
  3. #include <cstring> // memset()
  4. #include <algorithm> // std::min_element() and std::max_element()
  5.  
  6. using namespace std;
  7.  
  8. // Problem: http://n...content-available-to-author-only...r.net/Problem/Details/5621
  9.  
  10. int arr[18]; // Store the binary sequence
  11. long double result[262144]; // (=2^18) Store the possible results of number z
  12. double a, b;
  13. int na, nb, _index = 0, n = 0;
  14. /* a and b is the 2 numbers of input. na is the number of digit of a, nb is the number of
  15. digit of b, _index is the index of array result[], n is the number of elements of result[]*/
  16.  
  17. void Analyze(int a[])
  18. {
  19. /* "_na" is used with pow() of base 10 and mod 10 to take the first digit of "a" from
  20.   the left to the right. So is "_nb".
  21.   For instance: a = 584 => na = 3 => _na = 2 => (584 / 10^2) % 10 = 5 is the first digit
  22.   of a from the left to the right.
  23.   */
  24. int _na = na - 1, _nb = nb - 1, temp = na + nb - 1;
  25. for (int i = 0; i < (na + nb); ++i) {
  26. if (a[i] == 0) {
  27. int digit = static_cast<int>(::a/pow(10.0, (double)_na)) % 10;
  28. --_na;
  29. result[_index] += static_cast<double>(digit) * pow(10.0, (double)temp--);
  30. }
  31. else {
  32. int digit = static_cast<int>(::b/pow(10.0, (double)_nb)) % 10;
  33. --_nb;
  34. result[_index] += static_cast<double>(digit) * pow(10.0, (double)temp--);
  35. }
  36. }
  37. ++_index; // update the index of result[] for the next assignments.
  38. }
  39.  
  40. // Backtracking
  41. void ListBinary(int i)
  42. {
  43. // na + nb is the length of the binary sequence.
  44. if (i == (na + nb)) {
  45. /* Count if the number of value 0 in the binary sequence is equal to the length of
  46.   a and the number of value 1 in the binary sequence is equal to the length of b. */
  47. int zero = 0, one = 0;
  48. for (int i = 0; i < (na + nb); ++i) {
  49. if (arr[i] == 0)
  50. ++zero;
  51. else ++one;
  52. }
  53. /* If the binary sequence is appropriate, then we will use it to create a number
  54.   and put into the result[] */
  55. if (zero == na && one == nb) {
  56. ++n;
  57. Analyze(arr);
  58. }
  59. }
  60. else {
  61. arr[i] = 0;
  62. ListBinary(i + 1);
  63. arr[i] = 1; // backtrack
  64. ListBinary(i + 1);
  65. }
  66. }
  67.  
  68. int main()
  69. {
  70. memset(result, 0, sizeof(result));
  71. cin >> a >> b;
  72. na = (int)log10(a) + 1;
  73. nb = (int)log10(b) + 1;
  74. ListBinary(0);
  75. cout << *min_element(result, result + n) << endl << *max_element(result, result + n);
  76. return 0;
  77. }
  78.  
Success #stdin #stdout 0s 19336KB
stdin
287 45
stdout
24587
45287