Document directory
Description
SBB's mom SAA and SBB's father SCC have very strict requirements on him. before each meal, SBB should make a mathematical question, otherwise it will not be allowed to eat. Now the question of SCC is that there are n grains of rice in the bowl, SBB can take up to m grains from the bowl each time to eat, at least 1 granule is taken out, ask SBB how many ways to eat the food in the bowl, because the results may be very large, so we need to modulo P the final answer. SBB has no clue about this question, so he sent a text message to your friend. Please help him.
Input
The number of first behavior samples T (T <= 100 ).
Each sample is composed of three numbers n (1 <= n <= 10 ^ 9), m (1 <= m <= 10) and P (1 <= P <= 10 ^ 6.
Output
Output a row for each sample, and the result is P Modulo for the number of methods that SBB eats.
Sample Input
3
4 1 3
3 2 3
4 2 6
Sample output
1
0
5
Source
XTU OnlineJudge
I have been thinking about this question for a long time! I have made it recently !! Feel free! Haha...
If you only simulate the calculation process, it does not need to be timed out (1 <= n <= 10 ^ 9 ). But the implementation with matrix operations and the binary method will take 44 ms to get the AC !!
First, we know that n, m, p, and the methods for eating just now have a total
F (n) = f (n-1) + f (n-2) +... + f (n-m) (m-m> = 0 );
When m = 1, it is a constant series {1 }.
When m = 2j, f (n) = f (n-1) + f (n-2) is a Fibonacci sequence;
Perform matrix power operations by using the bipartite method.
When m = 3, construct the matrix .............
Finished.
# Include <iostream> <br/> # include <cmath> <br/> using namespace std; </p> <p >__ int64 rec [30] [10] [10]; <br/>__ int64 n, m, p; </p> <p> void multi (_ int64 s [] [10], _ int64 a [] [10], _ int64 B [] [10], _ int64 size) <br/>{< br/>__ int64 I, j, k; <br/>__ int64 x [10] [10], y [10] [10]; <br/> for (I = 0; I <size; I ++) <br/> for (j = 0; j <size; j ++) <br/>{< br/> x [I] [j] = a [I] [j]; <br/> y [I] [j] = B [I] [j]; <br/>}</p> <p> for (I = 0; I <size; I ++) <br/> for (j = 0; j <size; j ++) <br/> {<br/> s [I] [j] = 0; <br/> for (k = 0; k <size; k ++) <br/> s [I] [j] = (x [I] [k] * y [k] [j] + s [I] [j]) % p; <br/>}</p> <p> int main () <br/>{< br/> int I, j, k, t; <br/>__ int64 a [10] [10]; </p> <p> scanf ("% I64d", & t ); <br/> while (t --) <br/>{< br/> cin> n> m> p; </p> <p> for (I = 0; I <m; I ++) <br/> for (j = 0; j <m; j ++) <br/> if (j = 0) <br/> rec [0] [I] [j] = 1; <br/> else if (j = I + 1) <br/> rec [0] [I] [j] = 1; <br/> else <br/> rec [0] [I] [j] = 0; </p> <p> for (I = 1; I <30; I ++) <br/> multi (rec [I], rec [I-1], rec [I-1], m); </p> <p> memset (, 0, sizeof (a); <br/> for (I = 0; I <m; I ++) <br/> for (j = 0; j <m; j ++) </p> <p> if (m-1-i-j> 0) <br/> a [I] [j] = (1 <(m-2-i-j )); <br/> else if (m-1-i-j = 0) <br/> a [I] [j] = 1; </p> <p> for (I = 0; I <30; I ++) <br/>{< br/> if (n & (1 <I) <br/> multi (a,, rec [I], m); <br/>}< br/> printf ("% I64d/n", a [0] [M-1] % p ); <br/>}< br/> return 0; <br/>}