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)
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.
For each test case, print the value of f(n) on a single line.
乍一看 好簡單 簡簡單單一個遞歸就能解決了, 結果不能AC. 查了下,知道了,因爲1<=n<=100,000,000這個東西,要那麼多次...遞歸的話,結果就絕對是棧溢出了,因爲太多了..循環也不能直接就這麼用,因爲超出時間限制---此乃問題之始;
百度了下才知道 這是有規律的; 一開始想不通,怎麼去確定這個規律,爲什麼一定會有規律,; 好的念頭總是來的很突然,第二天一想就想明白了;
思路如下: 先找循環對 --比如3,4,1,5,4,6,3,4,....那麼3,4就是我們要找的了..可以去確定週期的長度,爲什麼說一定會有循環對,因爲,對於任意一個數,其後任意跟一個數,,這2個數都是0-6,也就是這兩個數最多種可能也不過7*7=49種可能,那麼定義一個50長度的數組便足夠了;之後就是在這個數組中找到循環啦;
另外 定義Q爲週期,那麼,n%Q ,和while(n>50)n-=Q; 雖然目的是一樣的..不過耗時卻不一樣.!--1005;