First, it is easy to find a circular section when a table is violent. Therefore, the statement at the beginning is to directly find the circular section. The result is always wa,
The reason is that some cycles are not starting from 1, 1, 1. Detailed proof here: http://acm.hdu.edu.cn/discuss/problem/post/reply.php? Postid = 19818 & messageid = 1 & Deep = 0
So I learned from the idea of the great gods. Because of % 7, V [7] [7] can be used to record f (n) = (A * F (n-1) + B * F (n-2) mod 7. this status. If the status is the same, the loop section appears.
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1005
The Code is as follows:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <cctype> # include <algorithm> # include <string> # define n 100 using namespace STD; int V [7] [7]; int f [N] = {0, 1}; int main () {int A, B, N; while (scanf ("% d", & A, & B, & N), A + B + n) {int x = 1, y = 1, k = 3; memset (v, 0, sizeof (v); While (! V [x] [Y]) {v [x] [Y] = K; // records the position where this status occurs. F [k] = (A * Y + B * X) % 7; X = y; y = f [k]; k ++ ;} int S = V [x] [Y]; // This is important because the cycle may not start from. If (n <k) printf ("% d \ n", F [N]); else printf ("% d \ n", F [(n-s) % (k-S) + S]); // K-S is the cycle T. } Return 0 ;}
HDU 1005 number sequence (loop section)