链接:传送门

题意:输出第 n 年向上小三角形的个数 % 10^9 + 7

思路:

  • 设 Fn 为第 n 年向上小三角形的个数,经过分析可以得到 Fn = 3 * Fn-1 + ( 4^(n-1) - Fn-1 ),根据这个递推式可以用矩阵快速幂来解决。

  • 下面三个矩阵设为矩阵 a ,b ,ans

    • 矩阵 a:
    2 1
    0 4
    • 矩阵 b:
    Fn-1 0
    4^(n-1) 0
    • 矩阵 ans:
    Fn 0
    4^n 0
    • 这样就可以表示出 上方递推关系了 ,所以 ans = Matrixpow( a, n-1 ) * b( n > 1 )

balabala:看来矩阵快速幂一般和递推关系相结合呀~


/*************************************************************************
> File Name: codeforces185At2.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月03日 星期三 19时42分09秒
************************************************************************/ #include<bits/stdc++.h>
using namespace std; const int MOD = 1000000007;
const int maxn = 3;
#define ll long long
#define mod(x) ((x)%MOD) struct mat{
int m[maxn][maxn];
}unit; mat operator *(mat a,mat b){
mat ret;
ll x;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
x = 0;
for(int k=0;k<2;k++)
x += mod( (ll)a.m[i][k]*b.m[k][j] );
ret.m[i][j] = x;
}
}
return ret;
}
mat pow_mat(mat a,ll x){
mat ret = unit;
while(x){
if(x&1) ret = ret*a;
a = a*a;
x >>= 1;
}
return ret;
}
void init_unit(){
for(int i=0;i<2;i++) unit.m[i][i] = 1;
return;
} mat a,b;
void init(){
memset(a.m,0,sizeof(a.m));
memset(b.m,0,sizeof(b.m));
a.m[0][0] = 2; a.m[0][1] = 1; a.m[1][1] = 4;
b.m[0][0] = 3; b.m[1][0] = 4;
}
int main(){
init_unit();
init();
ll n;
while(cin>>n){
if(n==0) cout<<"1"<<endl;
else if(n==1) cout<<"3"<<endl;
else{
mat ans = pow_mat(a,n-1);
ans = ans*b;
cout<< mod(ans.m[0][0]) <<endl;
}
}
return 0;
}

Codeforces 185A Plant( 递推关系 + 矩阵快速幂 )的更多相关文章

  1. CodeForces - 691E Xor-sequences 【矩阵快速幂】

    题目链接 http://codeforces.com/problemset/problem/691/E 题意 给出一个长度为n的序列,从其中选择k个数 组成长度为k的序列,因为(k 有可能 > ...

  2. HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...

  3. Codeforces 691E题解 DP+矩阵快速幂

    题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...

  4. Codeforces 691E Xor-sequences(矩阵快速幂)

    You are given n integers a1,  a2,  ...,  an. A sequence of integers x1,  x2,  ...,  xk is called a & ...

  5. Codeforces 954 dijsktra 离散化矩阵快速幂DP 前缀和二分check

    A B C D 给你一个联通图 给定S,T 要求你加一条边使得ST的最短距离不会减少 问你有多少种方法 因为N<=1000 所以N^2枚举边数 迪杰斯特拉两次 求出Sdis 和 Tdis 如果d ...

  6. Plant (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/185/A 题目: Dwarfs have planted a very interesting plant ...

  7. hdu 4602 递推关系矩阵快速幂模

    Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. uva 10870 递推关系矩阵快速幂模

    Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...

  9. CodeForces 185A. Plant (矩阵快速幂)

    CodeForces 185A. Plant (矩阵快速幂) 题意分析 求解N年后,向上的三角形和向下的三角形的个数分别是多少.如图所示: N=0时只有一个向上的三角形,N=1时有3个向上的三角形,1 ...

随机推荐

  1. 微信小程序:获取地理定位和显示相应的城市名称。

    最近在看微信小程序,遇到地理定位显示城市名称的问题.本文就是记录这一过程. 解决方案                                                          ...

  2. PHP回顾之协程

    转载请注明文章出处: https://tlanyan.me/php-review... PHP回顾系列目录 PHP基础 web请求 cookie web响应 session 数据库操作 加解密 Com ...

  3. 邓_ php SESSION

    学会php session可以在很多地方使用,比如做一个后台登录的功能,要让程序记住用户的session,其实很简单,看了下面的文章你就明白了. PHP session用法其实很简单它可以把用户提交的 ...

  4. anaconda jupyter

    本文主要讲解在Ubuntu系统中,如何在Anaconda下安装TensorFlow以及配置Jupyter Notebook远程访问的过程. 在官方文档中提到,TensorFlow的安装主要有以下五种形 ...

  5. nyoj 42判断欧拉路径模板题

    #include<stdio.h> #include<string.h> #define N 2100 int degree[N]; int pre[N];//很长时间没写欧拉 ...

  6. json_encode把中文字符的数组转为json格式

    function ch_json_encode($data) { /** * 将中文编码 * @param array $data * @returnstring */ function ch_url ...

  7. [Angular] ngx-formly (AKA angular-formly for Angular latest version)

    In our dynamic forms lessons we obviously didn’t account for all the various edge cases you might co ...

  8. [Node.js] Manage Configuration Values with Environment Variables

    Storing configuration in files instead of the environment has many downsides, including mistakenly c ...

  9. [HTML5] Accessibility Implementation for complex component

    When you developing a complex component by your own, one thing you cannot ignore is Accessibility. C ...

  10. emacs使用本地emacs server模式打开远程文件

    使用emacs的用户都知道,一般要打开远程机器上的文件要使用TrampMode模式,调用方式例如以下: C-x C-f /remotehost:filename RET (or /method:use ...