链接:传送门

题意:输出第 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. KMP算法(推导方法及模板)

    介绍 克努斯-莫里斯-普拉特算法Knuth-Morris-Pratt字符串查找算法(简称为KMP算法)可在一个主文本字符串S内查找一个词W的出现位置.此算法通过运用对这个词在不匹配时本身就包含足够的信 ...

  2. web前端对文件的引用规则

    web前端一般常用文件 .html .css .js.但是当用css文件和html引入资源(比如图片)时,路径可能不相同.下面总结了几条. 使用相对路径引入规则: html或者js引入图片,按照htm ...

  3. Linux中的gpio口使用方法

    Linux中的IO使用方法 应该是新版本内核才有的方法.请参考:./Documentation/gpio.txt文件 提供的API:驱动需要包含 #include <linux/gpio.h&g ...

  4. Problem 8

    Problem 8 # Problem_8.py """ The four adjacent digits in the 1000-digit number that h ...

  5. Java 超类引用子类对象的示例代码

    动态方法分配 dynamic method dispatch 一个被重写的方法的调用会在运行时解析,而不是编译时解析 Java 会根据在调用发生时引用的对象的类型来判断所要执行的方法 public c ...

  6. js获取日期当天的开始时间和结束时间

    //函数调用传参格式为 2018-6-6或者2018.6.6//如:startUnix(2018-6-6) 返回的时间戳格式‘1528300799’ function startUnix($date) ...

  7. 【hiho一下第十二周】刷油漆

    [题目链接]:http://hihocoder.com/problemset/problem/1055 [题意] [题解] 设f[x][i]表示以第x个节点为根的子树; 不选x这个节点,然后子树里面选 ...

  8. G - Arctic Network

    G - Arctic Network   #include<cmath> #include<cstdio> #include<cstring> #include&l ...

  9. A - Jungle Roads

    A - Jungle Roads 思路:并查集的板子,重点是字符的转换,不能忘了加上1. #include<cmath> #include<cstdio> #include&l ...

  10. oracle常见sql语句优化

    1.* 号引起的运行效率 尽量降低使用select * 来进行查询,当你查询使用*, 数据库会进行解析并将*转换为所有列. select count(si.student_id)  from Stud ...