HDU5015-233 Matrix (Matrix Rapid power), hdu5015-233matrix

Source: Internet
Author: User

HDU5015-233 Matrix (Matrix Rapid power), hdu5015-233matrix

Question Link


Given the number of 0th to n columns in a matrix, each number starts from 1st in the first row and is 1st ........, calculates the number of m in the nth row.

Train of Thought: shift the number of the first row to the right, and use the previous column to roll out the next column to construct a matrix, similar to the following
1 0 0 0 0 0 0
1 10 0 0 0 0 0
0 1 1 0 0 0
0 1 1 1 0 0 0
0 1 1 1 0 0
0 1 1 1 1 0

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int N = 15;const int MOD = 10000007;struct mat{    ll s[N][N];    int l;    mat(int o) {        l = o;        memset(s, 0, sizeof(s));     }    void init() {        s[0][0] = s[1][0] = 1;         s[1][1] = 10;        for (int i = 2; i < l; i++)            for (int j = 1; j <= i; j++)                s[i][j] = 1;    }    mat operator * (const mat& c) {        mat ans(l);         for (int i = 0; i < l; i++)             for (int j = 0; j < l; j++)                for (int k = 0; k < l; k++)                    ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j]) % MOD;        return ans;    }};int n, m;mat Pow(mat c, int k) {    mat ans = c;    k--;    while (k) {        if (k & 1)             ans = ans * c;        c = c * c;        k >>= 1;    }    return ans;}int main() {    while (scanf("%d%d", &n, &m) != EOF) {        mat a(n + 2);          a.s[0][0] = 3;        a.s[1][0] = 233;        for (int i = 2; i <= n + 1; i++)            scanf("%I64d", &a.s[i][0]);        mat tmp(n + 2);        tmp.init();        mat ans = Pow(tmp, m);        ans = ans * a;        printf("%I64d\n", ans.s[n + 1][0]);    }        return 0;}



The following is a program for getting the remainder of the nth entry of the Fibonacci series in the matrix's fast power solution, but it always runs an error.

According to the normal logic, only a [2] [2] = {1, 1, 1, 0} The n (a [0] [1]) of the Fibonacci series can be obtained from the Npower of the matrix. However, if you ignore this, you are requesting a [0] [1], a [1] [0], when you set the value of a [1] [1], your value of a [0] [0] has actually changed, as a result, the value of a [0] [1] You obtained is incorrect, which affects the values of a [1] [0] And a [1] [1.
Therefore, when traversing these four values, we cannot change any of them. We can only change the value after the change. Therefore, we can use several variables to first store the obtained new matrix values, as shown below:
# Include <stdio. h>
Int a [2] [2] = {1, 1, 0}, B [2] [2] = {1, 1, 0 }; // use two-dimensional arrays to represent the matrix used by the fast power algorithm
Int main ()
{
Int n, m, I, j, t, u;
Int a1, a2, a3, a4;
While (scanf ("% d", & n, & m )! = EOF)
{
If (m =-1 & n =-1) // end the entire algorithm when the input m. n value is-1.
Return 0;
/* The following is the calculation of the nth Fibonacci number */
If (n = 0)
A [0] [0] = 0;
Else if (n = 1 | n = 2)
A [0] [0] = 1;
Else
{
For (I = 3; I <= n; I ++)
{
A1 = a [0] [0] * B [0] [0] + a [0] [1] * B [1] [0];
A2 = a [0] [0] * B [0] [1] + a [0] [1] * B [1] [1];
A3 = a [1] [0] * B [0] [0] + a [1] [1] * B [1] [0];
A4 = a [1] [0] * B [0] [1] + a [1] [1] * B [1] [1];
A [0] [0] = a1;
A [0] [1] = a2;
A [1] [0] = a3;
A [1] [1] = a4;
}
}
T = a [0] [0];
A [0] [0] = 1; // resets the matrix.
A [0] [1] = 1;
A [1] [0] = 1;
A [1] [1] = 0;
If (m = 0)
A [0] [0] = 0;
Else if (m = 1 | m = 2)
A [0] [0] = 1;
Else
{
For (j = 3; j <= m; j ++)
{
A1 = a [0] [0] * B [0] [0] + a [0] [1] * B [1] [0];
A2 = a [0] [0] * B [0] [1] + a [0] [1] * B [1] [1];
A3 = a [1] [0] * B [0] [0] + a [1] [1] * B [1] [0];
A4 = a [1] [0] * B [0] [1] + a [1] [1] * B [1] [1];
A [0] [0] = a1;
A [0] [1] = a2;
A [1] [0] = a3;
A [1] [1] = a4;
}
}
U = a [0] [0];
A [0] [0] = 1; // resets the matrix.
A [0] [1] = 1;
A [1] [0] = 1;
A [1] [1] = 0;
T = t % u;
Printf ("% d \ n", t );
}
Return 0;
}

Another point is that the two items after the multiplication of your matrix are wrong. Let's make a comparison.

In this way, you can get the desired result.
... Remaining full text>

Matlab matrix

5*5 matrix, the input range is 0 ~ 4
Private static void Main (string [] args)
{
String;
String B;
While (true)
{
Console. WriteLine ("Input ");
A = Console. ReadLine ();
Console. WriteLine ("Input B ");
B = Console. ReadLine ();
If (IsValid (a) & IsValid (B) break;

Console. WriteLine ("error, re-enter the location again ");
}
Console. WriteLine (string. Format ("Now you see data ({0 },{ 1})", a, B ));
Console. Read ();
}

Private static bool IsValid (string)
{
Int B;
If (! Int. TryParse (a, out B) return false;
Return B <5 & B> = 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.