301163: CF217B. Blackboard Fibonacci

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Blackboard Fibonacci

题意翻译

Feyn 喜欢斐波那契数列和黑板,于是他决定把二者结合起来。 一开始黑板上有两个数 $0$ 和 $1$,$0$ 在上面 $1$ 在下面。定义两种操作 `T` 和 `B`。前者的意思是把当前上面的那个数擦掉,并在原位置写上两个数的和,后者差不多,只是改成了擦掉下面的数,当然也要写上两个数的和。这里的两个数指的都是擦掉之前的两个数。 给定 $n$ 和 $r$($n,r\le 10^6$),请构造一个长度为 $n$ 的操作序列使得完成这个操作序列的过程中,**最后一个写上的数恰好是 $r$**。并且希望这个序列第一个字母是 `T`,同时最小化序列中相邻的相同元素对数。无解输出 `IMPOSSIBLE`。

题目描述

Fibonacci numbers are the sequence of integers: $ f_{0}=0 $ , $ f_{1}=1 $ , $ f_{2}=1 $ , $ f_{3}=2 $ , $ f_{4}=3 $ , $ f_{5}=5 $ , $ ... $ , $ f_{n}=f_{n-2}+f_{n-1} $ . So every next number is the sum of the previous two. Bajtek has developed a nice way to compute Fibonacci numbers on a blackboard. First, he writes a 0. Then, below it, he writes a 1. Then he performs the following two operations: - operation "T": replace the top number with the sum of both numbers; - operation "B": replace the bottom number with the sum of both numbers. If he performs $ n $ operations, starting with "T" and then choosing operations alternately (so that the sequence of operations looks like "TBTBTBTB $ ... $ "), the last number written will be equal to $ f_{n+1} $ . Unfortunately, Bajtek sometimes makes mistakes and repeats an operation two or more times in a row. For example, if Bajtek wanted to compute $ f_{7} $ , then he would want to do $ n=6 $ operations: "TBTBTB". If he instead performs the sequence of operations "TTTBBT", then he will have made 3 mistakes, and he will incorrectly compute that the seventh Fibonacci number is 10. The number of mistakes in the sequence of operations is the number of neighbouring equal operations («TT» or «BB»). You are given the number $ n $ of operations that Bajtek has made in an attempt to compute $ f_{n+1} $ and the number $ r $ that is the result of his computations (that is last written number). Find the minimum possible number of mistakes that Bajtek must have made and any possible sequence of $ n $ operations resulting in $ r $ with that number of mistakes. Assume that Bajtek always correctly starts with operation "T".

输入输出格式

输入格式


The first line contains the integers $ n $ and $ r $ ( $ 1<=n,r<=10^{6}) $ .

输出格式


The first line of the output should contain one number — the minimum possible number of mistakes made by Bajtek. The second line should contain $ n $ characters, starting with "T", describing one possible sequence of operations with that number of mistakes. Each character must be either "T" or "B". If the required sequence doesn't exist, output "IMPOSSIBLE" (without quotes).

输入输出样例

输入样例 #1

6 10

输出样例 #1

2
TBBTTB

输入样例 #2

4 5

输出样例 #2

0
TBTB

输入样例 #3

2 1

输出样例 #3

IMPOSSIBLE

Input

题意翻译

Feyn 喜欢斐波那契数列和黑板,于是他决定把二者结合起来。 一开始黑板上有两个数 $0$ 和 $1$,$0$ 在上面 $1$ 在下面。定义两种操作 `T` 和 `B`。前者的意思是把当前上面的那个数擦掉,并在原位置写上两个数的和,后者差不多,只是改成了擦掉下面的数,当然也要写上两个数的和。这里的两个数指的都是擦掉之前的两个数。 给定 $n$ 和 $r$($n,r\le 10^6$),请构造一个长度为 $n$ 的操作序列使得完成这个操作序列的过程中,**最后一个写上的数恰好是 $r$**。并且希望这个序列第一个字母是 `T`,同时最小化序列中相邻的相同元素对数。无解输出 `IMPOSSIBLE`。

加入题单

上一题 下一题 算法标签: