矩阵快速幂(poj3070)、快速幂
来源:互联网 发布:exescope软件下载 编辑:程序博客网 时间:2024/05/20 09:23
</pre><table border="0" width="100%" background="http://poj.org/images/table_back.jpg" style="font-family: Times;"><tbody><tr><td><div class="ptt" lang="en-US" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue;">Fibonacci</div><div class="plm" style="text-align: center; font-size: 12pt;"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 1000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 65536K</td></tr><tr><td><strong>Total Submissions:</strong> 9781</td><td width="10px"> </td><td><strong>Accepted:</strong> 6968</td></tr></tbody></table></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Description</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt;"><span lang="en-us"></span><p>In the Fibonacci integer sequence, <em>F</em><sub>0</sub> = 0, <em>F</em><sub>1</sub> = 1, and <em>F<sub>n</sub></em> = <em>F<sub>n</sub></em><sub> − 1</sub> + <em>F<sub>n</sub></em><sub> − 2</sub> for <em>n</em> ≥ 2. For example, the first ten terms of the Fibonacci sequence are:</p><p align="center">0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …</p><p>An alternative formula for the Fibonacci sequence is</p><p align="center"><img src="http://poj.org/images/3070_1.png" align="middle" alt="" />.</p><p>Given an integer <em>n</em>, your goal is to compute the last 4 digits of <em>F<sub>n</sub></em>.</p></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Input</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt;"><span lang="en-us"></span><p>The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ <em>n</em> ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.</p></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Output</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif; font-size: 12pt;"><span lang="en-us"></span><p>For each test case, print the last four digits of <em>F<sub>n</sub></em>. If the last four digits of <em>F<sub>n</sub></em> are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print <em>F<sub>n</sub></em> mod 10000).</p></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Sample Input</p><pre class="sio" style="font-family: 'Courier New', Courier, monospace; font-size: 12pt;">099999999991000000000-1
Sample Output
0346266875
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
Stanford Local 2006
#include <stdio.h>const int mod=10000;struct matrix{ int a[2][2];}origin,res;struct matrix multiply(struct matrix x,struct matrix y){ int i,j,k; struct matrix temp; memset(temp.a, 0, sizeof(temp.a)); for(i=0;i<2;i++) for(j=0;j<2;j++) for(k=0;k<2;k++) temp.a[i][j]+=(x.a[i][k]%mod)*(y.a[k][j]%mod)%mod; return temp; }void init(){ memset(res.a, 0, sizeof(res.a)); res.a[0][0]=res.a[1][1]=1;}void calc(long long n){ while(n) { if(n&1) res=multiply(res, origin); n>>=1; origin=multiply(origin, origin); } printf("%d\n",res.a[1][0]%10000);}int main(int argc, const char * argv[]){ long long n; while(scanf("%lld",&n) && n!=-1) { origin.a[0][0]=1,origin.a[0][1]=1,origin.a[1][0]=1,origin.a[1][1]=0; init(); calc(n); init(); } return 0;}
通过这个题目学习到了矩阵快速幂~~~,感觉萌萌大~~~
顺便贴一下快速幂代码
int pw(int a, int b){int r = 1, base = a;while (b != 0){if (b & 1)r *= base;base *= base;b >>= 1;}return r;}
比较一下,其实矩阵快速幂只是将乘法变为了一种函数,其实想想普通的a*b中乘法也是一种函数,类比一下吧
再来一个矩阵快速幂
#include <stdio.h>#define mod 9973struct matrix{ int a[10][10];}res,origin;struct matrix multiply(struct matrix x,struct matrix y){ int i,j,k; struct matrix temp; memset(temp.a, 0, sizeof(temp.a)); for(i=0;i<10;i++) for(j=0;j<10;j++) for(k=0;k<10;k++) temp.a[i][j]+=(x.a[i][k]%mod)*(y.a[k][j]%mod)%mod; return temp; }void calc(long long n){ while(n) { if(n&1) res=multiply(res, origin); n>>=1; origin=multiply(origin, origin); }}int main(int argc, const char * argv[]){ int n,k,t,i,j,g,sum; scanf("%d",&t); for(i=0;i<t;i++) { sum=0; scanf("%d %d",&n,&k); memset(res.a, 0, sizeof(res.a)); for(j=0;j<n;j++) res.a[j][j]=1; for(j=0;j<n;j++) for(g=0;g<n;g++) scanf("%d",&origin.a[j][g]); calc(k); for(j=0;j<n;j++) sum+=res.a[j][j]%mod; sum%=mod; printf("%d\n",sum); } return 0;}
#include <stdio.h>#define mod 10000007long long n,m;struct matrix{ long long a[15][15];}origin,res;struct matrix multiply(struct matrix x,struct matrix y,long long q,long long w){ long long i,j,k; struct matrix temp; memset(temp.a,0,sizeof(temp.a)); for(i=0;i<q;i++) for(j=0;j<w;j++) for(k=0;k<w;k++) temp.a[i][j]+=(x.a[i][k])*(y.a[k][j])%mod;//通过i判断,则为矩阵x*y; return temp;}void init(){ long long i,k; memset(origin.a,0,sizeof(origin.a)); memset(res.a,0,sizeof(res.a)); for(i=0;i<=n+1;i++) { origin.a[0][i]=10; origin.a[i][n+1]=0; origin.a[n+1][i]=1; res.a[i][i]=1; } origin.a[0][n+1]=0; for(i=1;i<=n;i++) for(k=i;k<=n;k++) origin.a[i][k]=1;}void calc(){ while(m) { if(m&1) res=multiply( res,origin,n+2,n+2); m>>=1; origin=multiply(origin, origin,n+2,n+2); }}int main(int argc, const char * argv[]){ long long i; struct matrix c; memset(c.a,0,sizeof(c.a)); while(~scanf("%lld %lld",&n,&m)) { getchar(); c.a[0][0]=23,c.a[0][n+1]=3; for(i=1;i<=n;i++) scanf("%lld",&c.a[0][i]); init(); calc(); c=multiply(c, res, 1,n+2); printf("%lld\n",c.a[0][n]%mod); } return 0;}都是一个模块,可以学习一下
0 0
- poj3070--矩阵 快速幂
- poj3070矩阵快速幂
- poj3070矩阵 快速幂
- 快速矩阵幂POJ3070
- poj3070 矩阵快速幂
- poj3070 Fibonacci 矩阵快速幂
- poj3070 Fibonacci 矩阵快速幂
- POJ3070 - Fibonacci - 矩阵快速幂
- POJ3070 Fibonacci(矩阵快速幂)
- poj3070 Fibonacci(矩阵快速幂)
- poj3070 Fibonacci(矩阵快速幂)
- POJ3070--Fibonacci--矩阵快速幂
- POJ3070(矩阵快速幂模板)
- poj3070(矩阵快速幂)
- poj3070 矩阵快速幂Fib
- Poj3070 Fibonacci (矩阵快速幂)
- POJ3070 Fibonacci(矩阵快速幂)
- 矩阵快速幂(poj3070)、快速幂
- NSDateFormatter 'YYYY' 和 'yyyy' 的区别
- UVA 12563 Jin Ge Jin Qu hao(DP)
- C和C++宏定义详解
- 剑指offer 面试题43 n个骰子点数和的分布律
- 为什么那么喜欢c
- 矩阵快速幂(poj3070)、快速幂
- Mac下为Android项目添加Maven
- 时间规划
- 怎么加载包含文件
- 头文件和库的理解
- 【Unity快速实现小功能】NGUI篇——快速实现一个可滑动列表
- java中读取服务器配置文件方法
- 12-26-2014——复习java基础——星期五
- 运行.exe程序时,提示缺少MFC42