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 ...
随机推荐
- Red_Hat yum源配置
http://www.linuxidc.com/Linux/2016-06/132171.htm
- 多选按钮CheckBox
main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...
- 091、Java中String类之使用“==”比较
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- C语言常用函数
一.数学函数 调用数学函数时,要求在源文件中包下以下命令行: #include <math.h> 函数原型说明 功能 返回值 说明 int abs( int x) 求整数x的绝对值 计算结 ...
- 你必须知道的.Net 8.2.2 本质分析
1 .Equals 静态方法 Equals 静态方法实现了对两个对象的相等性判别,其在 System.Object 类型中实现过程可以表 示为: public static bool Equals ...
- Docker基本使用运行ngix镜像
docker pull 项目名 会从docker默认的仓库去拉去项目,如果是docker pull 项目名 地址 会从给定地址拉去镜像 docker run image名字 运行镜像 docker架 ...
- 四篇关于chen_zhe的美文
壹 chen_zhe人 那是谁 是谁 是谁 那就是 chen_zhe chen_zhe人 chen_zhe人 背负着暴政之名 抛弃了一切(指民心)而战斗(指禁言)的男人 chen_zhe代码是超音 ...
- 题解:luogu P1247
大概没你们说得复杂吧...... \(Part\;1\) \(Nim\)游戏 大家都对异或和感到懵逼吧(排除大佬),其实很简单,用\(SG\)函数打表计算即可解决: 抛个板子: void get_sg ...
- Ubuntu 更新源 内核升级
cat /etc/apt/sources.listdeb http://mirrors.sohu.com/ubuntu/ precise main restricted universe multiv ...
- grep -o -E
sed 命令可以很好的进行行匹配,但从某一行中精确匹配某些内容,则使用 grep 命令并辅以 -o 和 -E 选项可达到此目的.其中 -o 表示“only-matching”,即“仅匹配”之意.光用它 ...