So Easy HDU 4565 矩陣快速冪

2021-08-07 03:21:22 字數 1851 閱讀 4716

a sequence s n is defined as:

where a, b, n, m are positive integers.┌x┐is the ceil of x. for example, ┌3.14┐=4. you are to calculate s n.

you, a top coder, say: so easy!

input

there are several test cases, each test case in one line contains four positive integers: a, b, n, m. where 0< a, m < 2 ^15, (a-1)^ 2< b < a^ 2, 0 < b, n < 2 ^31.the input will finish with the end of file.
output

for each the case, output an integer s n.
sample input

2 3 1 2013

2 3 2 2013

2 2 1 2013

sample output

4

144

題意:

如同題目公式描述的那樣.

思路:

矩陣快速冪,

我們設 x, y為未知數,那麼(a

+b√)

n = x + y * b√

的形式,其他x, y為整數,又因為(a

−1)2

< b

,那麼a - 1

<

a,於是ceil(b√

) = a; 於是 (a

−b√)

n<

1,而它與(a

+b√)

n 相加後展開式中會抵消掉待有根號的項, 而我們又知道 (a

+b√)

n+1 =xn

+1+y

n+1∗

b√= ( xn

+yn∗

b√) *(a+

b√) = (x

n∗a+

ynb)

+(a∗

yn+x

n)∗b

√ 由此我們可以構造出遞推式, xn

+1=x

n∗a+

yn∗b

, yn

+1=x

n+yn

∗a;

又因為我們知道(a

+b√)

n+(a

−b√)

n=2∗

xn, 而且(a

−b√)

n<

1,所以ceil((a

+b√)

n ) = 2∗

xn,**:

#include

#include

#include

#include

#include

using

namespace

std;

typedef

long

long ll;

ll a, b,n, m;

struct mat

mat operator *(const mat q)return c;

}};mat qpow(mat x, ll n)return ans;

}int main()return

0;}

HDU 4565 矩陣快速冪

矩陣快速冪模板 用矩陣快速冪實現斐波拉希數的推導 這個矩陣自乘n次。連續自乘n次的話就沒意思了,那還不如直接上fibonacci遞推公式呢。矩陣的魅力就在於它可以上快速冪。因為矩陣乘法滿足結合律麼 注意取模 加m因為有mod負 include include include include defi...

hdu4565矩陣快速冪

這題太坑了。剛開始以為可以用 a sqrt b 1 水過。結果tle,還一直想明明我logn的做法怎麼可能tle。0 1 實在無奈看的題解 a sqrt b n x y sqrt b a sqrt b n 1 a x b y x a y sqrt b 這樣可以構造矩陣 a b x 1 b y 還有記...

HDU 4565 So Easy 矩陣快速冪

題目大意 求 解題思路 這題跟hdu 2256 problem of precision類似,只不過這題是向上取整 有乙個隱藏的條件 a 1 2 b a 2 表明a 1 b a 也就是 a sqrt b n是小於1的 附上hdu 2256 problem of precision的題解鏈結 注意in...