Problem
Zibon just started his courses in Computer science. After having some lectures on programming courses he fell in love with strings. He started to play with strings and experiments on them. One day he started a string of arbitrary (of course positive) length consisting of only {a, b}. He considered it as 1st string and generated subsequent strings from it by replacing all the b’s with ab and all the a’s with b. For example, if he ith string is abab, (i+1)th string will be b(ab)b(ab) = babbab. He found that the Nth string has the length X and Mth string has the length Y. He wondered what will be length of the Kth string. Can you help him?
Input
The first line of the input file contains an integer T (T ≤ 1000) which denotes the total number of test cases. The description of each test case is given below:
Five integers N, X, M, Y and K where (0 < N, M, X, Y, K < 109 and N ≠ M).
Output
For each test case produce one line of output giving either the number which is desired length (modulo 1000000007) or the string “Impossible”. You output Impossible if the given input is not possible.
Sample Input
|
|
Sample Output
|
|
Solution
題目描述:
一開始由 a, b 構成的字串,每一次操作會將 a 換成 b, 將 b 換成 ab。
在第 N 次之後長度為 X、第 M 次之後長度為 Y,求第 K 次之後長度為何?
題目解法:
看到 a 換成 b, 將 b 換成 ab,其實只考量數量關係,很明顯是一個費式數列的關係。
因此,只要假設一開始有多少個 a,有多少個 b 在一開始的字串中,利用兩條方程式求解,之後再帶入費式數列的快速運算即可。
|
|