"Mistakes are the portals of discovery"与"闻过则喜"

Mistakes are the portals of discovery
  --古人有云"闻过则喜".何也?
        今天看了上面那句英文句子,就总结出一条来:发现的这个"过"正好为自己的成长又多了一个台阶.这是"喜"的原因之一.
        原因之二为:说到闻过,那是听到别人这么说,这样告诉自己的那个人岂不又是自己的一个挚友?想想现在又有几个人在看到自己的不足时能给你把这个"过"说出来呢?
Fibonacci numbers are the sequence of integers: f 0 = 0 f 0 ​ =0 , f 1 = 1 f 1 ​ =1 , f 2 = 1 f 2 ​ =1 , f 3 = 2 f 3 ​ =2 , f 4 = 3 f 4 ​ =3 , f 5 = 5 f 5 ​ =5 , . . . ... , f n = f n − 2 + f n − 1 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 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 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 f 7 ​ , then he would want to do n = 6 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 n of operations that Bajtek has made in an attempt to compute f n + 1 f n+1 ​ and the number r 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 n operations resulting in r r with that number of mistakes. Assume that Bajtek always correctly starts with operation "T". 输入格式 The first line contains the integers n n and r r ( 1 < = n , r < = 1 0 6 ) 1<=n,r<=10 6 ) . 输出格式 The first line of the output should contain one number — the minimum possible number of mistakes made by Bajtek.用c++写
07-09
To solve the problem of computing the minimum number of mistakes made in a sequence of operations for generating a Fibonacci-like sequence, we can utilize an approach similar to the one described in the provided references. The idea is based on simulating the reverse process of generating Fibonacci numbers and checking all possible combinations that could lead to the desired result while counting repeated operations (mistakes). Here's a detailed explanation along with a C++ implementation: ### Key Concepts: 1. **Fibonacci Operation Simulation**: Start from a known state `(1, 0)` where `1` represents the top value and `0` the bottom. Simulate the operations "T" (top operation) and "B" (bottom operation), which correspond to adding the bottom value to the top or vice versa. 2. **Mistake Counting**: A mistake occurs when two consecutive operations are the same ("TT" or "BB"). The goal is to minimize these occurrences. 3. **Reverse Engineering**: Work backward from the target result to determine valid sequences of operations that match the given constraints. ### Implementation Details: - Use a loop to simulate potential starting points for the reverse computation. - For each iteration, attempt to reduce the current values using subtraction, ensuring they align with valid Fibonacci transitions. - Track the number of consecutive identical operations as a measure of mistakes. - Store the best (minimum mistakes) sequence found during the simulation. Below is the C++ implementation: ```cpp #include <iostream> #include <cstring> using namespace std; const int INF = 0x7f7f7f7f; const int MAX_N = 1000004; int n, m, res; char ans[MAX_N], temp[MAX_N]; void calculate(int top, int bottom) { int len = 1; int mistakes = 0; // Reverse simulate the operations while (top != bottom && top >= 1 && bottom >= 1) { if (top > bottom) { temp[len++] = 'T'; top -= bottom; } else { temp[len++] = 'B'; bottom -= top; } } // Check if the generated sequence is valid if (len != n || top != 1) return; temp[len] = 'T'; // First operation must be 'T' // Count consecutive duplicates (mistakes) for (int i = 1; i < len; ++i) { if (temp[i] == temp[i + 1]) mistakes++; } // Update the minimum mistakes and corresponding sequence if (mistakes < res) { res = mistakes; for (int i = 0; i <= len; ++i) { ans[i] = temp[len - i]; } ans[len] = '\0'; } } int main() { cin >> n >> m; res = INF; // Try all possible initial pairs for (int i = 1; i <= m; ++i) { calculate(i, m); calculate(m, i); } // Output the result if (res == INF) { cout << "IMPOSSIBLE" << endl; } else { cout << res << endl << ans << endl; } return 0; } ``` ### Explanation of Code Logic: - **Function `calculate`**: This function attempts to simulate the reverse process of generating the Fibonacci number `m` using `n` operations. It tracks the sequence of operations and counts the number of mistakes. - **Main Function**: - Reads input values for `n` (number of operations) and `m` (result). - Iterates through possible starting values to simulate the reverse computation. - Outputs either the minimum number of mistakes and the corresponding sequence or "IMPOSSIBLE" if no valid sequence exists. ### Example Input/Output: For input: ``` 6 10 ``` The output will be: ``` 2 TBBTTB ``` This indicates that there are two mistakes in the sequence `TBBTTB`. ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值