poj3070 求斐波那契数列第n项 ——矩阵快速幂
题目:http://poj.org/problem?id=3070
用矩阵快速幂加速递推。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,p=;
struct Matrix{
int a[][];
Matrix operator * (const Matrix &y) const
{
Matrix x;
memset(x.a,,sizeof x.a);
for(int i=;i<=;i++)
for(int k=;k<=;k++)
for(int j=;j<=;j++)
x.a[i][j]+=y.a[i][k]*a[k][j];
return x;
}
}m,ans;
void mod()
{
m.a[][]%=p;m.a[][]%=p;//
m.a[][]%=p;m.a[][]%=p;
ans.a[][]%=p;ans.a[][]%=p;
}
int main()
{
while(scanf("%d",&n)==)
{
if(n==-)return ;
memset(m.a,,sizeof m.a);
memset(ans.a,,sizeof ans.a);
m.a[][]=;m.a[][]=;
m.a[][]=;m.a[][]=;
ans.a[][]=;ans.a[][]=;
if(!n){printf("0\n");continue;}
if(n==){printf("1\n");continue;}
if(n==){printf("1\n");continue;}
n--;//!
while(n)
{
if(n&)ans=ans*m;
m=m*m;
mod();
n=(n>>);
}
printf("%d\n",ans.a[][]%p);
}
return ;
}
poj3070 求斐波那契数列第n项 ——矩阵快速幂的更多相关文章
- 黑马入学基础测试(三)求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
.获得用户的输入 计算 3打印就行了. 这里用到了java.util.Scanner 具体API 我就觉得不常用.解决问题就ok了.注意的是:他们按照流体的方式读取.而不是刻意反复 ...
- 用JS,求斐波那契数列第n项的值
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 01-封装函数求斐波那契数列第n项
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- C#求斐波那契数列第30项的值(递归和非递归)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Python - 求斐波那契数列前N项之和
n = int(input("Input N: ")) a = 0 b = 1 sum = 0 for i in range(n): sum += a a, b = b, a + ...
- JS求斐波那契数列的N项
第一种求法: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF- ...
- 【poj3070】矩阵乘法求斐波那契数列
[题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...
- C# 求斐波那契数列的前10个数字 :1 1 2 3 5 8 13 21 34 55
//C# 求斐波那契数列的前10个数字 :1 1 2 3 5 8 13 21 34 55 using System; using System.Collections.Generic; using S ...
- golang 闭包求斐波那契数列
题目是Go指南中的闭包求斐波那契数列 package main import "fmt" // 返回一个"返回int的函数" func fibonacci() ...
随机推荐
- Vue.js 使用cordova camera插件调取相机
本文给出在vue.js里如何使用cordova的插件完成调取相机及图库,并完成图片上传的操作.具体的操作步骤如下 第一步:在cordova项目下安装cordova-plugin-camera插件 co ...
- python 调用函数时使用星号 *, **
python 调用函数时使用星号 *, ** 调用函数时使用星号 * 或 ** test(*args):* 的作用其实就是把序列 args 中的每个元素,当作位置参数传进去.如果 args 等于 (1 ...
- spring核心包功能解析
- LeetCode求能够装得最多的水
费了半天劲还是没想出来,然后跑到网上找答案,明白了是怎么回事儿了,就是不知道为啥自己没有想出来. 明天再搞~~~ 国庆快乐~~~ 一年了~~~~~ 明年今日~~我会在哪儿 =====2017.9.30 ...
- 代写GIS系统
代写GIS系统,熟悉arcgis,leaflet ,百度地图等api.可以提供系统代写,技术咨询等
- java手写单例模式
1 懒汉模式 public class Singleton { private Singleton singleton = null; private Singleton() { } public S ...
- The threads in the thread pool will process the requests on the connections concurrently.
https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html Most of the executor implem ...
- Redis(一)基础数据结构
1.目录 Redis 基础数据结构 string (字符串) list (列表) hash (字典) set (集合) zset (集合) 容器型数据结构的通用规则 过期时间 2.Redis 基础数据 ...
- Java中实现函数的阻塞
使用Object.wait()即可实现阻塞,使用Object.notify()解除阻塞,代码示例如下 MainFrame.java import javax.swing.JFrame; import ...
- java web service
1.编写服务代码 服务代码提供了两个函数,分别为sayHello和sayHelloToPerson,源代码如下: /* * File name: HelloService.java * * Versi ...