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 ...
随机推荐
- springBoot整合MyBatise及简单应用
springBoot整合MyBatise及简单应用 我采用的是 工具IDEA 框架是springBoot+maven+Mybatise 第一步: pom.xml 引入相关jar包 <?xml v ...
- Leetcode 98 验证二叉搜索树 Python实现
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- 2016级算法期末上机-A.简单·Bamboo's Fight with DDLs I
简单·Bamboo's Fight with DDLs I 分析 一句话:要装满的完全背包问题. 对比完全背包只有一点要改变:初始化为负无穷 传送门: https://buaacoding.cn/pr ...
- string常用字符串操作函数
1.strdup和strndup 说明:strdup() 函数将参数 s 指向的字符串复制到一个字符串指针上去,这个字符串指针事先可以没被初始化.在复制时,strdup() 会给这个指针分配空间,使用 ...
- lspci
lspci 是一个用来显示系统中所有PCI总线设备或连接到该总线上的所有设备的工具. 列出所有的PCIE设备: lspci 选项: -v 使得 lspci 以冗余模式显示所有设备的详细信息. -vv ...
- Codeforces Round #555 (Div. 3) A B C1(很水的题目)
A. Reachable Numbers 题意:设f(x)为 x+1 这个数去掉后缀0的数,现在给出n,问经过无数次这种变换后,最多能得到多少个不同的数. 代码 #include<cstdio& ...
- 用 diff 比较两个 hdfs 文件内容
diff <(hadoop fs -cat /path/to/file) <(hadoop fs -cat /path/to/file2)
- laravel 表单接收
POST方式接收 视图层 <form action="/submit" method="post"> {{csrf_field()}} //必 ...
- hibernate基本配置show_sql、sql_format
show_sql是否打印生成的sql语句: <property name="show_sql">false</property> sql_format,格式 ...
- 解决windows10下总是很快自动黑屏进入睡眠问题
在用win10的过程中总是过几分钟不操作电脑,就自动黑屏睡眠了. 下面讲解一下如何解决这个问题: 第一步:win +r 输入regedit.exe 运行注册表管理器 第二步:定位到 HKEY_LOC ...