fork(1) download
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <stack>
  7. #include <bitset>
  8. #include <algorithm>
  9. #include <functional>
  10. #include <numeric>
  11. #include <sstream>
  12. #include <iostream>
  13. #include <cstdio>
  14. #include <cmath>
  15. #include <cstdlib>
  16. #include <ctime>
  17. #include <queue>
  18. #include <cstring>
  19. #include <string>
  20. using namespace std;
  21.  
  22. #define FOREACH(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it ++)
  23. #define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; i ++)
  24. #define DOW(i, b, a) for(int i = (b), _a = (a); i >= _a; i --)
  25. #define REP(i, n) FOR(i, 0, (n) - 1)
  26. #define DEP(i, n) DOW(i, (n) - 1, 0)
  27. #define all(a) (a).begin(), (a).end()
  28. #define pb(a, b) (a).push_back((b))
  29. #define pf(a, b) (a).push_front((b))
  30. #define ave(a, b) (a+b)/2
  31. #define RI(a) scanf("%d", &a)
  32. #define RII(a) scanf("%I64d", &a)
  33.  
  34. typedef vector <int> VI;
  35. typedef vector <string> VS;
  36. typedef pair <int, int> PII;
  37. typedef vector <PII> VII;
  38. typedef long long int64;
  39. template <class T> inline int size(const T&c) { return c.size(); }
  40. template <class T> inline int lenght(const T&c) { return c.length(); }
  41.  
  42. int n, r;
  43. bool istrace;
  44. string trace;
  45.  
  46. int mistake(int t, int b, char next, int lv) {
  47. if (t == b)
  48. if (t == 1 && lv+1 == n) {
  49. trace = "T";
  50. return next == 'T';
  51. }
  52. else return 1e9;
  53. if (t > b) {
  54. int ret = mistake(t-b, b, 'T', lv+1)+(next=='T');
  55. if (istrace) trace += "T";
  56. return ret;
  57. }
  58. else {
  59. int ret = mistake(t, b-t, 'B', lv+1)+(next=='B');
  60. if (istrace) trace += "B";
  61. return ret;
  62. }
  63. }
  64.  
  65. int main() {
  66. RI(n); RI(r);
  67. if (r == 1) {
  68. if (n == 1) cout << 0 << endl << "T";
  69. else cout << "IMPOSSIBLE";
  70. return 0;
  71. }
  72. istrace = false;
  73. int ret = 1e9, t = -1;
  74. FOR(i, 1, r-1) {
  75. int tmp = 1e9;
  76. if (i > r-i) tmp = mistake(i, r-i, 'B', 1);
  77. if (i < r-i) tmp = mistake(i, r-i, 'T', 1);
  78. if (ret > tmp) {
  79. ret = tmp;
  80. t = i;
  81. }
  82. }
  83. if (t == -1) cout << "IMPOSSIBLE";
  84. else {
  85. istrace = true;
  86. trace = "";
  87.  
  88. if (t > r-t) { mistake(t, r-t, 'B', 1); trace += "B"; }
  89. if (t < r-t) { mistake(t, r-t, 'T', 1); trace += "T"; }
  90. cout << ret << endl;
  91. cout << trace << endl;
  92. }
  93. return 0;
  94. }
Runtime error #stdin #stdout 0.02s 10792KB
stdin
999999 999999 
stdout
Standard output is empty