矩阵快速幂求递推式

Problem I

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 35   Accepted Submission(s) : 13
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

I think that you might have played the traditional Chinese ring game: The Chinese Linking Rings (here we call its nickname Jiulianhuan —— “九连环”). Well, you say you haven’t played it before? Then you must have seen it before, right? If not seen, come to borrow mine to have a good look at it and enjoy it!

Now, I would like to mention the rules or common sense of Jiulianhuan again.
1)The first ring can put on or down the handles at any time. That is, when the first ring is under the handle, it can climb up the handle within one step, and vice versa.
2)At any moment, you can only operate one ring, on the condition that the ring is operable.
3)If the first k-2 rings are under the handle, and the (k-1)th ring is on the handle, then if the k-th ring is under the handle, you can put it on the handle, and if it is not under the handle, you can put it down the handle.
Seems complicated? But I tried my simplest explanation to you, and I hope its not hard for you to understand. Maybe you have played the game before, and the above is what actually a “step” means in the game.

Input

Given n (not bigger than 10 8), you are to output the minimum steps it needs to down n well-put rings. There are no more than 100 test cases.

Output

A number a line. Because the number are so huge ,you are to output the result after it mod prime 10007.

Sample Input

1
2
9
1005

Sample Output

1
2
341
0
 
 
彭彭代码如下
#include<stdio.h>
#include<iostream>
using namespace std;
const int mod = 10007;
struct Matrix
{
    int g, s[11][11];
};
Matrix mult(Matrix a, Matrix b)
{
    Matrix ret;
    int i, j, k, n;
    n = a.g;
    ret.g = n;
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
        {
            ret.s[i][j] = 0;
            for (k = 0; k < n; k++)
                ret.s[i][j] += a.s[i][k] * b.s[k][j];
            ret.s[i][j] = ret.s[i][j] % mod;
        }
    return ret;
}
Matrix asum(Matrix a, int p)
{
    Matrix sum, t;
    int n, i, j, k, m;
    n = a.g;
    sum.g = t.g = n;
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
        {
            if (i == j) sum.s[i][j] = 1;
            else sum.s[i][j] = 0;
            t.s[i][j] = a.s[i][j];
        }
    k = p;
    while (k > 0)
    {
        i = k % 2;
        k = k / 2;
        if (i)
            sum = mult(sum, t);

        t = mult(t, t);

    }

    return sum;
}
int main()
{
    Matrix ans, a;
    int i, j, k, t, n, sum;
    int x, y;
    x = 0;
    y = 1;
    while (scanf("%d", &n) != EOF)
    {
        if (n == 0)
        {
            printf("0\n");
            continue;
        }
        if (n == 1)
        {
            printf("1\n");
            continue;
        }
        if (n == 2)
        {
            printf("2\n");
            continue;
        }
        a.s[0][0] = 1; a.s[0][1] = 1; a.s[0][2] = 0;
        a.s[1][0] = 2; a.s[1][1] = 0; a.s[1][2] = 0;
        a.s[2][0] = 1; a.s[2][1] = 0; a.s[2][2] = 1;
        a.g = 3;
        ans = asum(a, n - 2);

        /*    for (i=0;i<n;i++)
                for (j=0;j<n;j++)
                    printf("%d %d %d \n",i,j,ans.s[i][j]);
        */
        sum = 0;
        //printf("%d\n",sum);
        sum = (2 * ans.s[0][0] + ans.s[1][0] + 1 * ans.s[2][0]);
        sum = sum % mod;
        printf("%d\n", sum);
    }
    return 0;
}
//fn=fn-1+2*fn-2+1;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值