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设计分析(以学校领导/历届领导为例)

    第一次的冲刺阶段让我过了一把PM的瘾,第一阶段的冲刺完成以后第一感觉就是PM不好当,在大学里做个课程设计当个PM相对而言还是比较容易的,但是我明白,当我们走向工作岗位以后,面临的情况会比学校的情况的复 ...

  2. 写在SVM之前——凸优化与对偶问题

    SVM之问题形式化 SVM之对偶问题 SVM之核函数 SVM之解决线性不可分 >>>写在SVM之前——凸优化与对偶问题 本篇是写在SVM之前的关于优化问题的一点知识,在SVM中会用到 ...

  3. 【Leetcode】50. Pow(x, n)

    Implement pow(x, n). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 O ...

  4. MySQL存储引擎InnoDB与Myisam

    InnoDB与Myisam的六大区别 InnoDB与Myisam的六大区别 MyISAM InnoDB 构成上的区别: 每个MyISAM在磁盘上存储成三个文件.第一个 文件的名字以表的名字开始,扩展名 ...

  5. BZOJ 1266 上学路线(最短路+最小割)

    给出n个点的无向图,每条边有两个属性,边权和代价. 第一问求1-n的最短路.第二问求用最小的代价删边使得最短路的距离变大. 对于第二问.显然该删除的是出现在最短路径上的边.如果我们将图用最短路跑一遍预 ...

  6. presence_of_element_located与visibility_of_element_located区别

    selenium 问题:加了显性等待后,操作元素依然出错   背景: 用WebDriverWait时,一开始用的是presence_of_element_located,我对它的想法就是他就是用来等待 ...

  7. BZOJ3551 ONTAK2010Peaks加强版(kruskal重构树+dfs序+主席树)

    kruskal重构树本质就是给并查集显式建树来替代可持久化并查集.将边按困难度从小到大排序后建出该树,按dfs序建主席树即可.查询时跳到深度最浅的满足在该重要度下已被合并的点,在子树内查询第k大. # ...

  8. QVariant实质

    QVariant实质 QVariant是一种可以存储不同类型的数据结构,在很多场合这是很有用得为了达到这种目的,可以想象,该对象应该存储对象的类型信息,数据信息以及其他辅助详细考虑用途,这种对象必须支 ...

  9. 【刷题】洛谷 P2664 树上游戏

    题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 \[sum_i=\sum_{j=1}^ns(i,j)\] 现在他想让你求出所有 ...

  10. sql语句查询各门课程平均分的最大值

    解法一: select courseno,stuno,avg(score) '平均分最高值'--这里是求平均,后面的条件是过滤最大值的 from tablename group by courseno ...