Modular Fibonacci

The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...) are defined by the recurrence:
F0 = 0
F1 = 1
Fi = Fi−1 + Fi−2 for i > 1
Write a program which calculates Mn = Fn mod 2m for given pair of n and m. 0 ≤ n ≤ 2147483647
and 0 ≤ m < 20. Note that a mod b gives the remainder when a is divided by b.
Input
Input consists of several lines specifying a pair of n and m.
Output
Output should be corresponding Mn, one per line.
Sample Input
11 7
11 6
Sample Output
89
25

题解

由于n<=2 147 483 647,直接for会超时。用矩阵快速幂就好了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll;
ll MOD;
struct Matrix {
ll mat[][];
}U,F;
Matrix multi (Matrix a, Matrix b) {
Matrix ans;
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
ans.mat[i][j] = ;
for(int k = ; k < ; k++)
ans.mat[i][j] += a.mat[i][k] * b.mat[k][j];
ans.mat[i][j] %= MOD;
}
}
return ans;
}
Matrix powss(ll n) {
Matrix ans = U,p = F;
while(n) {
if(n&) ans = multi(ans,p);
n>>=;
p = multi(p,p);
}
return ans;
}
int main() {
U = {,,,};
F = {,,,};
ll n,m;
while(~scanf("%lld%lld",&n,&m)) {
MOD = 1ll<<m;
Matrix ans = powss(n);
printf("%lld\n",ans.mat[][]);
}
return ;
}

UVA - 10229 Modular Fibonacci 矩阵快速幂的更多相关文章

  1. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  2. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  3. HDU 1588 Gauss Fibonacci(矩阵快速幂)

    Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  4. POJ 3070 Fibonacci 矩阵快速幂模板

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18607   Accepted: 12920 Descr ...

  5. poj3070 Fibonacci 矩阵快速幂

    学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...

  6. UVA 10229 Modular Fibonacci

    斐波那契取MOD.利用矩阵快速幂取模 http://www.cnblogs.com/Commence/p/3976132.html 代码: #include <map> #include ...

  7. UVA - 10870 Recurrences 【矩阵快速幂】

    题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...

  8. $loj$10222 佳佳的$Fibonacci$ 矩阵快速幂

    正解:矩阵快速幂 解题报告: 我永远喜欢loj! 一看到这个就应该能想到矩阵快速幂? 然后就考虑转移式,发现好像直接想不好想,,,主要的问题在于这个*$i$,就很不好搞$QAQ$ 其实不难想到,$\s ...

  9. POJ 3070 Fibonacci矩阵快速幂 --斐波那契

    题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...

随机推荐

  1. iOS-为方便项目开发在pch加入一些经常使用宏定义

    1.关于NSLog输出 /** * 当Xcode为Release时不输出,为Debug时输出 * * @param ... * * @return */ #ifndef __OPTIMIZE__ #d ...

  2. SSD纠错码向LDPC码演变

    作者:Stephen Bates SSD控制器芯片中採用的纠错编码(ECCs)的类型正在发生一场演变.相信很多这篇博文的读者对此都有所了解.传统上採用的纠错码是基于群变换的博斯-查德胡里-霍昆格母(B ...

  3. 读取excel模板填充数据 并合并相同文本单元格

    try             { string OutFileName = "北京市国控企业污染源废气在线比对监测数据审核表" + DateTime.Now.ToString(& ...

  4. c语言数组小谈

    #include <stdio.h> #include <stdlib.h> #define N 5 int main() { double score[5]; int i; ...

  5. query.setFirstResult解析

    转自:https://blog.csdn.net/thinkingcao/article/details/78053622

  6. [Tomcat]Tomcat6和Tomcat7的区别

    Tomcat 7最大的改进是对Servlet 3.0和Java EE 6的支持.◆Tomcat 7完全支持Servlet 3.0规范◆Tomcat 7新增了对Java注释的支持◆Tomcat 7通过w ...

  7. js接收文件流并下载

    js接收文件流并下载 标签(空格分隔): js 在此输入正文 <script type="text/javascript"> function download(fil ...

  8. POJ 3233 矩阵快速幂&二分

    题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...

  9. OAuth2建立webapi认证服务供自己的客户端使用--密码模式

    场景:你自己实现了一套webApi,想供自己的客户端调用,又想做认证. 第一步:通过vs2015建立web api项目,Startup.cs,这个类将会作为Owin的启动类. 第二步:在webapi. ...

  10. 对ListView的Item子控件监听并跳转页面

    public class MyAdapteforOwner extends BaseAdapter{ List<OwnerDevice>datas; private Context con ...