矩阵快速幂(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中乘法也是一种函数,类比一下吧


再来一个矩阵快速幂

C - Tr A
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。 
 

Input

数据的第一行是一个T,表示有T组数据。 
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。 
 

Output

对应每组数据,输出Tr(A^k)%9973。
 

Sample Input

22 21 00 13 999999991 2 34 5 67 8 9
 

Sample Output

22686
 

#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;}
D - 233 Matrix
Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333... (it means a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333...) Besides, in 233 matrix, we got a i,j = a i-1,j +a i,j-1( i,j ≠ 0). Now you have known a 1,0,a 2,0,...,a n,0, could you tell me a n,m in the 233 matrix?
 

Input

There are multiple test cases. Please process till EOF. 

For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).
 

Output

For each case, output a n,m mod 10000007.
 

Sample Input

1 112 20 03 723 47 16
 

Sample Output

234279972937

Hint

 

#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
原创粉丝点击