Rapid power of the ultraviolet A 10870 recurrences Matrix

Source: Internet
Author: User

Question: give you the nth item of the recursive formula that requires the rapid power of the matrix

Solution: Classic Matrix Quick power recursion of the nth project practice.

Solution code:

1 // file name: temp. CPP 2 // Author: darkdream 3 // created time: wednesday, September 17, 2014, 45 seconds, 4 5 # include <vector> 6 # include <list> 7 # include <map> 8 # include <set> 9 # include <deque> 10 # include <stack> 11 # include <bitset> 12 # include <algorithm> 13 # include <functional> 14 # include <numeric> 15 # include <utility> 16 # include <sstream> 17 # include <iostream> 18 # include <iomanip> 19 # include <cstdio> 20 # include <cmath> 21 # include <cstdlib> 22 # include <cstring> 23 # include <ctime> 24 # define ll long long25 using namespace STD; 26 ll n, m, D; 27 int Ta, TB; 28 struct matrix29 {30 LL mat [15] [15]; 31 void clear () 32 {33 memset (mat, 0, sizeof (MAT); 34} 35 void output () 36 {37 for (INT I = 0; I <n; I ++) 38 {39 for (Int J = 0; j <n; j ++) 40 printf ("% LLD", mat [I] [J]); 41 printf ("\ n"); 42} 43} 44 void Init () 45 {46 clear (); 47 for (INT I = 0; I <N; I ++) 48 {49 scanf ("% LLD", & mat [0] [I]); 50 mat [0] [I] % = m; 51} 52 for (INT I = 1; I <n; I ++) 53 {54 mat [I] [I-1] = 1; 55} 56} 57 Matrix Operator * (const Matrix & B) const58 {59 matrix ret; 60 ret. clear (); 61 for (INT I = 0; I <n; I ++) 62 for (Int J = 0; j <n; j ++) 63 {64 for (int s = 0; S <n; s ++) 65 {66 ret. mat [I] [J] = (Ret. mat [I] [J] + mat [I] [s] * B. mat [s] [J]) % m; // column J 67} 68} 69 return ret; 70} 71}; 72 matrix POW (matrix, ll t) 73 {74 matrix ret; 75 ret. clear (); 76 for (INT I = 0; I <n; I ++) 77 ret. mat [I] [I] = 1; 78 matrix TMP = A; 79 while (t) 80 {81 If (T & 1) ret = RET * TMP;
View code

 

Rapid power of the ultraviolet A 10870 recurrences Matrix

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.