Number Sequence
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 43278 Accepted Submission (s): 9449
Problem Descriptiona 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 is to calculate the value of f (n). Inputthe input consists of multiple test cases. Each test case contains 3 integers a, b and N in a single line (1 <= A, b <=, 1 <= n <= 100,000,000). Three zeros signal the end of the input and this test case are not a is processed. Outputfor each test case, print the value of f (n) in a single line. Sample INPUT1 1 3 1 2 0 0 0 Sample Output2 5
//first edition, direct violence recursion, timeout#include <iostream>using namespacestd;Long Long intFacintAintBintN) { if(n = =1|| n = =2) return 1; Else return(A * FAC (A,B,N-1) + B * FAC (A,B,N-2)) %7;}intMain () {intA, B, N; while(Cin >> A >> B >> n&&a! =0&& B! =0&& n! =0) {cout<< FAC (A, B, n% -) <<Endl; } return 0;}
HDU (1005) Number Sequence