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. 四则运算2+psp0

    程序要求: 1.题目避免重复 2.可定制(数量\打印方式) 3.可以一下控制参数 ① 是否有乘除法 ② 是否有括号(最多支持十个数参与运算) ③ 数值范围 ④加减有无负数 ⑤除法有无余数 分析:① 如 ...

  2. HDU 5464 Clarke and problem 动态规划

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5464 Clarke and problem  Accepts: 130  Submissions: ...

  3. 敏捷冲刺DAY6

    一. 每日会议 1. 照片 2. 昨日完成工作 3. 今日完成工作 4. 工作中遇到的困难 对于可视控件,是能进行设计的,但是对于不可视组件,比如AdoConnection怎么才能设计.但是我看del ...

  4. jquery 添加与删除的规律 当要添加时候要定位到自己的父元素 当要删除时候 通过事件函数传入的this找到自己的父元素进行删除

    jquery 添加与删除的规律 当要添加时候要定位到自己的父元素  当要删除时候 通过事件函数传入的this找到自己的父元素进行删除

  5. java map添加另一个map时候 键值对的类型要一致

    java map添加另一个map时候 键值对的类型要一致

  6. cdq分治学习

    看了stdcall大佬的博客 传送门: http://www.cnblogs.com/mlystdcall/p/6219421.html 感觉cdq分治似乎很多时候都要用到归并的思想

  7. BZOJ4977 跳伞求生(贪心)

    如果现在选定了一些要求消灭的敌人而不考虑积分,显然应该让每个敌人被刚好能消灭他的人消灭.再考虑最大化积分,显然我们应该优先消灭ci-bi大的敌人,所选用的a也应尽量大.于是按ci-bi从大到小排序,用 ...

  8. 【JavaScript】离线应用与客户端存储

    一.前言        这章非常重要,由于之后需要负责平台手机APP的日后维护,如何让用户在离线状态下正常使用,以及联网后的数据合并变得非常重要. 二.内容        离线检测 navigator ...

  9. 洛谷 P3338 [ZJOI2014]力 解题报告

    P3338 [ZJOI2014]力 题目描述 给出n个数qi,给出Fj的定义如下: \(F_j = \sum_{i<j}\frac{q_i q_j}{(i-j)^2 }-\sum_{i>j ...

  10. Redis的Sorted Set有序集合命令

    Sorted Set是Set的一个升级版本,它在Set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的值调整顺序.可以理解为有两列的mysql表, ...