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(模块&包)
参考:https://www.cnblogs.com/yuanchenqi/articles/5732581.html 在linux下给pycharm安装第三方库,需要在.bashrc中加: 因为对应 ...
- Linux下的压缩及归档
Linux下常用的压缩格式有: gz,bz2,xz,zip,Z //只能压缩文件不能压缩目录,如果传递一个目录,他会把目录中的文件逐个压缩 ..压缩算法:算法不同,压缩比也不同 gz:gzip,压缩后 ...
- JMeter PerfMon Metrics Collector性能监控插件
官方文档地址https://jmeter-plugins.org/wiki/PerfMon/ JMeter是一款压力测试工具,我们也可以用它来监控服务器资源使用情况. JMeter正常自带可以通过To ...
- docker 运行容器时为容器起别名
docker run --name=mydemo -p -d 2222:80 imagename --name: 指定容器名称 -p:指定容器端口号 -d:指定容器后台运行
- 升级vue-cli为 cli3 并创建项目
一.升级npm install -g @vue/cli 二.创建项目 1.vue create vue3-project 下面会提示让你配置下自己想要用到的功能,然后它会自动帮你安装,这个看自己需求 ...
- 查看哪个用户、IP、什么时间登陆过服务器
2019-01-07 utmpdump /var/log/wtmp 或者 who /var/log/wtmp
- Openerp 添加修改报表
Report Designer 模块在生成新报表的时候是有BUG的不建议直接使用,不过我们也可以通过该插件再写简单的代码来实现新添加报表,插件安装成功后我们可以按照下列方法来添加报表 OpenERP ...
- MySQL Migration Tool报“initialized java loader”错误的问题
MySQL Migration Tool报“initialized java loader”错误的问题 运行MySQL Migration Tool时经常会提示“An error occured ...
- java多线程---------java.util.concurrent并发包----------ThreadPoolExecutor
ThreadPoolExecutor线程池 一.三个构造方法 ThreadPoolExecutor(int corePoolSize,int MaxmumPoolSize,long KeepAlive ...
- 各种数据库maven的pom文件编写与ibernate链接配置
各种数据库Hibernate链接配置 Derby db driver maven dependency <dependency> <groupId>org.ap ...