HDU5015-233 Matrix(矩陣快速冪),hdu5015-233matrix

來源:互聯網
上載者:User

HDU5015-233 Matrix(矩陣快速冪),hdu5015-233matrix

題目連結


題意:給定一個矩陣的第0列的第1到n個數,第一行第1個數開始每個數分別為233, 2333........,求第n行的第m個數。

思路:將第一行的數全部右移一位,用前一列遞推出下一列,構造矩陣,類似如下 
1 0 0 0 0 0 0 
1 10 0 0 0 0 0 
0 1 1 0 0 0 0 
0 1 1 1 0 0 0 
0 1 1 1 1 0 0 
0 1 1 1 1 1 0

代碼:

#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;}



下面是一個矩陣快速冪解斐波那契數列第n項對第m項取餘的程式,但是總是運行出錯,解

按照正常的邏輯是只要求a[2][2]={1,1,1,0}這個矩陣的n次方就可以得到斐波那契數列的第n項(即a[0][1])的值。但是你忽略了一點,就是你在求a[0][1],a[1][0],a[1][1]的值的時候你的a[0][0]的值其實已經改變了,導致你求得的a[0][1]的值不正確,繼而影響到a[1][0]和a[1][1]的值。
因此,我們在遍曆這四個值的時候是不能改變其中任何一個的,只有改完之後才能去改變它的值。所以我們可以用幾個變數先將求得的新矩陣各值存起來,如下:
#include<stdio.h>
int a[2][2]={1,1,1,0},b[2][2]={1,1,1,0};//使用兩個二維數組表示快速冪演算法要用到的矩陣
int main()
{
int n,m,i,j,t,u;
int a1,a2,a3,a4;
while(scanf("%d %d",&n,&m)!=EOF)
{
if(m==-1 && n==-1)//當輸入的m.n值為-1時結束整個演算法
return 0;
/*以下是對於第n項斐波那契數的計算*/
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;//重設矩陣
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;//重設矩陣
a[0][1]=1;
a[1][0]=1;
a[1][1]=0;
t=t%u;
printf("%d\n",t);
}
return 0;
}

還有一點,就是你的矩陣相乘後兩項寫錯了,自己對比一下改過來吧。

這樣就能得到你想要的結果了。
...餘下全文>>
 
Matlab matrix(矩陣)問題

5*5的矩陣,那麼輸入的範圍是0~4
private static void Main(string[] args)
{
string a;
string b;
while (true)
{
Console.WriteLine("Input a");
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 a)
{
int b;
if (!int.TryParse(a, out b)) return false;
return b < 5 && b >= 0;
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.