UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)
题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数。
分析:n最大为109,矩阵快速幂求解,复杂度log2(109)。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-9;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1000007;
const double pi = acos(-1.0);
const int MAXN = 400 + 10;
const int MAXT = 1000 + 10;
using namespace std;
int POW[10];
int a, b, n, m;
void init(){
POW[1] = 10;
for(int i = 2; i <= 4; ++i){
POW[i] = POW[i - 1] * 10;
}
}
struct Matrix{
int r, c;
int matrix[2][2];
Matrix(int rr, int cc):r(rr), c(cc){
memset(matrix, 0, sizeof matrix);
}
};
Matrix mul(Matrix a, Matrix b){
Matrix ans(a.r, b.c);
for(int i = 0; i < a.r; ++i){
for(int j = 0; j < b.c; ++j){
int tmp = 0;
for(int k = 0; k < a.c; ++k){
(tmp += a.matrix[i][k] * b.matrix[k][j]) %= POW[m];
}
ans.matrix[i][j] = tmp;
}
}
return ans;
}
Matrix QPOW(Matrix tmp, int k){
Matrix ans(2, 2);
ans.matrix[0][0] = ans.matrix[1][1] = 1;
while(k){
if(k & 1) ans = mul(ans, tmp);
tmp = mul(tmp, tmp);
k >>= 1;
}
return ans;
}
int main(){
int T;
scanf("%d", &T);
init();
while(T--){
scanf("%d%d%d%d", &a, &b, &n, &m);
Matrix tmp1(2, 2), tmp2(2, 1);
tmp1.matrix[0][0] = tmp1.matrix[0][1] = tmp1.matrix[1][0] = 1;
tmp2.matrix[0][0] = b;
tmp2.matrix[1][0] = a;
Matrix ans = mul(QPOW(tmp1, n - 1), tmp2);
printf("%d\n", ans.matrix[0][0]);
}
return 0;
}
UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)的更多相关文章
- codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质
E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- poj3070矩阵快速幂求斐波那契数列
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13172 Accepted: 9368 Desc ...
- UVA 10689 Yet another Number Sequence 矩阵快速幂 水呀水
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- 51 Nod 1242 矩阵快速幂求斐波那契数列
#include<bits/stdc++.h> #define mod 1000000009 using namespace std; typedef long long ll; type ...
- 矩阵快速幂 求斐波那契第N项
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> us ...
- 矩阵快速幂--51nod-1242斐波那契数列的第N项
斐波那契额数列的第N项 斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, ...
- codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)
题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...
- python 快速幂求斐波那契数列
先占坑 后面再写详细的 import numpy as np def pow(n): a = np.array([[1,0],[0,1]]) b = np.array([[1,1],[1,0]]) n ...
随机推荐
- java虚拟机之内存分配
Java 的自动内存管理主要是针对对象内存的回收和对象内存的分配.同时,Java 自动内存管理最核心的功能是 堆 内存中对象的分配与回收. JDK1.8之前的堆内存示意图: 从上图可以看出堆内存分为新 ...
- 《O’Reilly精品图书系列共21册》azw3
套装书目: <机器学习:实用案例解析> <利用Python进行数据分析> <社交网站的数据挖掘与分析(原书第2版)> <社会网络分析:方法与实践> &l ...
- 开源Web测试工具介绍
HtmlUnitHtmlUnit 是 JUnit 的扩展测试框架之一.HtmlUnit 将返回文档模拟成 HTML,这样您便可以直接处理这些文档了.HtmlUnit 使用例如 table.form 等 ...
- PHP截取指定字符前的字符串
$str = 'A|B||C|D'; echo substr($str,0,strpos($str, '||')); 输出:A|B
- C语言中指针和取地址符&的关系
一 概念定义: 严格说起来,应该这么讲:指针存的是地址,而&运算符是取变量的地址. 指针原理: 其实计算机中的内存就像一个个抽屉(一兆内存就相当于1百万个抽屉),每个抽屉都有一个编号,用于存放 ...
- Delphi 10.3.3 THTTPClient Post问题
如果对于Post提交,需要对参数进行urlEncode处理的需要注意. 对于Post参数,可以用TString或者TStringStream两者.如果你采用的是用TStringStream,那么必须按 ...
- 012.Delphi插件之QPlugins,多实例内嵌窗口服务
这个DEMO中主要是在DLL中建立了一个IDockableControl类,并在DLL的子类中写了具体的实现方法. 在主程序exe中,找到这个服务,然后调用DLL的内嵌方法,把DLL插件窗口内嵌到主程 ...
- centos6或7查看端口占用及解除占用
一.查看端口占用 netstat -lnp|grep 要查看的端口号 例如:查看占用端口7000的进程 netstat -lnp|grep 7000 二.清除占用 (1)一次性的清除占用80端口的程序 ...
- 记一次Win上MySQL乱码问题
Win上MySQL乱码问题 笔记本上的数据库总会时不时的乱码(或者是一直乱码我没注意到?),在谷歌上试了几次错才正确解决,在此记录一下. 在MySQL数据库存储目录找到my.ini,在相应的标签下分别 ...
- symbol数据类型
symbol声明的类型独一无二 概念:表示独一无二的值,永远不相等 s1 = Symbol() s2 = Symbol() s1 !== s2 基本使用: 通过Symbol函数生成,得到一个symbo ...