【问题描述】
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).

【解题报告】
因为是mod 7,所以说最多有49种可能。数据范围太大,不能用裸搜,于是找循环点。
 1 program number;
2
3 var
4 h:array[0..50]of longint;
5 t:array[0..7,0..7]of longint;
6 a,b,n,i,j,w,s:longint;
7 can:boolean;
8
9 procedure init;
10 begin
11 assign(input,'number.in');
12 reset(input);
13 assign(output,'number.out');
14 rewrite(output);
15 end;
16
17 procedure outit;
18 begin
19 close(input);
20 close(output);
21 halt;
22 end;
23
24 procedure main;
25 begin
26 repeat
27 fillchar(t,sizeof(t),0);
28 can:=false;
29 readln(a,b,n);
30 if (a+b+n=0) then outit;
31 h[1]:=1;
32 h[2]:=1;
33 for i:=3 to n do
34 begin
35 if t[h[i-1],h[i-2]]<=0 then
36 begin
37 h[i]:=(h[i-1]*a+h[i-2]*b)mod 7;
38 t[h[i-1],h[i-2]]:=i;
39 end
40 else
41 begin
42 s:=t[h[i-1],h[i-2]]-2;
43 can:=true;
44 w:=i-s-2;
45 for j:=1 to w-1 do h[j]:=h[j+s-1];
46 h[0]:=h[w+s-1];
47 break;
48 end;
49 end;
50 if not can then writeln(h[n]) else writeln(h[(n-s+1)mod w]);
51 until seekeof;
52 end;
53
54 begin
55 init;
56 main;
57 outit;
58 end.
59