|
|
back to boardCommon BoardWho can solve this problem(e-mail to:tracy_wz@tom.com) Posted by tracy 2 May 2005 12:39 Time limit: 1 Seconds Memory limit: 32768K Total Submit: 3035 Accepted Submit: 684 -------------------------------------------------------------------------------- A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed. Output For each test case, print the value of f(n) on a single line. Sample Input 1 1 3 1 2 10 0 0 0 Sample Output 2 5 Re: Who can solve this problem(e-mail to:tracy_wz@tom.com) This is a generalization of the Fibonacci sequence. We can compute a Fibonacci number in O(logN) with the (0 1; 1 1) matrix. Multiplied by (Fn-1 Fn; Fn Fn+1) it gives (Fn Fn+1; Fn+1 Fn+2). We can do the multiplication in O(logN) time with the natural powers of 2 (ie a^9 = (((a^2)^2)^2)*a). We now generalize this: instead of (0 1; 1 1) (because Fn = Fn-1 + Fn) now we have (0 1;A B). And again we compute it by repeated exponentiary. Being modulo 7 its quite easy to solve it. I'll send you a source code if I will have time to write it... Edited by author 04.05.2005 20:15 |
|
|