Yet another number Sequence
Let's de?ne another number sequence, given by the following function:
F (0) = a
F (1) = b
F (n) = f (n−1) + f (n−2), n > 1
When a = 0 and B = 1, this sequence gives the Fibonacci sequence. Changing the values of A and
b, you can get many di?erent sequences. Given the values of a, B, you have the. nd the last m digits of
F (N).
Input
The. RST line gives the number of test cases, which was less than 10001. Each test case consists of a
Single line containing the integers a B N m. The values of A and B range in [0,100], value of n ranges in
[0,1000000000] and value of M ranges in [1,4].
Output
For each test case, print the last m digits of f (n). However, you should not print any leading zero.
Sample Input
4
0 1 11 3
0 1 42 4
0 1 22 4
0 1 21 4
Sample Output
89
4296
7711
946
Test instructions
Give you f[0],f[1] (b) for F[n]% (10^m)
Exercises
n is a bit large, the matrix fast power
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespacestd; typedefLong Longll;Const intN =100000+Ten;Const intMoD = 1e9 +7;Const intm[ -] = {1,Ten, -, +,10000};structMatrix {ll mat[2][2];} U,f,l;ll MOD; Matrix multi (matrix A, matrix B) {matrix ans; for(inti =0; I <2; i++) { for(intj =0; J <2; J + +) {Ans.mat[i][j]=0; for(intK =0; K <2; k++) Ans.mat[i][j]+ = a.mat[i][k] *B.mat[k][j]; ANS.MAT[I][J]%=MOD; } } returnans;} ll A,b,m; Matrix Powss (ll N) {matrix ans= L,p =U; while(n) {if(n&1) ans =multi (P,ans); N>>=1; P=multi (p,p); } returnans;}intMain () {intT; scanf ("%d",&T); while(t--) {ll n; scanf ("%lld%lld%lld%lld",&a,&b,&n,&m); U= {1,1,1,0}; L= {B,0A0}; MOD=M[m]; Matrix ans=POWSS (n); printf ("%lld\n", ans.mat[1][0]); } return 0;}
UVA-10689 yet another number Sequence matrix fast power