矩阵快速幂(poj3070)、快速幂

Fibonacci
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14845 Accepted: 10442

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 <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

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

           
           
2 2 2 1 0 0 1 3 99999999 1 2 3 4 5 6 7 8 9
 

Sample Output

           
           
2 2686
 

#include <stdio.h>
#define mod 9973
struct 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

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 1 1 2 2 0 0 3 7 23 47 16
 

Sample Output

           
           
234 2799 72937

Hint

          
          
 

#include <stdio.h>
#define  mod 10000007
long 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值