Bzoj2326: [hnoi2011] mathematical assignments

Source: Internet
Author: User
2326: [hnoi2011] Time Limit: 10 sec memory limit: 128 MB
Submit: 955 solved: 535
[Submit] [Status] Description

Question:

It's amazing to know that Matrix Multiplication can be used like this for the first time!

I have come up with the recursive formula:

F [I] = (F [I-1] * 10 ^ Len [I] + I) mod p

But I can't solve this with matrix multiplication. f [I] = A * f [I-1] + B, but both A and B change with I.

The quantile number can be used to remove the changes of A, but B cannot be solved.

After reading the question, I found that this is still the case!

Adds a matrix to one dimension.

(F [I-1], I-1, 1) * (10 ^ Len (I), 0 0 = (F [I], I, 1)

1, 1, 0

1, 1)

It's amazing! The original matrix can not only quickly solve the linear recursive formula, but also solve this problem. You only need to add the changed quantity to the Matrix Expression together.

Digress:
I don't want to write the rapid power of the C ++ matrix, so I have to go to Pascal...

Note the boundary and write it again.

The matrix satisfies the combination law and does not satisfy the exchange law.

Code:

 1 type matrix=array[1..3,1..3] of int64; 2 var a,b,c:matrix; 3     m:array[0..18] of int64; 4     i,j:longint; 5     cs,n,p:int64; 6     s:string; 7 operator *(a,b:matrix)c:matrix; 8  var i,j,k:longint; 9  begin10    fillchar(c,sizeof(c),0);11    for i:=1 to 3 do12     for j:=1 to 3 do13      for k:=1 to 3 do14       c[i,j]:=(c[i,j]+(a[i,k]*b[k,j] mod p)) mod p;15  end;16 procedure main;17  begin18    m[0]:=1;19    for i:=1 to 18 do m[i]:=m[i-1]*10;20    readln(n,p);str(n,s);21    fillchar(b,sizeof(b),0);22    for i:=1 to 3 do b[i,i]:=1;23    for i:=1 to length(s) do24     begin25      if i<>length(s) then cs:=m[i]-m[i-1] else cs:=n-m[length(s)-1]+1;26      a[1,1]:=m[i] mod p;a[1,2]:=0;a[1,3]:=0;27      a[2,1]:=1;a[2,2]:=1;a[2,3]:=0;28      a[3,1]:=1;a[3,2]:=1;a[3,3]:=1;29      while cs>0 do30       begin31        if cs and 1=1 then b:=b*a;32        cs:=cs>>1;33        a:=a*a;34       end;35     end;36    writeln(b[3,1]);37  end;38 39 begin40   assign(input,‘input.txt‘);assign(output,‘output.txt‘);41   reset(input);rewrite(output);42   main;43   close(input);close(output);44 end.   
View code

 

 

Bzoj2326: [hnoi2011] mathematical assignments

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.