poj 3070 Fibonacci 矩阵相乘
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7715 | Accepted: 5474 |
Description
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is
.
Given an integer n, your goal is to compute the last 4 digits of Fn.
Input
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
Output
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
Sample Input
0
9
999999999
1000000000
-1
Sample Output
0
34
626
6875
Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
.
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:
.
Source
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring> struct node
{
int a[][];
}now,cur; void make_first()
{
now.a[][]=;
now.a[][]=;
now.a[][]=;
now.a[][]=; cur.a[][]=;
cur.a[][]=;
cur.a[][]=;
cur.a[][]=;
} struct node make_cheng(node a,node b) //发现结构体忘记了。
{
struct node f;
int i,k,j;
memset(f.a,,sizeof(f.a));
for(i=;i<=;i++)
for(k=;k<=;k++)
if(a.a[i][k])
{
for(j=;j<=;j++)
if(b.a[k][j])
{
f.a[i][j]+=a.a[i][k]*b.a[k][j];
if(f.a[i][j]>=)
f.a[i][j]%=;
}
}
return f;
} void make_EF(int n)
{
make_first();
while(n)
{
if(n&)
{
now=make_cheng(now,cur);//!!!
}
n=n/;
cur=make_cheng(cur,cur);
}
printf("%d\n",now.a[][]);
} int main()
{
int n;
while(scanf("%d",&n)>)
{
if(n==-)break;
if(n==){printf("0\n");continue;}
if(n==||n==){printf("1\n");continue;}
make_EF(n-);
}
return ;
}
poj 3070 Fibonacci 矩阵相乘的更多相关文章
- POJ 3070 Fibonacci(矩阵高速功率)
职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- POJ 3070 Fibonacci 矩阵高速求法
就是Fibonacci的矩阵算法.只是添加一点就是由于数字非常大,所以须要取10000模,计算矩阵的时候取模就能够了. 本题数据不强,只是数值本来就限制整数,故此能够0ms秒了. 以下程序十分清晰了, ...
- POJ 3070 Fibonacci 矩阵快速幂模板
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18607 Accepted: 12920 Descr ...
- POJ 3070 Fibonacci矩阵快速幂 --斐波那契
题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...
- POJ 3070 Fibonacci(矩阵快速幂)
题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...
- 题解报告:poj 3070 Fibonacci
题目链接:http://poj.org/problem?id=3070 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, a ...
随机推荐
- Python(序列化json,pickle,shelve)
序列化 参考:https://www.cnblogs.com/yuanchenqi/articles/5732581.html # dic = str({'1':'111'}) # # f = ope ...
- Android IPC机制—Binder的工作机制
进程和线程的关系 IPC机制即为跨进程通信,是inter-Process Communication的缩写.是指两个进程之间进行通信.在说进程通信之前,我们的弄明白什么是线程,什么是进程.进程和线程是 ...
- 面向对象之ajax
1.Ajax发送请求的几个步骤 1. 创建 XMLHttpRequest 对象 var xhr = new XMLHttpRequest();//IE6 使用var xhr= new ActiveXO ...
- [Swift]遍历集合类型(数组、集合和字典)
Swift提供了三种主要的集合类型,称为数组,集合和字典,用于存储值集合. 数组是有序的值集合. 集是唯一值的无序集合. 字典是键值关联的无序集合. Swift中无法再使用传统形式的for循环. // ...
- 【vim】简介与基本配置
vim是一款非常强大的文字编辑软件,是各种类UNIX系统标配的文本编辑工具.相信此文的读者对它应该不会陌生,在这里就不做介绍了. 1.为什么要使用vim 在认识vim之前,我用过好多IDE:Visua ...
- SpringAOP的应用实例与总结
一:AOP的背景 面试的时候面试官让我解释一下什么是AOP,当时不懂,在路上就查了,AOP:面向切面的编程技术,困惑了,JAVA是OOP:面向对象的编程技术.那么自己就立刻查了几个为题:1.什么是面向 ...
- javascript获取wx.config内部字段解决微信分享
转自:http://www.jb51.net/article/80679.htm 专题推荐:js微信开发_脚本之家 http://www.jb51.net/Special/879.htm 背景在微信分 ...
- Metal Programming Guide
读苹果文档时的笔记,给自己看. primary goal of Metal is to minimize the CPU overhead incurred by executing GPU work ...
- kao shi
1 #include "date.h" #include "utils.h" #include <iostream> using std::cout ...
- Typecho V1.1反序列化导致代码执行分析
0x00 前言 今天在Seebug的公众号看到了Typecho的一个前台getshell分析的文章,然后自己也想来学习一下.保持对行内的关注,了解最新的漏洞很重要. 0x01 什么是反序列 ...