矩阵快速幂求递推式

来源:互联网 发布:迅捷网络fwr200 编辑:程序博客网 时间:2024/05/16 17:30

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 108), 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

1291005

Sample Output

123410
彭彭代码如下
#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