bzoj2326: [HNOI2011] Math Homework

Source: Internet
Author: User

Matrix multiplication.

10^k,0,0

(f[i+1],i+1,1) = (f[i],i,1) (1, 1,0)

1.1,0)

K is the number of digits (i+1) . This is important, so every time we count to 999 ... 9, then K will be +1. So both L and R in the title are actual values of +1. (Need yy for a bit).

#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;unsignedLong LongN,mod;structMatrix {Long Longa[3][3]; Long Long*operator[] (intx) {returnA[x]; } Matrixoperator*(Matrix B) {matrix res;  for(intI=0;i<3; i++)         for(intj=0;j<3; j + +)         for(intk=0;k<3; k++) Res[i][j]= (Res[i][j]+a[i][k]*b[k][j])%MoD; returnRes; } Matrixoperator^ (Long Longe) {Matrix res (1), tmp=* This;  while(e) {if(e&1) res=res*tmp; TMP=tmp*tmp; E>>=1; }        returnRes; }        voidBuildLong Longx) {memset (A,0,sizeof(a)); a[0][0]=x%MoD;  for(intI=1;i<3; i++)         for(intj=0; j<=i;j++) A[i][j]=1; } Matrix (Long Longx=0) {memset (A,0,sizeof(a));  for(intI=0;i<3; i++) a[i][i]=x; }}cur;structVector {Long Longa[3]; Long Long&operator[] (intx) {returnA[x]; } Vectoroperator*(Matrix b) {Vector res;  for(intI=0;i<3; i++)          for(intk=0;k<3; k++) Res[i]= (Res[i]+a[k]*b[k][i])%MoD; returnRes; }        voidbuild () {memset (A,0,sizeof(a)); a[2]=1; } Vector () {memset (A,0,sizeof(a)); }}res;intMain () {scanf ("%lld%lld",&n,&MoD);    Res.build (); Long LongL,r;  for(l=1, r=Ten; r<=n;l=r,r=l*Ten) {cur.build (R); Res=res* (cur^ (Rl));    } cur.build (R); Res=res* (cur^ (n-l+1)); printf ("%lld\n", res[0]); return 0;}

bzoj2326: [HNOI2011] Math Homework

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.