Problem Description
As we know, sequence in the form of an=a1+ (n−1) d are called arithmetic progression and sequence in the form of bn=b1qn−1 (q& gt;1,b1≠0) is called geometric progression. Huazheng wants to use these the simple matrix of sequences to generate a. Here's what he decides to do:
Use the geometric progression as the first row of the and the simple matrix:c0,n=bn
Use the arithmetic progression as the first column of the simple Matrix:cn,0=an
Calculate the item at N-th Row, m-thcolumn of the simple matrix as cn,m=cn−1,m+cn,m−1, where n≥1 and m≥1.
Given the sequences, Huazheng wants to know the value of cn,m, but he was too busy with his for tofigure it out. him to work it out. By the the-the-can assume that c0,0=0.
Input
The first line of input contains a number T indicatingthe number of test cases (t≤200).
For each test case, the there is only one line containing six non-negative integers b1,q,a1,d,n,m. (0≤n≤10000). All integers is less than 231.
Output
For each test case, the output a single lineconsisting the "Case #X: Y". X is the test Case number starting from1. Y is cn,m module 1000000007.
Sample Input
2
3 10 1 1 3 3
5 2 1 10 4 2
Sample Output
Case #1:423
Case #2:140
I have been using violence to optimize the problem for a long time. Although 5 minutes after the game, but can still be very lucky.
The main topic is to give the No. 0 line is geometric series, the No. 0 column is arithmetic progression, then there is a recursive C (n,m) = C (n-1, M) +c (n, m-1),
Requires arbitrary c (n, m), N <= 10000.
The key point is that the M range is M < 2^31, so the first is not enumerated for the row aspect.
Because of the line, line No. 0 is geometric series, but to line 1th is not, this is more tricky.
In addition, for line I, it is not difficult to find that C (i, J) is the previous line of the former J and plus AI.
But for C (n,m), it's tricky to say how much an AK contributes to it.
I first consider the equal and the linear can actually separate the calculation, this a little push to know.
I first consider how to solve the ratio, mainly to solve the problem of M too big.
It may be advisable to set F (n,i) to indicate the number of the nth rows (only the items obtained by B are considered, assuming that all a is 0)
Then f (0,i) = QF (0, I-1)
and f (1,i) = SUM (f (0, J)) (J <= i)
This step can be obtained by a single recursion f (1, i) = QF (1, i-1) + f (0, 1)
By matching a constant can be obtained (f (1, I) + f (0, 1)/(q-1)) = Q (f (1, i-1) +f (0,1)/(Q-1))
So by adding an F (0, 1)/(Q-1) can make the second line also become geometric series, and then subtract the F (0, 1)/(Q-1) at the A1 to ensure that the subsequent value is not changed.
In so doing, F (n, i) plus f (n-1, 1)/(Q-1) is geometric series and then lost in an.
This only requires maintaining the first and the AI for each line of geometric series.
In this case, for C (N, m), the maintenance of nth row geometric series to its contribution is the item m, i.e. f (n, 1) *q^ (m-1).
Note: The division in the above procedure is calculated by multiplying the inverse element.
The next step is to solve the AI's contribution to C (n, m). In fact, it is not difficult to find it and Yang Hui Triangle is the same, but need to figure out the relationship between the ranks. For example, Ai's contribution to the following:
Ai Ai ai
0 AI 2ai 3ai 4ai
0 AI 3ai 6ai 10ai
0 AI 4ai 10ai 20ai
0 AI 5ai 15ai 35ai
0 AI 6ai 21ai 56ai
To skew this matrix is the Yang Hui triangle and for the AI down the x row of the Y column,
Corresponds to the x-1 of line x+y-2 of the Yang Hui Triangle.
This contribution is C (x+y-2, x-1)
and x equals n-i+1.
So the AI's contribution to C (n, m) is C (N+m-1-i, n-i).
Then it was found that C (N+m-1-i, n-i) was a large combination of numbers, with Lucas always playing T.
Later found that although N+m-1-i is very big, but n-i is very small.
and C (n+m-1-i,n-i) = C (n+m-1-i-1, n-i-1) * (n+m-1-i)/(N-i).
The inverse of this step (n-i) is calculated.
In addition, n-i when I takes N (n+m-1-i, n-i) is 1.
Then save all of a beforehand, then add it from the last item and maintain C (N+m-1-i, n-i).
The problem can be optimized before this point. The intermediate process also has some inverse elements to optimize the time.
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#defineLL Long Long#defineMOD 1000000007using namespacestd; LL B1, q, A1, D, N, M; LL ans, dis[10005];voidinput () {ans=0; scanf ("%i64d%i64d%i64d%i64d%i64d%i64d", &B1, &q, &A1, &d, &n, &m);}//EXGCD//solving equation ax+by=d, i.e. Ax=d mod (b)//expand to find the inverse element//O (LOGN)voidEXGCD (ll A, ll B, LL &x, LL &y, LL &d) { if(b = =0) {x=1; Y=0; D=A; } Else{EXGCD (b, a%b, y, X, D); Y-= a/b*x; }}//a = BX (mod N)ll Moddiv (ll A, ll b) {ll x, Y, D; EXGCD (b, MOD, X, y, D); X= (x+mod)%MOD; X= (x*a/d)%MOD; returnx;}//Fast Power M^nll Quickpow (ll X, ll N) {ll a=1; while(n) {a*= n&1? X:1; A%=MOD; N>>=1 ; X*=x; X%=MOD; } returnA;}voidWork () {LL now=B1, TMP; now%=MOD; for(inti =1; I <= N; ++i) {tmp= Moddiv (now, Q-1); now= now+tmp; now%=MOD; Dis[i]= (a1+ (i-1) *d%mod-tmp)%MOD; Dis[i]= (dis[i]+mod)%MOD; } LL qt=1; Ans+ = qt*Dis[n]; for(inti = n1; i >0; --i) {qt= Moddiv ((qt* (n+m-1)-i)%mod+mod)%mod, N-i); Ans+ = (Qt*dis[i])%MOD; Ans%=MOD; } tmp= Quickpow (q, (M-1)% (mod-1))%MOD; Ans+ = now*tmp%MOD; Ans%=MOD; printf ("%i64d\n", ans);}intMain () {//freopen ("test.in", "R", stdin); intT; scanf ("%d", &T); for(intTimes =1; Times <= T; ++Times ) {printf ("Case #%d:", times); Input (); Work (); } return 0;}
ACM Learning process-hdu5490 Simple Matrix (math && inverse && fast Power) (2015 Hefei net race 07)