您的位置:首页 > 产品设计 > UI/UE

uva10689 Yet another Number Sequence

2015-04-08 16:12 337 查看
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 have to find the last m

digits of f(n) .

Input



The first line gives the number of test cases, which is 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
/*
就是快速幂斐波那契数 
加油!!!
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 &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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: