Southwest min da oj (Matrix fast Power)

Source: Internet
Author: User

My name can't be so hard to rememberTime limit (normal/java): ms/3000 MS Run memory limit: 65536 KByte
Total Submissions: 16 Tested by: 9

Describe

Nirvava:hi,misaki, said ZC to leave.

Misaki: Good to go not to send, wish smooth sailing ...

Nirvana: But he left a lot of Doge for US ...

Misaki: ...

Nirvana: and they have a name, the name is as follows.

It is said that one of the reasons for ZC's departure is that nth doge always ask him if he can remember his name.

Misaki: Remember someone else's name or something, so annoying da, Zc certainly can't remember.

> Sure, it's his pet, after all. > Baffled, because he counted the doge > The spirit is polluted.

Misaki: That's a very difficult question. However, they should be able to solve.

PS: This problem is not a bit ripe ♂, too lazy to think about the surface, the previous problem surface took out.

Input

Multiple sets of inputs

One N per group (0<n<=10^9)

Output

How long is the name of Nth Doge, because it may be too long, you only need to output the result of length%1000000007 (1e9+7).

Sample input

1
2
3
4
500

Sample output

1
3
7
17
875025602

Tips

name[1]= "X"name[2]= "XXY" name[3]= "Xxyxxyx" name[4]= "Xxyxxyxxxyxxyxxxy" ... And so on                                Analysis: Set A[i] is the total length, b[i] is the number of x, C[i] is the number of Y, then there is a[i]=3*b[i-1]+c[i-1],b[i]=2*b[i-1]+c[i-1],c[i]=b[i-1]. The matrix can be constructed |0,0,0| |                                A[i-1], b[i-1],c[i-1]|*|3,2,1|=|a[i],b[i],c[i]| |1,1,0|
#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<stack>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineN 1000010using namespacestd;structmatrix{LL m[3][3];};    LL N;matrix mult (Matrix A,matrix b) {matrix C; memset (C.M,0,sizeof(C.M));  for(intI=0;i<3; i++)     for(intk=0;k<3; k++)    {        if(a.m[i][k]==0)Continue;  for(intj=0;j<3; j + +)        {            if(b.m[k][j]==0)Continue; C.M[I][J]+=a.m[i][k]*b.m[k][j]%MoD; C.M[I][J]%=MoD; }    }    returnC;}    Matrix Quickmod (Matrix A,ll n) {matrix temp; memset (temp.m,0,sizeof(temp.m));  for(intI=0;i<3; i++) temp.m[i][i]=1;  while(n) {if(n&1) temp=mult (temp,a); A=mult (a,a); N>>=1; }    returntemp;}intMain () { while(SCANF ("%lld", &n)! =EOF) {        if(n==1) {puts ("1");Continue;}        Matrix ans; memset (ANS.M,0,sizeof(ANS.M)); ans.m[0][0]=0; ans.m[0][1]=0; ans.m[0][2]=0; ans.m[1][0]=3; ans.m[1][1]=2; ans.m[1][2]=1; ans.m[2][0]=1; ans.m[2][1]=1; ans.m[2][2]=0; Ans=quickmod (ans,n-1); printf ("%lld\n", ans.m[1][0]); }}
View Code

Southwest min da oj (Matrix fast Power)

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.