Codeforces Round #118 (Div. 1) A. Plant
原创
©著作权归作者所有:来自51CTO博客作者Nemaleswang的原创作品,请联系作者获取转载授权,否则将追究法律责任
题目链接:Plant
题目大意:给你一些图形,问第n个图形里面有几个上三角形
题目思路:下一张图的上三角形和下三角形个数可以由上一张图得到,具体为upn=3∗upn−1+downn−1和downn=3∗downn−1+upn−1,然后构造矩阵就好了
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll maxn = 2;
const ll MOD = 1e9+7;
#define mod(x) ((x)%MOD)
struct mat{
ll m[maxn][maxn];
}unit;
mat operator *(mat a,mat b){
mat ret;
ll x;
for(ll i = 0;i < maxn;i++){
for(ll j = 0;j < maxn;j++){
x = 0;
for(ll k = 0;k < maxn;k++)
x += mod((ll)a.m[i][k]*b.m[k][j]);
ret.m[i][j] = mod(x);
}
}
return ret;
}
void init_unit(){
for(ll i = 0;i < maxn;i++)
unit.m[i][i] = 1;
return ;
}
mat pow_mat(mat a,ll n){
mat ret = unit;
while(n){
if(n&1) ret = ret*a;
a = a*a;
n >>= 1;
}
return ret;
}
ll quick_pow(ll a,ll n){
ll ans = 1;
while(n){
if(n&1) ans = (ans*a)%MOD;
a = (a*a)%MOD;
n >>= 1;
}
return ans;
}
int main(){
ll n;
init_unit();
while(~scanf("%lld",&n)){
if(n == 0ll) puts("1");
else if(n == 1ll) puts("3");
else{
mat a,b;
b.m[0][0] = 3,b.m[0][1] = 1;
b.m[1][0] = 1,b.m[1][1] = 3;
a.m[0][0] = 3,a.m[0][1] = 1;
b = pow_mat(b,n-1);
a = a*b;
printf("%lld\n",(a.m[0][0]+MOD)%MOD);
}
}
return 0;
}