HDU 1005Time
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U
Description
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 is 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 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.
Output
For each test case, print the value of f (n) in a single line.
Sample Input
1 1 31 2 100 0 0
Sample Output
25
Exercises
At first, I skipped the question directly.
But later found that there are some rules, for f[n-1] or f[n-2] Value only 0,1,2,3,4,5,6 these 7 numbers, a, b is fixed, so there are only 49 possible values. This relationship is known that each item only has a relationship with the first two items, so when two consecutive entries appear in front of the cyclic section, note that the Loop section is not necessarily the beginning of 1, 1. And because of a set of test data in F[n] only 49 of the possible answers, the worst case is that all situations are encountered, then it will also be in the 50 operations to produce a follow-up link. After the loop is found, you can solve the problem
Note: The size of the cycle, there may be a large cycle, there may be a small cycle!
#include<iostream>UsingNamespace Std;
int F[54]={0,1,1};IntMain(){int ABNQ=1;While(CIN>>a>>b>>n&&a&&b&&n){For(int I=3; I<54; ++i){F[I]= (A*f[I-1]+b*f[I-2])%7;If(I>4){If(f[I-1]==f[3]& &f[i]==f[4 {q=i-4< Span class= "Sh-symbol" >; //to pay special attention, you can think about why? }}cout <<f[n%q]<<endl; }return 0;< Span class= "Sh-cbracket" >}
HDU 1005 ()