D
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 const int md=1e9+7;
6 struct matr
7 {
8 int m[3][3];
9 }a;
10 int n;
11
12 matr mul(matr a,matr b)
13 {
14 matr c;
15 for (int i=0;i<3;i++)
16 for (int j=0;j<3;j++) {
17 c.m[i][j]=0;
18 for (int k=0;k<3;k++) {
19 c.m[i][j]+=(long long)a.m[i][k]*b.m[k][j]%md;
20 c.m[i][j]%=md;
21 }
22 }
23 return c;
24 }
25
26 matr qsm(matr a,int b)
27 {
28 matr s;
29 for (int i=0;i<3;i++)
30 for (int j=0;j<3;j++) {
31 s.m[i][j]=(i==j);
32 }
33 while (b) {
34 if (b&1) s=mul(s,a);
35 a=mul(a,a);
36 b>>=1;
37 }
38 return s;
39 }
40
41 int baoli(int n)
42 {
43 int q=0,r=0;
44 for (int i=1;i<=n;i++) {
45 int _q=q,_r=r;
46 q=(2*_r+1)%md;
47 r=(q+_q+1)%md;
48 }
49 return q%md;
50 }
51
52 int main()
53 {
54 // freopen("hanoi.in","r",stdin);
55 // freopen("hanoi.out","w",stdout);
56 a.m[0][0]=0; a.m[0][1]=2; a.m[0][2]=1;
57 a.m[1][0]=1; a.m[1][1]=2; a.m[1][2]=2;
58 a.m[2][0]=0; a.m[2][1]=0; a.m[2][2]=1;
59 while (~scanf("%d",&n)) {
60 matr b=qsm(a,n);
61 printf("%d\n",b.m[0][2]%md);
62 printf("%d\n",baoli(n));
63 }
64 return 0;
65 }
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 const int md=1e9+7;
6 struct matr
7 {
8 int m[3][3];
9 }a;
10 int n;
11
12 matr mul(matr a,matr b)
13 {
14 matr c;
15 for (int i=0;i<3;i++)
16 for (int j=0;j<3;j++) {
17 c.m[i][j]=0;
18 for (int k=0;k<3;k++) {
19 c.m[i][j]+=(long long)a.m[i][k]*b.m[k][j]%md;
20 c.m[i][j]%=md;
21 }
22 }
23 return c;
24 }
25
26 matr qsm(matr a,int b)
27 {
28 matr s;
29 for (int i=0;i<3;i++)
30 for (int j=0;j<3;j++) {
31 s.m[i][j]=(i==j);
32 }
33 while (b) {
34 if (b&1) s=mul(s,a);
35 a=mul(a,a);
36 b>>=1;
37 }
38 return s;
39 }
40
41 int baoli(int n)
42 {
43 int q=0,r=0;
44 for (int i=1;i<=n;i++) {
45 int _q=q,_r=r;
46 q=(2*_r+1)%md;
47 r=(q+_q+1)%md;
48 }
49 return q%md;
50 }
51
52 int main()
53 {
54 // freopen("hanoi.in","r",stdin);
55 // freopen("hanoi.out","w",stdout);
56 a.m[0][0]=0; a.m[0][1]=2; a.m[0][2]=1;
57 a.m[1][0]=1; a.m[1][1]=2; a.m[1][2]=2;
58 a.m[2][0]=0; a.m[2][1]=0; a.m[2][2]=1;
59 while (~scanf("%d",&n)) {
60 matr b=qsm(a,n);
61 printf("%d\n",b.m[0][2]%md);
62 printf("%d\n",baoli(n));
63 }
64 return 0;
65 }