hdu 1588 矩陣運算的應用

2022-08-17 23:36:14 字數 1827 閱讀 1272

這題弄了兩天才做出來,還是去請教了竹教主。

貼個別人的解說吧,自己懶得寫了

把斐波那契數列轉化為矩陣:a=;

= a^n ;最後輸出右上角那項

或者用 = a^(n+1); 最後輸出右下角那項

我們用第乙個公式

所求即為a^b + a^(k+b) + a^(2*k+b) + ... + a^((n-1)*k+b)

=a^b * ( a^0 + a^k + a^(2*k) + ... + a^((n-1)*k) )

=a^b * ( (a^k)^0 + (a^k)^1 + (a^k)^2 + ...+ (a^k)^(n-1) );

b=a^k;

上式=a^b * ( b^0 + b^1 + b^2 + ... + b^(n-1) );

b^0 + b^1 + b^2 + ... + b^(n-1)用上篇介紹到的遞迴二分 方法求解

最後輸出矩陣的第二項(右上角)即可;

對於求解 b^0 + b^1 + b^2 + ... + b^(n-1)

我們也可以構造矩陣的方法。

我們來設定這樣乙個矩陣

b io i

其中o是零矩陣,i是單位矩陣

將它乘方,得到

b^2 i+b

o i乘三方,得到

b^3 i+b+b^2

o i乘四方,得到

b^4 i+b+b^2+b^3

o i用快速冪求出n方,讓第二項再與a^b相乘即可。

/*

* hdu1588/win.cpp

* created on: 2011-11-30

* author : ben

*/#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using

namespace std;

typedef long

long typec;

int mod;

//矩陣的最大階

const

int max_order = 4;

struct mymatrix

void init() }}

};mymatrix operator*(mymatrix ma, mymatrix mb) }}

return numc;

}mymatrix mpow(mymatrix ma, typec x)

for (; x; x >>= 1)

ma = ma * ma;

}return numc;

}inline void inita(mymatrix &m)

inline void initc(mymatrix &c, mymatrix b)

}c.num[0][2] = 1;

c.num[1][3] = 1;

c.num[2][2] = 1;

c.num[3][3] = 1;

}mymatrix getbrfromd(mymatrix &d)

}return res;

}int main()

return

0;}

hdu 1588 線段相交 並查集

判斷第k 個線段的集合中一共有幾條線段。先用並查集將相交的線段合併記錄,最後查詢sum陣列即可。include include includeusing namespace std struct node s 2004 struct edge e 2005 int fa 2005 int sum 2...

hdu 2254 矩陣的應用

題意 有向圖中求a點到b點路徑長度為t1 t2的路徑總數 離散數學中,有向圖的鄰接矩陣a表示所有點之間路徑長度為1的路徑數量,a n則表示路徑長度為n的路徑數量,故需要求某兩點在 a t1 a t2 的路徑數量之和 1 include2 include3 include4 const int n 3...

矩陣的運算

目錄 1.矩陣與數相乘 每一項都要乘 2.矩陣的加減運算 每一項都要乘 3.矩陣相乘 4.矩陣對應元素相乘 同型矩陣 5.矩陣的轉置 t 6.矩陣的共軛轉置 h 7.矩陣的逆 i 8.矩陣的試圖 a import numpy as np m1 np.mat 1,2,3 2,3,4 print m m...