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 ...
随机推荐
- Django:邮件功能实现
django-users2和django的邮件功能模块都有相关的实现 ----------------------------------------------------------------- ...
- ROS学习笔记9-创建ros消息和服务
该节内容主要来自于官方文档的两个小节:1.使用rosed来编辑2.创建ros消息的服务 先来看rosed: rosedrosed命令是rosbash的一部分,使用rosed可以直接编辑包中的一个文件, ...
- Referenced file contains errors
Referenced file contains errors (file:/D:/TONG/tong/eclipse/config_/xsd/spring-context-4.2.xsd). For ...
- 在vnware中配置好redis后,不能使用图形化工具打开
1.先检查防火墙的状态 通过systemctl status firewalld查看firewalld状态,发现当前是dead状态,即防火墙未开启 通过systemctl start firewall ...
- centos 安装 memcached
centos 安装 memcached 1. 安装依赖: libeventyum install libevent-devel 2. 获取最新版本wget http://memcached.org/l ...
- Uboot 命令行 介绍
背景 基本上,本文转载自:<ARM板移植Linux系统启动(五)Uboot命令行> 上次说到uboot的启动方式,最后会使用autoboot(自主模式)尝试引导kernel,如果失败或者被 ...
- linux下解决git clone太慢
此教程同样也适用与vscode下载太慢的问题 git和vscode会自动使用http_proxy,https_proxy环境变量的代理,所以我们只需要设置这个环境变量即可 前提 需要一个可用的代理,这 ...
- mysql 安装完以后没有mysql服务
用管理员身份打开命令控制台(cmd),然后将mysql的安装文件的路径打开(bin文件的路径),然后再路径下打上mysqld.exe -install, 会出现提示 Service successf ...
- phpstudy后门漏洞复现php5.2
前段时间phpstudy被人发现某些版本存在后门,许多人就这样被当作肉鸡长达两年之久 后门隐藏在程序自带的php的php_xmlrpc.dll模块 影响的版本:phpstudy2016和2018 在H ...
- 在命令提示符中运行install adb 包名.apk文件 遇到的问题
昨天更新了SDK以后遇到一个奇怪的问题,使用eclipse直接运行程序到手机没有问题,但是如果将程序打包之后在命令提示符中执行adb install 包名.apk 就会报错:adb server is ...