BZOJ 2326 HNOI2011 數學作業 矩陣乘法

來源:互聯網
上載者:User

標籤:bzoj   bzoj2326   矩陣乘法   

題目大意:求1234567891011121314...n mod m 的值

設F(n)=1234567891011121314...n 那麼顯然有F(n)=F(n-1)*(floor(lgn)+1)+n

於是我們可以矩乘

將數字按照floor(lgn)+1分類

構造狀態矩陣F(n) n+1 1 初值為0 1 1

1~9的轉移矩陣為

10 0 0
1 1 0
0 1 1

10~99的轉移矩陣為

100 0 0

1 1 0

0 1 1

以此類推

注意構造矩陣的時候要模數不然會掛

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef unsigned long long ll;struct Matrix{ll xx[4][4];Matrix(int flag){int i,j;for(i=1;i<=3;i++)for(j=1;j<=3;j++)if(i==j) xx[i][j]=flag;else xx[i][j]=0;}Matrix(bool,ll digit){xx[1][1]=digit;xx[2][1]=xx[2][2]=xx[3][2]=xx[3][3]=1;xx[1][3]=xx[1][2]=xx[2][3]=xx[3][1]=0;}ll* operator [] (int x){return xx[x];}}ans(1);ll n,p;void operator *= (Matrix &x,Matrix y){int i,j,k;Matrix z(0);for(i=1;i<=3;i++)for(j=1;j<=3;j++)for(k=1;k<=3;k++)z[i][j]+=x[i][k]*y[k][j],z[i][j]%=p;x=z;}Matrix Quick_Power(Matrix x,ll y){Matrix re(1);while(y){if(y&1) re*=x;x*=x;y>>=1;}return re;}int main(){ll i;cin>>n>>p;for(i=10;i<=n;i*=10)ans*=Quick_Power(Matrix(true,i%p),i-i/10);ans*=Quick_Power(Matrix(true,i%p),n-i/10+1);cout<<(ans[2][1]+ans[3][1])%p<<endl;}


BZOJ 2326 HNOI2011 數學作業 矩陣乘法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.