码迷,mamicode.com
首页 > 其他好文 > 详细

POJ - 3233 Matrix Power Series

时间:2019-02-16 09:32:46      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:std   click   closed   logs   题解   close   register   ios   blog   

传送门

 

Solution:

1.矩阵分块 题解在这里

技术图片
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #define R register
 7 #define go(i,a,b) for(R int i=a;i<=b;i++)
 8 #define yes(i,a,b) for(R int i=a;i>=b;i--)
 9 #define ll long long
10 #define db double
11 using namespace std;
12 ll n,m;int mod;
13 struct node{ll mt[3][3];};
14 node calc(node x,node y,int a,int b,int c)
15 {
16     node z;memset(z.mt,0,sizeof(z.mt));
17     go(i,1,a)
18         go(j,1,b)
19         go(k,1,c)
20         z.mt[i][j]=(z.mt[i][j]+x.mt[i][k]*y.mt[k][j])%mod;
21     return z;
22 }
23 node a,b,c;
24 void sol()
25 {
26     while(m)
27     {
28         if(m&1) b=calc(b,a,2,2,2);
29         m>>=1;a=calc(a,a,2,2,2);
30     }
31 }
32 int main()
33 {
34     scanf("%lld",&n);m=n-2;mod=1000000007;
35     if(n<=2){printf("1");return 0;}
36     a.mt[1][1]=1,a.mt[1][2]=1,a.mt[2][1]=1,a.mt[2][2]=0;
37     b.mt[1][1]=1,b.mt[1][2]=0,b.mt[2][1]=0,b.mt[2][2]=1;
38     c.mt[1][1]=1;c.mt[2][1]=1;
39     sol();
40     c=calc(b,c,2,1,2);
41     printf("%lld",c.mt[1][1]);
42     return 0;
43 }
View Code

但是这个代码有点问题 就是k=1和k=2的时候会错,是因为递推式子的原因,详见题解

但还是AC了 因为没有k=1,2这么水的数据 这里说一下我就不改了

2.分治 题解

 

没啦qwq

 

POJ - 3233 Matrix Power Series

标签:std   click   closed   logs   题解   close   register   ios   blog   

原文地址:https://www.cnblogs.com/forward777/p/10386687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!