SHUOJ1857 Yaoge Chicken Row Series nine--a lot of CHICKEN steak!!! "Matrix Fast Power"

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/KirisameMarisa/p/4187670.html

Title Link: http://202.121.199.212/JudgeOnline/problem.php?id=1857

1857:yaoge Chicken Row Series nine--a lot of CHICKEN steak!!! Time limit:3 Sec Memory limit:64 MB
submit:98 Solved:6 Description

Yaoge bought n blocks of chicken, of which the mass of the nth block is M (n), while its mass m (n) satisfies m (n) =f (n) 2

Known as f (n) =x*f (n-1) +y*f (n-2). Wherein, f (0) =1,f (1) = 1.

Yaoge hope you can help him figure out the results of the total mass of these chickens after the 10007 modulo.

Input

The first line enters a T, which indicates that there is a T set of test data (t<=10000),

Next T line enter 3 numbers per line, N,x,y, (2<=n,x,y<=100000000);

Output

Results of the total mass of the output chicken row after 10007 modulo

Sample Input1 of 2 3Sample Output5195HINT

First set of examples, F (1) =1,f (2) =2,m (1) =1,m (2) = 4, sum equals 5

Group II, F (1) =1,f (2) =5,f (3) =13,m (1) =1,m (2) =25,m (3) = 169, sum equal to 195

Source

Xyiyy

The simple point is that s (n) =∑f (n) 2, where F (n) =x*f (n-1) +y*f (n-2), and F (0) =1,f (1) = 1.

First of all, we see an F (n) =x*f (n-1) +y*f (n-2), and I think the first reaction must feel like a Fibonacci sequence. That's right, so before we solve this problem, let's first talk about the solution to the Fibonacci sequence.

the solution of the Fibonacci sequence

The Fibonacci sequence, also known as the Golden Section, refers to a sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 、......

Mathematically, the Fibonacci sequence is defined as a recursive method: F (0) =0,f (1) =1,fn=f (n-1) +f (n-2) (n>=2,n∈n*)

Two-force youth practices: Obviously can be calculated by the F (n), can be in O (n) time to get the answer, but the efficiency of this algorithm is too low, once n is a larger number must time out undoubtedly.

Literary youth Practices: Then, the first reaction of the students in the Department of Mathematics is to ask for the formula in order to reach an answer at O (1). Yes, the formula for the Fibonacci sequence is available, and has been begged by the predecessors:

But there are irrational numbers in this equation, the use of floating-point numbers in the computer can not be accurately stored, more can not get the results of a number of models, and like the Fibonacci sequence can be found in the formula, and other such as the subject can only be feel powerless.

High-end atmosphere on the level of a cool boom-and-blast computer system for Youth Practices (pia~): Well, to go back, let's look at the practice in the ACM Program Design Competition.

Matrices are a good thing, and sometimes we can use matrices to simplify calculations. We can change the recursion of the Fibonacci sequence into matrix form, that is, to construct a matrix:

To remember this matrix is a, there are:

So, we only ask out an can get FN, how to quickly solve an, it is necessary to use the matrix fast power, can be solved in O (logn) time, again not fine, we can see the final code implementation.

Ii. the method of finding a similar Fibonacci sequence

Returning to the subject, we have observed an equation of f (n) =x*f (n-1) +y*f (n-2), which we want to use to simplify the operation of the Matrix fast power. However, in this case we encountered a problem, although the Fibonacci sequence of the basis f (n) is very good, but requires ∑f (n) is not, because the matrix fast power operation is "jumping", and do not talk about the ∑f (n) 2. At this time, we need to expand our thinking.

Further derivation of the recursion: S (n) =∑f (n) 2 = s (n-1) +f (n) 2 = s (n-1) +x2f (n-1) 2+y2f (n-2) 2+2XYF (n-1) f (n-2)

The rest is good, there is a 2XYF (n-1) F (n-2) More annoying, then we continue to further, write another: F (n) *f (n-1) = (x*f (n-1) +y*f (n-2)) *f (n-1) = X*f (n-1) 2+y*f (n-1) *f (n-2 ), so it is convenient to construct the matrix recursion.

We construct the matrix recursion:

So that we can use the matrix fast power, and finally just add the first line of An-1 up on the line

#include <iostream>#include<ios>#include<iomanip>#include<functional>#include<algorithm>#include<vector>#include<sstream>#include<list>#include<queue>#include<deque>#include<stack>#include<string>#include<Set>#include<map>#include<cstdio>#include<cstdlib>#include<cctype>#include<cmath>#include<cstring>#include<climits>using namespacestd;#defineXinf Int_max#defineINF 1<<30#defineMAXN 0x7fffffff#defineEPS 1e-8#defineZero (a) fabs (a) <eps#defineSqr (a) ((a) * (a))#defineMP (x, y) make_pair (x, y)#definePB (x) push_back (x)#definePF (x) push_front (x)#defineREP (x,n) for (int x=0; x<n; X + +)#defineREP2 (X,L,R) for (int x=l; x<=r; X + +)#defineDEP (x,r,l) for (int x=r; x>=l; x--)#defineCLR (a,x) memset (a,x,sizeof (A))#defineIT iterator#definePI ACOs (-1.0)#defineTest puts ("OK");#define_ Ios_base::sync_with_stdio (0); Cin.tie (0);typedefLong LongLl;typedef pair<int,int>pii;typedef priority_queue<int,vector<int>,greater<int> >pqi;typedef Vector<PII>vii;typedef Vector<int>VI;#defineX First#defineY Second#defineMOD 10007typedefstruct{    intdata[4][4];} M M I={1,0,0,0,//Unit Matrix           0,1,0,0,           0,0,1,0,           0,0,0,1}; M Matrixmul (M a,m b) {m res=I; REP (i,4) {REP (J,4)        {            inttemp=0; REP (k,4) Temp= (temp+ (a.data[i][k]*b.data[k][j])%mod)%MOD; RES.DATA[I][J]=temp; }    }    returnRes;} intPow_mod (M A,intN//Fast Power{M res=I;  while(n>0)    {        if(n&1) Res=Matrixmul (res,a); A=Matrixmul (a,a); N>>=1; }    intans=0; REP (J,4) ans= (ans+res.data[0][J])%mod;//Add the first line of the final Solution result    returnans;} intMain () {_intT; scanf ("%d",&T);  while(t--)    {        intN,x,y; scanf ("%d%d%d",&n,&x,&y); X=x%mod;y=y%MOD; M a={1, (X*x)%mod, (Y*y)%mod, (2*x*y)%MOD,0, (X*x)%mod, (Y*y)%mod, (2*x*y)%MOD,0,1,0,0,                   0X0, y}; printf ("%d\n", Pow_mod (a,n-1)); }    return 0;}

Third, the promotion ("The Challenge Program Design Competition second Edition" P201)

In general, for M-item recursion, if the recursive type is

You can write the recursive form as matrix

by calculating the n power of the Matrix, the value of the nth item can be computed in the time of O (M3logn). However, if a recursive type contains constant items, it is slightly more complex and needs to be the following form:

PostScript: This is my classmate xyiyy out of the topic, as the sixth session of Shanghai University graduate student algorithm design and application of a mathematical problem appeared. (Yao in the big ACM team played often black role, because Yao elder brother love to eat chicken steak (hush, in fact, he is a fat), so xyiyy and Ghbgh always caught the opportunity on the Black Yao elder brother, so that the big OJ among the appearance of a beautiful scenery line: Yao Brother chicken Row series)

Links Yao Blog: http://www.cnblogs.com/baobaopangzi88/(you see he calls himself a baby fat)

By--kirisame_marisa 2014-12-27 00:25:05

SHUOJ1857 Yaoge Chicken Row Series nine--a lot of CHICKEN steak!!! "Matrix Fast Power"

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.