HDU 1005 (periodic problem), hdu1005 Periodic Problem

Source: Internet
Author: User

HDU 1005 (periodic problem), hdu1005 Periodic Problem
HDU 1005Time Limit:1000 MSMemory Limit:32768KB64bit IO Format:% I64d & % I64u

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 are 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 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.

Output

For each test case, print the value of f (n) on a single line.

Sample Input

1 1 31 2 100 0 0

Sample Output

25
Question:

I skipped this question at the beginning ..

But later found that there are still some rules, for f [n-1] or f [N-2] values only 0, 1, 2, 3, 4, 5, 6 these 7 numbers, A, B is fixed, so there are only 49 possible values. The relationship between each item and the first two items is known, so when the two consecutive items appear in the previous cycle section, note that the cycle section does not necessarily start. In addition, in a group of test data, f [n] has only 49 possible answers. The worst case is that all the situations are met, then, a circular section is generated in 50 operations. After finding the cycle, you can solve this problem.

Note: The cycle size may be large or small!

# Include <iostream> using namespace std;

Int f [54] = {0, 1}; int main () {int A, B, n, q = 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; // pay special attention, can you think about why ?} } Cout <f [n % q] <endl;} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.