[HDU 1757] A Simple Math Problem (matrix fast power)

Source: Internet
Author: User

[HDU 1757] A Simple Math Problem (matrix fast power)

Question:


If x <10 f (x) = x.
If x> = 10 f (x) = a0 * f (x-1) + a1 * f (X-2) + a2 * f (X-3) + ...... + A9 * f (X-10 );
And ai (0 <= I <= 9) can only be 0 or 1.
For k and m, evaluate f (k) % m.

Solution:

F (x) = a0 * f (x-1) + a1 * f (X-2) + a2 * f (X-3) + ...... + A9 * f (X-10)

K is 10 ^ 9 orders of magnitude, so it cannot be done in recursive mode. This type of question can be done by constructing a matrix and using a matrix's rapid power.


The constructed matrix is:

| 010 ...... 0 | | F0 | | F1 |
| 0010 ...... 0 | | F1 | | F2 |
| ...... 1 |* |... | =|... |
| A9A8... A0 | | F9 | | F10 |


Code:

/* ID: wuqi9395@126.comPROG: LANG: C ++ */# include# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include
            
              # Include # define INF (1 <30) # define PI acos (-1.0) # define mem (a, B) memset (a, B, sizeof ()) # define For (I, n) for (int I = 0; I <n; I ++) typedef long ll; using namespace std; const int maxn = 20; const int maxm = 20; ll mod, k; struct Matrix {int n, m; int a [maxn] [maxm]; void clear () {n = m = 0; memset (a, 0, sizeof (a);} Matrix operator * (const Matrix & B) const {// implement Matrix multiplication Matrix tmp; tmp. clear (); tmp. n = n; tmp. m = B. m; for (int I = 0; I <n; I ++) for (int j = 0; j <B. m; j ++) for (int k = 0; k <m; k ++) {tmp. a [I] [j] + = a [I] [k] * B. a [k] [j]; tmp. a [I] [j] % = mod;} return tmp ;}}; Matrix A, B; void init () {. clear (); // matrix A is the constructed matrix. n =. m = 10; for (int I = 0; I <9; I ++). a [I] [I + 1] = 1; B. clear (); B. n = 10; B. m = 1; // matrix B is the first 10 count of f (x) for (int I = 0; I <10; I ++) B. a [I] [0] = I;} Matrix Matrix_pow (Matrix A, ll k, ll mod) {// Matrix fast power Matrix res; res. clear (); res. n = res. m = 10; for (int I = 0; I <10; I ++) res. a [I] [I] = 1; while (k) {if (k & 1) res = A * res; k >>=1; A = A * ;} return res;} int main () {init (); while (cin> k> mod) {int x; for (int I = 0; I <10; I ++) {scanf ("% d", & x);. a [9] [9-I] = x;} if (k <10) {printf ("% lld \ n", k % mod );} else {Matrix res = Matrix_pow (A, k-9, mod); res = res * B; cout <
             
              


Related Article

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.