HDU 1757 matrix multiplication, fast power template problem

Source: Internet
Author: User
Tags mul

HDU 1757

Test Instructions : If x < Ten, f (x) = x;

If x >=, f (x) = a0 * F (X-1) + A1 * F (x-2) + A2 * F (x-3) + ... + A9 * F (x-10);

Give the K and MoD and ask F (k).

Summary : 1 , especially note that matrix multiplication does not satisfy the commutative law, that is, a*b! = b*a.   2 , the sense-push equation is a bit difficult. 3 , matrix initialization note.


F (x-10)    0 0 0 0 0 0 0 0 0        (First matrix )       
F (x-9)     0 0 0 0 0 0 0 0 0
F (x-8)     0 0 0 0 0 0 0 0 0
F (x-7)     0 0 0 0 0 0 0 0 0
F (x-6)     0 0 0 0 0 0 0 0 0
F (x-5)    ;  0 0 0 0 0 0 0 0 0
F (x-4)     0 0 0 0 0 0 0 0 0
F (x-3)     0 0 0 0 0 0 0 0 0
F (x-2)     0 0 0 0 0 0 0 0 0
F (x-1)     0 0 0 0 0 0 0 0 0

*

  0 1 0 0 0 0 0 0 0 0 (temp matrix)
  0 0 1 0 0 0 0 0 0 0
  0 0 0 1 0 0 0 0 0 0
  0 0 0 0 1 0 0 0 0 0
&N Bsp 0 0 0 0 0 1 0 0 0 0
  0 0 0 0 0 0 1 0 0 0
  0 0 0 0 0 0 0 1 0 0
  0 0 0 0 0 0 0 0 1 0
  0 0 0 0 0 0 0 0 0 1

A9 8 7 6 5 4 3 2 1 0 (this line is AI)

=

F (x-9) 0 0 0 0 0 0 0 0 0
F (x-8) 0 0 0 0 0 0 0 0 0
F (x-7) 0 0 0 0 0 0 0 0 0
F (x-6) 0 0 0 0 0 0 0 0 0
F (x-5) 0 0 0 0 0 0 0 0 0
F (x-4) 0 0 0 0 0 0 0 0 0
F (x-3) 0 0 0 0 0 0 0 0 0
F (x-2) 0 0 0 0 0 0 0 0 0
F (x-1) 0 0 0 0 0 0 0 0 0
F (x) 0 0 0 0 0 0 0 0 0

#include <iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<map>#include<bitset>#include<vector>#include<Set>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")#defineF (I,A,B) for (int i=a;i<b;i++)#defineFF (I,A,B) for (int i=a;i<=b;i++)#defineMes (A, b) memset (A,b,sizeof (a))#defineINF 0x3f3f3f3ftypedefLong Longll;Const intN = 1e5+Ten, MAXN = the; ll K,m= -;structmat{ll MAT[MAXN][MAXN]; Mat () {Mes (Mat,0); F (i,0, MAXN) mat[i][i]=1; }} E; Mat First, temp; Matoperator*(Mat A, Mat b) {mat ans; memset (Ans.mat,0,sizeof(Ans.mat));  for(inti =0; i < MAXN; i++)         for(intj =0; J < Maxn; J + +)             for(intK =0; K < MAXN; k++) Ans.mat[i][j]= (Ans.mat[i][j] + a.mat[i][k] * b.mat[k][j])%m; returnans;} Matoperator^(Mat a,ll x) {mat P= E, q =A;  while(x) {if(x&1) p = p*Q; X>>=1; Q= q*Q; }    returnp;}intMain () {mes (First.mat,0); F (i,0,Ten) first.mat[i][0]=i;  while(~SCANF ("%lld%lld", &k, &m) {mes (Temp.mat,0); FF (i,0,8) temp.mat[i][i+1]=1; F (i,0,Ten) {scanf ("%lld", &temp.mat[9][9-i]); }        if(k<Ten) {printf ("%lld\n", K); Continue; } Mat ans= (temp^ (k-9)) *first;//notice, the order here is not reversed.printf"%lld\n", ans.mat[9][0]); }    return 0;}
View Code

Matrix templates

Const intMOD, MAXN;//mod for remainder, MAXN as matrix rangestructmat{ll MAT[MAXN][MAXN]; //open a long longMat () {Mes (Mat,0); F (i,0, MAXN) mat[i][i]=1;//the diagonal is initialized to 1, the other 0}} E; //Unit MatrixMat First, temp; Matoperator* (Mat A, Mat b)//overloading *, especially note that matrix multiplication does not satisfy the commutative law, i.e. a*b! = B*a{Mat ans; memset (Ans.mat,0,sizeof(Ans.mat));  for(inti =0; i < MAXN; i++)         for(intj =0; J < Maxn; J + +)             for(intK =0; K < MAXN; k++) Ans.mat[i][j]= (Ans.mat[i][j] + a.mat[i][k] * b.mat[k][j])%MOD; returnans;} Matoperator^ (Mat a,ll x)////Heavy Duty ^{Mat P= E, q =A;  while(x) {if(x&1) p = p*Q; X>>=1; Q= q*Q; }    returnp;} Mat Mul (Mat A,mat b)//function *{Mat ans; inti,j,k;  for(i=0; i<maxn;i++){         for(j=0; j<maxn;j++) {Ans.mat[i][j]=0;  for(k=0; k<maxn;k++) {Ans.mat[i][j]+=a.mat[i][k]*b.mat[k][j]%m; ANS.MAT[I][J]%=m; }        }    }    returnans;} Mat Pow_mod (Mat S,intb) {//function RadicalMat ans;  for(intI=0; i<maxn; ++i) ans.mat[i][i]=1;  while(b) {if(b&1) ans=Mul (ans, s); S=Mul (S, s); b>>=1; }    returnans;}voidPrmat (Mat a)//Print Matrix{    inti,j;  for(i=0; i<maxn;i++)    {         for(j=0; j<maxn;j++) printf ("%d", A.mat[i][j]); Puts (""); } puts (""); return ;}

HDU 1757 matrix multiplication, fast power template problem

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.