2326: [HNOI2011] Math Homework

Source: Internet
Author: User
Tags mul

2326: [HNOI2011] Math homeworkDescription

Don't say anything acty.


Linear recursion is easy to write, F[i]=f[i-1]*10+i (mod)

But when n is large, you need to use matrix multiplication.

We have enough to build the matrix

10^i 0 0 f[i-1] f[i]

1 1 0 * i-1 = i

1 1 1) 1 1

#include <stdio.h>#include<iostream>#include<string.h>using namespacestd;#definell Long Longinti;ll n,m,s,a[4][4],b[4][4],c[4][4];voidMul (LL a[4][4],ll b[4][4],ll ans[4][4]){    inti,j,k; Memset (c,0,sizeof(c));  for(i=1; i<=3; i++)      for(j=1; j<=3; j + +)       for(k=1; k<=3; k++) C[i][j]= (c[i][j]+ (a[i][k]%m) * (b[k][j]%m))%m;  for(i=1; i<=3; i++)      for(j=1; j<=3; j + +) Ans[i][j]=c[i][j];}voidSolve (ll x,ll y) {y=y-x/Ten+1; memset (b,0,sizeof(b)); b[1][1]=x; b[2][1]=b[3][1]=b[2][2]=b[3][2]=b[3][3]=1;  while(y) {if(y&1) Mul (a,b,a);        Mul (B,B,B); Y>>=1; }}intMain () {CIN>>n>>m;  for(i=1; i<=3; i++) A[i][i]=1; S=Ten;  while(n>=s) {Solve (S,s-1); S*=Ten;    } solve (S,n); cout<<a[3][1]; return 0;}

2326: [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.