Poj3734blocks Matrix fast Power plus DP thought

Source: Internet
Author: User
Tags mul

#include <cstdio>#include <algorithm>#include <iostream>#include <vector>using namespace STD;typedef  vector<int>Vectypedef  vector<vec>MatConst intm=10007;typedef Long LongLl;mat Mul (Mat &a,mat &b) {Mat C (A.size (), VEC (b[0].size ())); for(inti =0; i < a.size (); i++) { for(intK =0; K < B.size (); k++) { for(intj =0; J < b[0].size (); j + +) {C[i][j]+=a[i][k]*b[k][j];            C[I][J]%= M; }        }    }returnC;} Mat Fun (Mat a,ll N) {Mat B (A.size (), VEC (A.size ())); for(inti =0; i < a.size (); i + +) {B[i][i] =1; } while(N >0){if(n&1) B=mul (b,a);        A = Mul (a,a); N >>=1; }returnB;}intMain () {intTCin>> T; while(t--) {intNCin>> N; Mat A (4, VEC (4)); a[0][0]=2; a[0][1]=1; a[0][2]=1; a[0][3]=0; a[1][0]=1; a[1][1]=2; a[1][2]=0; a[1][3]=1; a[2][0]=1; a[2][1]=0; a[2][2]=2; a[2][3]=1; a[3][0]=0; a[3][1]=1; a[3][2]=1; a[3][3]=2; A = Fun (a,n);cout<< a[0][0] << Endl; }return 0;}

Dp[i][0] On behalf of I a red and green are even
Dp[i][1] represents I a red block is an odd green block is even
Dp[i][2] represents I a red block is even green block is odd
DP[I][3] On behalf of I a red and green are all odd
This state can be turned out of a matrix and then the matrix is quickly idempotent

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Poj3734blocks Matrix fast Power plus DP thought

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.