Happy Necklace(矩陣快速冪)

2021-10-02 23:58:22 字數 1390 閱讀 6305

the first line of the input contains an integer t(1≤t≤10000), denoting the number of test cases.

for each test case, there is a single line containing an integer n(2≤n≤1018), denoting the number of beads on the necklace.

output

for each test case, print a single line containing a single integer, denoting the answer modulo 109+7.

sample input

223

sample output

3

4

思路:

通過規律發現是乙個1 * 3的矩陣乘上乙個3 * 3的矩陣;

而關係是f(n) = f(n-1)+f(n-3),所以就直接用矩陣快速冪。題目給了兩項了,只需通過規律再求一項即可。乘乙個3*3的矩陣,從而求出剩下的項。

完整**:

#include

using

namespace std;

typedef

long

long ll;

const

int mod=

1e9+7;

const

int maxn=3;

#define mod(x) ((x)%mod)

struct mat //矩陣結構體

unit;

mat mat_mul

(mat a,mat b)

//矩陣乘法

ret.m[i]

[j]=

mod(x);}

}return ret;

}void

init_unit()

return;}

mat pow_mat

(mat a,ll n)

//快速冪

a=mat_mul

(a,a)

; n>>=1;

}return ret;

}int

main()

else

if(n==4)

//找出規律算出第四項為6(當然找出規律算第一項也可以)

else

}return0;

}

Happy Necklace 矩陣快速冪

the first line of the input contains an integer t 1 t 10000 denoting the number of test cases.for each test case,there is a single line containing an ...

矩陣乘矩陣

7 15 矩陣a乘以b 15分 給定兩個矩陣a和b,要求你計算它們的乘積矩陣ab。需要注意的是,只有規模匹配的矩陣才可以相乘。即若a有r a 行 c a 列,b有r b 行 c b 列,則只有c a 與r b 相等時,兩個矩陣才能相乘。輸入格式 輸入先後給出兩個矩陣a和b。對於每個矩陣,首先在一行中...

spark矩陣向量 矩陣矩陣相乘

import org.apache.spark.util.val paramatrix1 sc.parallelize list vector 2,2,4 vector 3,2,1 vector 1,3,2 首先,在spark shell中將乙個矩陣按照行進行並行化,val vec1 vector ...