POJ 3070 Matrix Mob

Source: Internet
Author: User
Tags mul

.

Matrix high-speed power idea and fast power same

#include <iostream>#include<cstdio>#include<cstring>#defineMOD 10000using namespacestd;structMatrix {intmat[2][2]; Matrix () {//constructor with the same name as the struct if the writing init () function is to be calledMemset (Mat,0,sizeof(MAT));    }};matrix mul (Matrix A, Matrix B) {matrix C;  for(intI=0; i<=1; i++) {         for(intj=0; j<=1; j + +) {             for(intk=0; k<=1; k++) {C.mat[i][j]= (C.mat[i][j]+a.mat[i][k]*b.mat[k][j])%MOD; }        }    }    returnC;} Matrix Powmul (Matrix A,intN) {matrix B; //Initialize to the unit arrayb.mat[0][0]=1; b.mat[1][1]=1;  while(n>=1) {        if(n&1) {B=Mul (b,a); } A=mul (A,a);//Self PowerN/=2; }    returnB;}intMain () {intN;  while(~SCANF ("%d",&N)) {if(n==-1) Break;         Matrix A; a.mat[0][0]=a.mat[0][1]=a.mat[1][0]=1; a.mat[1][1]=0; A=Powmul (a,n); cout<<a.mat[0][1]<<Endl; }        return 0; } 

POJ 3070 Matrix Mob

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.