uva10689 yet another number Sequence

Source: Internet
Author: User
Problem B
Yet another number Sequence
Input:standard input
Output:standard output
Time Limit:3 seconds

Let's define 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 different sequences. Given the 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 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 Output for sample input
4
0 1 11 3
0 1 42 4
0 1 22 4
0 1 21 4
89
4296
7711
946

Problem Setter:sadrul Habib Chowdhury
Special Thanks:derek Kisman, Member of Elite problem Setters ' Panel
/* is the fast power Fibonacci number refueling ... Time:2015-4-8 16:14 * * #include <cstdio> #include <cstring> #include <cmath> #include <algorithm
> Using namespace std;
int mod;
    struct matrix{int mat[2][2];
    int n;
        void Init (int _n) {n=_n;
    memset (Mat,0,sizeof (MAT)); } Matrix operator * (const matrix &AMP;B) const{matrix ret; ret.
        Init (n);
                    for (int i=0;i<n;i++) {for (int. j=0;j<n;j++) {for (int k=0;k<n;k++) {
                    RET.MAT[I][J]+=MAT[I][K]*B.MAT[K][J];
                Ret.mat[i][j]%=mod;
    }}} return ret;
}
};
    Matrix Pow_mat (Matrix A,int k) {matrix ret; Ret.
    Init (2);
    Ret.mat[0][0]=1;ret.mat[1][1]=1;

        while (k>0) {if (k&1) ret=ret*a;
        A=a*a;
    k>>=1;
} return ret;
    } int main () {int T;
    int a,b,c,d;
    Matrix F,trans; Trans.
    Init (2); F.init (2); trans.mat[0][0]=0; Trans.mat[0][1]=trans. mat[1][0]=trans.mat[1][1]=1;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d%d%d", &a,&b,&c,&d);
        Mod=1;while (d--) {mod*=10;}

        F.mat[0][0]=a;f.mat[0][1]=b;
        Matrix Ret=pow_mat (TRANS,C);
            /* for (int i=0;i<2;i++) {for (int j=0;j<2;j++) printf ("%d", ret.mat[i][j]);
        Puts ("");
        }*/F=f*ret;
    printf ("%d\n", f.mat[0][0]);
} 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.