Codeforces 185A Plant( 递推关系 + 矩阵快速幂 )
链接:传送门
题意:输出第 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( 递推关系 + 矩阵快速幂 )的更多相关文章
- CodeForces - 691E Xor-sequences 【矩阵快速幂】
题目链接 http://codeforces.com/problemset/problem/691/E 题意 给出一个长度为n的序列,从其中选择k个数 组成长度为k的序列,因为(k 有可能 > ...
- HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...
- Codeforces 691E题解 DP+矩阵快速幂
题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...
- Codeforces 691E Xor-sequences(矩阵快速幂)
You are given n integers a1, a2, ..., an. A sequence of integers x1, x2, ..., xk is called a & ...
- Codeforces 954 dijsktra 离散化矩阵快速幂DP 前缀和二分check
A B C D 给你一个联通图 给定S,T 要求你加一条边使得ST的最短距离不会减少 问你有多少种方法 因为N<=1000 所以N^2枚举边数 迪杰斯特拉两次 求出Sdis 和 Tdis 如果d ...
- Plant (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/185/A 题目: Dwarfs have planted a very interesting plant ...
- hdu 4602 递推关系矩阵快速幂模
Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- uva 10870 递推关系矩阵快速幂模
Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...
- CodeForces 185A. Plant (矩阵快速幂)
CodeForces 185A. Plant (矩阵快速幂) 题意分析 求解N年后,向上的三角形和向下的三角形的个数分别是多少.如图所示: N=0时只有一个向上的三角形,N=1时有3个向上的三角形,1 ...
随机推荐
- asyncio 自动跳出长时间堵塞的 task
https://www.cnblogs.com/ywhyme/p/10660411.html 的升级版 可以知道当前是卡在哪一个 task 甚至是多少行 import asyncio import o ...
- nyoj314-斐波那契数列四吧
斐波那契数列四吧 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 斐波那契数列为:0,1,1,2,3,5,8,13....,常规递推公式为f(n)=f(n-1)+f(n- ...
- MySQL导入到SQLServer
Mysql是在Linux环境服务器,MSSQL在windows服务器上 1.在MSServer中安装VPN 2.为VPN配置Mysql服务器账号 3.账号中的文件 4.在MSSQL服务器上安装mysq ...
- vue-cli3.0 搭建项目
1.首先我们先在安装好node node用于npm安装[自行百度] 2.全局安装vue 通过npm命令安装vue.js 在用vue.js在用于构建大型的应用时推荐使用npm安装,npm能很好的和w ...
- 02023_Arrays类的方法练习
1.定义一个方法,接收一个数组,数组中存储10个学生考试分数,该方法要求返回考试分数最低的后三名考试分数. public static int[] method(double[] arr){ Arra ...
- (3)Spring Boot热部署【从零开始学Spring Boot】
在编写代码的时候,你会发现我们只是简单把打印信息改变了下,就需要重新部署,如果是这样的编码方式,那么我们估计一天下来之后就真的是打几个Hello World之后就下班了.那么如何解决热部署的问题呢?那 ...
- WinSCP介绍、安装、使用
前言 如果说XManager通过Xshell.Xftp可以很方便的进行远程管理,那么PuTTY显然不能满足我们的需求,所以这也是今天要介绍的另外一个工具-WinSCP. 简介 WinSCP是一个Win ...
- OJ刷题---手机尾号评分
题目要求: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dis ...
- struts自己定义拦截器--登录权限控制
说明:该自己定义的拦截器实现用户登录的权限控制. login.jsp--->LoginAction--重定向-->MainAction--->main.jsp 一.1.整体的步骤: ...
- USACO 2.2 Runaround Numbers
Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81 ...