Topic 1081: Recursive sequence (Matrix fast-power-recursive)

Source: Internet
Author: User

Topic 1081: Recursive sequence

time limit:1 seconds

Memory limit:32 MB

Special question: No

submitted:5885

Resolution:800

Title Description:

Given A0,A1, and n-2 in An=p*a (n-1) + q*a (p,q). Here n >= 2. The modulus of the number of K to 10000 is obtained.

Input:

The input consists of 5 integers: A0, A1, P, Q, K.

Output:

The number of k a (k) to 10000 modulo.

Sample input:
20 1 1) 14 5
Sample output:
8359
Source:
2009, Tsinghua University computer Research Life Test real problem
1#include <stdio.h>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cstdlib>6#include <cmath>7#include <vector>8#include <queue>9#include <map>Ten#include <Set> One  A using namespacestd; -  -typedefLong Longll; the  - Constll mod =10000; -  -  + structMatrix - { +ll mat[2][2]; A Matrix () at     { -Memset (Mat,0,sizeof(MAT)); -     } -Matrixoperator*(ConstMatrix A)Const -     { - Matrix ret; in          for(intI=0;i<2; i++) -         { to              for(intj=0;j<2; j + +) +             { -                  for(intk=0;k<2; k++) theRET.MAT[I][J] = (ret.mat[i][j]+ (mat[i][k]*a.mat[k][j])%mod)%MoD; *             } $         }Panax Notoginseng         returnret; -     } the }; +  AMatrix Quickmul (Matrix X,intk) the { + Matrix ret; -      for(intI=0;i<2; i++) $Ret.mat[i][i] =1; $      while(k>0) -     { -         if(k&1) ret=ret*x; thek>>=1; -x=x*x;Wuyi     } the     returnret; - } Wu  - intMain () About { $ ll A0,a1,p,q,k; -      while(~SCANF ("%lld%lld%lld%lld%lld",&a0,&a1,&p,&q,&k)) -     { - Matrix St; Ast.mat[0][0] = p,st.mat[0][1] =Q; +st.mat[1][0] =1, st.mat[1][1] =0; theMatrix A =Quickmul (st,k); - Matrix B; $b.mat[0][0] =A1; theb.mat[1][0] =A0; theA = A *b; theprintf"%lld\n", a.mat[1][0]%MoD); the     } -     return 0; in}
View Code

Topic 1081: Recursive sequence (Matrix fast-power-recursive)

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.