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 ...
随机推荐
- Oracle查看正在执行的存储过程
正在执行的存储过程 select owner,name from v$db_object_cache where type like '%PROCE%' and locks >0 and pin ...
- js 保留两位小数 input要求是数字框,
要求:input文本框只能输入数字,且只保留两位小数 问题:若设置input的 type="number" ,js处理部分若用到parseFloat方法处理,结果是string类 ...
- luogu P3357 最长k可重线段集问题
这题和3358一模一样,建模形式直接不用变,就两点不一样,一是len变化了,加入y后再更新即可,还有就是可能会出现x0=x1的情况,即一条开线段垂直x轴,如果我们依旧按照上一题的建图方法,就会出现负权 ...
- 通用dao的demo
代码片段 1. [代码]整型映射工具 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package org.dave.common.databas ...
- c# Thread、ThreadPool、Task的区别
Thread与ThreadPoll 前台线程:主程序必须等待线程执行完毕后才可退出程序.Thread默认为前台线程,也可以设置为后台线程 后台线程:主程序执行完毕后就退出,不管线程是否执行完毕.Thr ...
- pytho 基本数据类型
1.字符串(引号) name = "........." 双引号引出来的都是字符串 name = """.........""& ...
- 二、Linux目录结构&常用指令
Linux目录结构: ps -ef:任务管理器 ifconfig: 查看ip ping : 测试与目标主机的连通性,ctrl+c停止 目录指令: ll:列出当前目录下的文件信息 ls -al : 列 ...
- zabbix监控memcached服务
zabbix监控memcached服务 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装并配置memcached服务 1>.使用yum方式安装memcached [ro ...
- xfpt 连接Linux失败问题
首先切换到root用户 1. su 未设置root密码的可以使用一下命令 sudo passwd root 一.上传文件失败(一动不动) 1.安装ftp服务 apt-get install vsftp ...
- hook框架frida的安装以及简单实用案例
1.下载地址 https://github.co/frida/frida/releases 2.另外两种安装方法 1.Install from prebuilt binaries This is th ...