"Topic link" click here~~
"The main topic"
Let's define another number sequence, given by the following function:
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 ofa and b , you can get many different sequences. GivenThe values of a, b, you had to find the last m digits of f (n) .
Input
The first line gives the number of test cases, which was less than 10001. Each test case consists of a containing the integersa b N m. The values of a and B range in[0,100], value of n ranges in [0, 1000000000] a nd value ofm ranges in [1, 4].
Input
The first line gives the number of test cases, which was less than 10001. Each test case consists of a containing the integersa b N m. The values of a and B range in[0,100], value of n ranges in [0, 100000000 0] and value ofm 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.
"problem-solving ideas"
Similar to the Fibonacci sequence, it is worth noting that the title is not for the simple f (n), but for the F (n)%f (m), which is known by the topic,
m ranges in [1, 4]. Then define a mod array const int mod[5]= {0,10,100,1000,10000};
Code
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < math.h>using namespace Std;const int mod[5]= {0,10,100,1000,10000};const int mod =1e9+7; #define LL long Longll x,y,n,m, i,j;struct matrlc{int mapp[2][2];} ans,base; MATRLC unit= {1,0,0,1}; MATRLC mult (MATRLC A,MATRLC b) {MATRLC C; for (int i=0, i<2; i++) for (int j=0; j<2; J + +) {c.mapp[i][j]=0; for (int k=0; k<2; k++) c.mapp[i][j]+= (A.mapp[i][k]*b.mapp[k][j])%mod[m]; C.MAPP[I][J]%=MOD[M]; } return C;} void Pow1 (int n) {base.mapp[0][0] =base.mapp[0][1]=base.mapp[1][0]=1; base.mapp[1][1]=0; Ans.mapp[0][0] = ans.mapp[1][1] = 1;//ans initialized to the unit matrix ans.mapp[0][1] = ans.mapp[1][0] = 0; while (n) {if (n&1) Ans=mult (ans,base); Base=mult (base,base); n>>=1; }//Return ans.mapp[0][1];} int main () {int t; scanf ("%d", &t); while (t--) {scanf ("%lld%lld%lld%lld", &x,&y,&n,&m); if (n==0) return X; else if (n==1) return Y; else {pow1 (N-1); LL result= (ans.mapp[0][0]*y+ans.mapp[0][1]*x)%mod[m]; printf ("%lld\n", result); }} return 0;}
"Matrix fast Power" UVA 10698 g-yet another number Sequence