(HDU) 1005--Number Sequence (series)

Source: Internet
Author: User

The problem description sequence is defined as follows: F (1) =1, F (2) =1, f (n) = (A * F (n1) + B * F (n2)) MoD7. Given a, B and N, you want to calculate the value of f (n). The input input is made up of multiple test cases. Each test case is in one row (1<= A, B <= +,1<= N <= -, the, the) contains 3 integers, a, B, and N. Three zeros indicates the end of the input and this test case is not processed. Output for each test case, the value of f (n) is output on one line. Sample Input1 1 31 2 Ten0 0 0Sample Output25
problem

Note that the range of N, 1≤n≤1000000000, be careful to timeout the hyper-memory issue.

See the topic directly write often ignore a lot of details, give the formula of the topic attention to see if there is a law.

MoD is the meaning of modulo, this can be considered to take the remainder.

The so-called congruence, as the name implies, is that many of the numbers are removed by a number D and have the same remainder. D The mathematical title is modulo. such as A=6, b=1, d=5, we say that A and B are modulo d congruence.       Because they all have the same remainder 1. The mathematical notation is: a≡b (mod D) can be seen when n<D, all n to the D with the quotient, such as the number of hours on the clock, are less than 12, so the number of hours are modulo 12 of the same business. For the same remainder there are three kinds of statements are equivalent, respectively: (1A and B are modulo d congruence. (2) there is an integer n, which makes the a=b+nd. (3) d divisible by A-B. The above three statements can be obtained by conversion to be correct and equivalent. Basic Law: The congruence formula also has many of our common laws, such as the Equality Law, the binding law, the Exchange law, the transfer law .... As indicated below:1) A≡a (mod d)2) symmetry a≡b (mod d) →b≡a (mod d)3) transitivity (a≡b (mod D), b≡c (mod D)) →a≡c (mod D) if a≡x (mod d), b≡m (mod d), then4) a+b≡x+m (mod d)5) a-b≡x-m (mod d)6) a*b≡x*m (mod d)7) a/b≡x/m (mod d)8) A≡b (mod D) is a-b divisible by D9) A≡b (mod d) a^n≡b^N (mod d)Tenif AC≡BC (mod m), and C and M coprime, then the a≡b (mod m) modulo operation rules: (A+ b) MoD p = (a mod p + b mod p) mod p (1) (A-b) MoD p = (a mod p-b mod p) mod p (2) (A* b) MoD p = (a mod p * b mod p) mod p (3) A^b mod p = ((a mod p) ^b) mod p (4) Binding Rate: ((a)+B mod p + c) mod p = (A + (b+c) mod p) mod p (5) ((a*B) mod p * c) mod p = (A * (b*c) mod p) mod p (6) exchange rate: (A+ b) MoD p = (b+a) mod p (7) (A* b) MoD p = (b * a) mod p (8) Allocation rate: ((a)+B mod p * c) mod p = ((A * c) mod p + (b * c) mod p) mod p (9) Important theorem: if A≡b (mod p), then for any C, there is (a+ c) ≡ (b + c) (mod p); (Tenif A≡b (mod p), then for any C, there is (a* c) ≡ (b * c) (mod p); ( Oneif A≡b (mod p), then for any C, there is AC≡BC (mod p); -) This article address: http://blog.csdn.net/a359680405/article/details/41675143
what to know: modulo operation Properties

For formula F (n) = (A * f (n-1) + B * F (n-2)) MoD 7, to prevent overflow, change to f (n) = (a mod 7 * f (n-1) + b mod 7* f (n-2)) MoD 7

(If you don't understand the reason for rewriting, take a look at the above, or write yourself a few examples to verify.) This question A and B range is smaller, in fact, can not change . )

F (n) itself is guaranteed to be within the range of 0-6 integers, and the same f (n-1) and F (n-2) have only these seven values.

F (n) is determined by F (n-1) and F (n-2), because A and B are a definite value that has no effect on the entire formula.

With the idea of mapping, a pair of F (n-1) and F (n-2) can only map one f (n),

So the calculation of f (n) is at most only 49 possible, if you still can not understand, look at the following table ...

Suppose a=2,b=1 ... but the cycle period is not 49 (this is the worst case).

Fn F (n-1)
0 1 2 3 4 5 6
F (n-2) 0 0 2 4 6 1 3 5
1 1 3 5 0 2 4 6
2 2 4 6 1 3 5 0
3 3 5 0 2 4 6 1
4 4 6 1 3 5 0 2
5 5 0 2 4 6 1 3
6 6 1 3 5 0 2 4

If there are two consecutive entries in the preceding sequence, the minimum cycle section is found (note that there is not necessarily a continuous two consecutive times, that is, f (1) and F (2))

Eg:1 1 3 4 1 6 1 3 (1 and 3 in front, minimum cycle section is 13416)

With the smallest loop section found, the code is easy to write.

The following is a direct 49 number as a circular section, but is not the smallest loop section (the periodic function is not the minimum positive period).

1#include <iostream>2 using namespacestd; 3 intarr[ -]; 4 intMain ()5 {  6     intn,a,b; 7arr[1]=arr[2]=1; 8      while(cin>>a>>b>>N)9     {  Ten         if(a==0&&b==0&&n==0)   One              Break;  A         intminn=n< -? N: -;//a little bit of optimization -          for(intI=3; i<=minn; i++)   -         {   theArr[i]= (a*arr[i-1]+b*arr[i-2])%7;  -         }   -cout<<arr[n% the]<<Endl;  -    +     }   -     return 0;  +}
do not look for the minimum cycle section, directly using 49 as the notation of the cycle

This is a tricky way to do it, but it's also an extension:

1#include <cstring>2#include <cstdio>3#include <cstdlib>4 using namespacestd;5 6 intrec[ -];7 8 intMain ()9 {Ten     intA, B, N; Onerec[0] = rec[1] = rec[2] =1; A      while(SCANF (" %d%d%d", &a, &b, &n), a | B |N) -     { -         intBeg, end, flag =0; the          for(inti =3; I <= n &&!flag; ++i) -         { -Rec[i] = (A * rec[i-1] + b * rec[i-2] ) %7; -              for(intj =2; J <= I-1; ++j) +             { -                 if(Rec[i] = = Rec[j] && rec[i-1] = = rec[j-1] ) +                 { ABeg = j, end =i; atFlag =1; -                      Break; -                 } -             } -         } -         if(flag) in         { -printf"%d\n", rec[beg+ (n-end)% (end-Beg)] ); to         } +         Else -printf"%d\n", Rec[n]); the     } *     return 0; $}
finding the smallest follow -up link

(HDU) 1005--Number Sequence (series)

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.