HDU 5451 Best Solver (Fibonacci)

Source: Internet
Author: User

Thank you for this question let me review the Line generation, also learned some strange number theory.

Make

After two expansion, the square root was partially offset.

Obviously there are

So the answer to that request is

If n is relatively small, you can directly power the two-item, but the question N is very large.

This problem has a strange connection with the eigenvalues of matrices and the recurrence of numbers.

The form of a generalized Fibonacci sequence is as follows

Written in matrix form is

There is a strange conclusion:

Where lambda1,lambda2 is the characteristic value of the recursive matrix, only the Lambda1!=lambda2 case is discussed here.

This strange conclusion is in fact very easy to prove,

Based on the above results, the multiplication and distribution law of the matrix can be used to conclude the conclusion completely.

Make lambda1=p,lambda2=q, can find A and B, the answer is in the recursive nth item

Then reduce the n by looking for a loop section

M is prime when the general approach: http://blog.csdn.net/ACdreamers/article/details/25616461

Fermat theorem and Orazhun are unknown ...

All of the M cycle sections are small, direct violence, and then memory

Lambda1!=lambda2, so a must be diagonal, and a^n can be expressed as

corresponding characteristic value

and have

So the answer to the fast power of a matrix is to calculate the trace minus 1.

#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;structmatrix{inte[2][2]; int*operator[](intp) {        returnE[p]; }};ll Mod; Matrixoperator* (Matrix &a, Matrix &B)    {Matrix R;  for(inti =0; I <2; i++){         for(intj =0; J <2; J + +) {R[i][j]=0;  for(intK =0; K <2; k++) {R[i][j]= (R[i][j] + (LL) a[i][k]*b[k][j]+mod)%Mod; }        }    }    returnR;}    Matrix Matrix_pow (Matrix A,ll p) {matrix R;  for(inti =0; I <2; i++){         for(intj =0; J <2; J + +) {R[i][j]= I==j?1:0; }    }     while(p) {if(p&1) R = r*A; A= A *A; P>>=1; }    returnR;} ll Qpow (ll a,ll p,ll MoD) {LL ret=1;  while(p) {if(p&1) ret = (ret*a)%MoD; A= (a*a)%MoD; P>>=1; }    returnret;}Const intMAXM =46337+5;intR[MAXM],F[MAXM];intMain () {//freopen ("In.txt", "R", stdin);    intT, Kas =0; scanf"%d",&T);  while(t--) {ll x; scanf ("%i64d%i64d",&x,&Mod); if(!R[mod]) {f[0] =2; f[1] =Ten;  for(inti =2;; i++) {F[i]= (10ll*f[i-1]-f[i-2]+MOD)%Mod; if(F[i] = = f[1] && f[i-1] = = f[0]) {R[mod] = I1; Break; }        }} Matrix A; a[0][0] =Ten%mod; a[0][1] = mod-1; a[1][0] =1; a[1][1] =0; Auto ans= Matrix_pow (A, (Qpow (2, X,r[mod]) +1)%r[Mod]); printf ("Case #%d:%d\n", ++kas, (ans[0][0]+ans[1][1]+mod-1)%Mod); }    return 0;}

HDU 5451 Best Solver (Fibonacci)

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.