题意:给定一个大三角形,然后每次按照图片分割成4个小三角形,问n次后有多少个向上的三角形。
思路:矩阵快速幂,可以发现,每一个向上的的可以在下一次产生3个向上的,1个向下的,向下的每次可以产生一个向上的和三个向下的。这刚好是矩阵的应用。注意0的情况。
code:
#include <bits/stdc++.h>
using namespace std;typedef long long ll;
const ll mod=1e9+7;struct node
{ll v[5];node (){v[1]=3;v[2]=1;v[3]=1;v[4]=3;}node mul(node t){node p;p.v[1]=(v[1]*t.v[1]+v[2]*t.v[3])%mod;p.v[2]=(v[1]*t.v[2]+v[2]*t.v[4])%mod;p.v[3]=(v[3]*t.v[1]+v[4]*t.v[3])%mod;p.v[4]=(v[3]*t.v[2]+v[4]*t.v[4])%mod;return p;}void print(){cout<<v[1]<<" "<<v[2]<<endl;cout<<v[3]<<" "<<v[4]<<endl;}
};node mi(node k,ll n)
{if (n==1) return k;node t=mi(k,n/2);//t.print();t=t.mul(t);// t.print();if (n%2==1) t=t.mul(k);return t;
}int main()
{ll n;cin>>n;if (n==0) {puts("1");return 0;}node t;t=mi(t,n);cout<<t.v[1]%mod<<endl;
}