Quick power (including second-order square matrix) template

Source: Internet
Author: User


Pow: *** should be added to the POW of a namespace ::****

 

1 namespace POW {
2 typedef int t; // you can change "int" to the Data Type stored in the matrix.
3 const t mod = T (1e9 + 7); // change to the modulus required by the fast power.
4
5 template <class T>
6 T powmod (t a, int N, t mod ){
7 t ans =;
8 -- N;
9 While (n ){
10 if (N & 1) ans = ans * A % MOD;
11 A = A * A % MOD;
12 N> = 1;
13}
14 return ans;
15}
16 // powmod (t a, int N, t mod)
17
18 struct mat {
19 t a, B, c, d; // you can change it to a two-dimensional array and modify the following function if necessary.
20 MAT (t w, t x, t y, t Z): A (W), B (X), C (Y), D (z ){}
21 mat operator * (MAT & B) const {
22 t w, x, y, z;
23 W = (LL) a * B. a) % mod + (LL) B * B. C) % mod) % MOD;
24 x = (LL) a * B. B) % mod + (LL) B * B. D) % mod) % MOD;
25 y = (LL) C * B. a) % mod + (LL) D * B. C) % mod) % MOD;
26 z = (LL) C * B. B) % mod + (LL) D * B. D) % mod) % MOD;
27 return MAT (w, x, y, z );
28}
29}; // struct mat
30
31} // namespace pow

Quick power (including second-order square matrix) template

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.