http://www.lightoj.com/volume_showproblem.php?problem=1065

题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - 1) +f(n - 2) 求f(n)

思路:给出了递推式就是水题。

/** @Date    : 2016-12-17-15.54
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;
struct matrix
{
LL mat[2][2];
void init()
{
mat[0][0] = mat[1][0] = mat[0][1] = mat[1][1] = 0;
}
}; matrix mul(matrix a, matrix b, LL mod)
{
matrix c;
c.init();
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
{
c.mat[i][j] += a.mat[i][k] * b.mat[k][j];
c.mat[i][j] %= mod;
}
return c;
} matrix fpow(matrix x, LL n, LL mod)
{
matrix r;
r.init();
for(int i = 0; i < 2; i++)
r.mat[i][i] = 1;
while(n > 0)
{
if(n & 1)
r = mul(r, x, mod);
x = mul(x, x, mod);
n >>= 1;
}
return r;
} LL Tis(LL x, LL a, LL b, LL mod)
{
matrix t;
t.init();
t.mat[0][0] = 1;
t.mat[0][1] = 1;
t.mat[1][0] = 1;
matrix s;
s = fpow(t, x - 1, mod);
LL ans = s.mat[0][0]*b + s.mat[1][0]*a;
return ans % mod;
}
int main()
{
int T;
cin >> T;
int cnt = 0;
while(T--)
{
LL n, a, b, m;
scanf("%lld%lld%lld%lld", &a, &b, &n, &m);
LL x = 1;
while(m--)
{
x*=10;
}
printf("Case %d: %lld\n",++cnt, Tis(n, a, b, x));
}
return 0;
}

LightOJ 1065 - Number Sequence 矩阵快速幂水题的更多相关文章

  1. UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  2. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  3. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  4. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  5. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  6. HDU - 1005 -Number Sequence(矩阵快速幂系数变式)

    A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...

  7. Yet another Number Sequence 矩阵快速幂

    Let’s define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n ...

  8. SDUT1607:Number Sequence(矩阵快速幂)

    题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...

  9. Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)

    题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...

随机推荐

  1. 对首师大研究生院的UI分析

    我们分析的是首都师范大学研究生院的UI界面 网站链接http://grad.cnu.edu.cn/index.htm 学校设计和石家庄铁道大学的界面类似,都是有一个大的置顶的名字,将学校或者学院的名字 ...

  2. Ubuntu16.04安装json-c

    1. 安装依赖 sudo apt-get install git gcc clang libtool autoconf automake doxygen valgrind 一些版本要求,如果版本过低可 ...

  3. MySQL 事物机制

    前言:事物:一组原子性的SQL查询,或多个SQL语句组成了一个独立的单元.要么这一组SQL语句全部执行,要么全部不执行 (一)事物日志介绍:管理事物机制的日志 redo日志:记录SQL执行的语句,这些 ...

  4. Struts2(二)

    以下内容是基于导入struts2-2.3.32.jar包来讲的 1.关于StrutsPrepareAndExecuteFilter 启动StrutsPrepareAndExecuteFilter时加载 ...

  5. 将oracle数据库表使用命令的形式导入到excle文件中 亲测可用!

    main.sql 中的代码 set markup html on entmap ON spool on preformat off spool D:\新建文件夹\mick\tables.xls @ge ...

  6. Node.js系列——(2)发起get/post请求

    服务器与浏览器的交互主要方式有get/post请求. 下面,我们来看一下node.js发起get/post请求. 1.get 由于get请求的参数在url后面,所以相对比较简单.node.js中的ur ...

  7. .net下使用NPOI读取Excel表数据

    这里只写MVC下的情况 public ActionResult Index() { var path = Server.MapPath(@"/content/user.xlsx") ...

  8. bzoj1211-树的计数

    题意 给出 \(n\) 和长度为 \(n\) 的数列 \(d\) 表示每个点的度数,问有多少颗满足要求的树. 分析 这题是prufer编码的应用. prufer编码是对一个带标号无根树的刻画,生成方式 ...

  9. bzoj5127[Lydsy12月赛]数据校验

    多少年不写题解了 题目描述: 著名出题人小 Q 出了一道题,这个题给定一个正整数序列 a1, a2, ..., an,并保证输入数据中,对于 a 的任意一个非空连续子区间 [l, r],该区间内出现过 ...

  10. 2月24日考试——ZYYS

    LSGJ zyys 战队的 CYA 小垃圾,被各位神佬出的题目搞得心态爆炸.于是他模仿了蔡老师给了你两个整数 n 和 m .让你计算字母表大小为 m ,(即可用 m 个字母)长度为 n ,不存在长度至 ...