Vijos1067 Warcraft III worries Matrix Multiplication

Source: Internet
Author: User
/* We can use the above method to obtain the nth item of any linear recursive formula. The corresponding matrix construction method is: (n-1) * (n-1) in the upper right corner) fill in 1 on the primary diagonal line in the small matrix, fill in the corresponding coefficient in the nth line of the matrix, and fill in 0 in other places. For example, we can use the following matrix multiplication to calculate the K entry of F (n) = 4f (n-1)-3f (n-2) + 2f (n-4: [[0 1 0 0] [F (K-4)] [F (K-3)] [0 0 1 0] * [F (K-3)] = [F (K-2)] [0 0 0 1] [F (K-2)] [F (k-1)] [2 0-3 4] [F (k-1)] [F (k)] in this question, we need to calculate F (n) = sum (f (n-I )) 1 <= I <= K. Therefore, a K * k matrix is constructed, and the n-k power is obtained. Then, the first K items are constructed and then multiplied by the matrix obtained above. F (N ); my constructor uses k = 2 as an example [F (1), F (2)] * [[0 1] ^ (n-k) = [F (n-1 ), F (n)] [1] the construction method of left multiplication and right multiplication is actually the same. Because of my personal habits, I like left multiplication, So I constructed the method above ,*/

 

Background

Watcher-Warden, who has been serving as a prison inspection task in the capital of the night elves, is a growth line. The watcher warden has a skill named "Flickering ", this skill can be transferred to the following prison to check her. She is relatively lazy. Generally, she does not check all the prisons, but just enters from the entrance, and then completes the task even if she exits.

Description

Warden, who is not well-developed, is thinking about a problem recently. Her flash skills can be upgraded, and her K-level flash skills can move at most K prisons forward, there were a total of n prisons for inspection. She went in from the entrance, there were n prisons along the way, and she would not go back. Of course she did not visit every prison, but she had to go to the nth prison, because the prison exit was there, but she was not necessarily going to the seventh prison.

Watcher Warden wants to know how many solutions she has to visit n prisons when she has K-level flash skills?

Format input format

The first line is the skill level K (1 <= k <= 10)
The second row is the number of prisons N (1 <=n <= 2 ^ 31-1)

Output Format

Because there are many solutions, output the results after mod 7777777.

Example 1 input 1 [copy]

24
Sample output 1 [copy]

5
Restrictions

1 s for each test point

Prompt

Set the prison number 1 2 3 4 and the flash skill to level 2,
A total of five solutions
→ 1 → 2 → 3 → 4
→ 2 → 3 → 4
→ 2 → 4
→ 1 → 3 → 4
→ 1 → 2 → 4

TIPS: int64 is recommended, otherwise it may overflow

 

# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <string> # include <cmath> # define maxn 20 # define mod 7777777 using namespace STD; struct matrix {int size; long modulo; long element [maxn] [maxn]; void setsize (INT); void setmodulo (long); Matrix Operator * (matrix ); matrix power (INT) ;}; void matrix: setsize (int A) {for (INT I = 0; I <A; I ++) for (Int J = 0; j <A; j ++) El Ement [I] [J] = 0; size = A;} void matrix: setmodulo (long a) {modulo = A;} matrix :: operator * (matrix PARAM) {matrix product; product. setsize (size); product. setmodulo (Modulo); For (INT I = 0; I <size; I ++) for (Int J = 0; j <size; j ++) for (int K = 0; k <size; k ++) {product. element [I] [J] + = (element [I] [k] * Param. element [k] [J]); product. element [I] [J] % = modulo;} return product;} matrix: Power (INT ex P) {matrix res, A; A = * This; Res. setsize (size); Res. setmodulo (MOD); For (INT I = 0; I <size; I ++) res. element [I] [I] = 1; while (exp) {If (exp & 1) RES = res * A; exp> = 1; A = A * ;} return res;} int N, K; long f [20]; matrix m; int main () {While (~ Scanf ("% d", & K, & N) {f [0] = 0; For (INT I = 1; I <= K; I ++) {f [I] = 0; For (Int J = 1; j <= I; j ++) f [I] = (F [I] + F [I-j]) % MOD; F [I] ++;} If (n <= K) printf ("% LLD \ n", F [N]); else {M. setmodulo (MOD); M. setsize (k); For (INT I = 0; I <K-1; I ++) M. element [I + 1] [I] = 1; for (INT I = 0; I <K; I ++) M. element [I] [k-1] = 1; M = m. power (n-k); long ans = 0; For (INT I = 0; I <K; I ++) ans = (ANS + (F [I + 1] * m. element [I] [k-1]) % mod) % MOD; printf ("% LLD \ n", ANS) ;}} return 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.