Mathematician QSC

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 274    Accepted Submission(s): 141


Problem Description
QSC dream of becoming a mathematician, he believes that everything in this world has a mathematical law.

Through unremitting efforts, one day he finally found the QSC sequence, it is a very magical sequence, can be calculated by a series of calculations to predict the results of a course of a semester of a student.

This sequence is such like that, first of all, f(0)=0,f(1)=1,f(n)=f(n2)+2f(n1)(n2)Then the definition of the QSC sequence is g(n)=ni=0f(i)2. If we know the birthday of the student is n, the year at the beginning of the semester is y, the course number x and the course total score s, then the forecast mark is xg(ny)%(s+1).
QSC sequence published caused a sensation, after a number of students to find out the results of the prediction is very accurate, the shortcoming is the complex calculation. As clever as you are, can you write a program to predict the mark?
 

Input
First line is an integer T(1≤T≤1000).

The next T lines were given n, y, x, s, respectively.

n、x is 8 bits decimal integer, for example, 00001234.

y is 4 bits decimal integer, for example, 1234.
n、x、y are not negetive.

1≤s≤100000000
 

Output
For each test case the output is only one integer number ans in a line.
 

Sample Input
2 20160830 2016 12345678 666 20101010 2014 03030303 333
 

Sample Output
1 317
 【题意】
已知f(0)=0,f(1)=1,f(n)=f(n−2)+2∗f(n−1)(n≥2)


给你n,y,x,s的值

的值

【解题方法】可以看这篇blog,写得太好,我不好意思写了。http://blog.csdn.net/queuelovestack/article/details/52577212

【AC 代码】

//
//Created by just_sort 2016/9/20 11:13
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL mod;
struct Matrix{
    int row,col;
    LL mat[2][2];
    void init(int row,int col,bool ok=false){
        this->row=row,this->col=col;
        memset(mat,0,sizeof(mat));
        if(!ok) return ;
        for(int i=0; i<row; i++) mat[i][i]=1;
    }
    Matrix operator*(const Matrix &rhs){
        Matrix ret;ret.init(row,rhs.col);
        for(int k=0; k<col; k++){
            for(int i=0; i<row; i++){
                if(mat[i][k]==0) continue;
                for(int j=0; j<rhs.col; j++){
                    if(rhs.mat[k][j]==0) continue;
                    ret.mat[i][j]+=mat[i][k]*rhs.mat[k][j]%mod;
                    ret.mat[i][j]%=mod;
                }
            }
        }
        return ret;
    }
    Matrix operator^(LL n){
        Matrix ret,x=*this;
        ret.init(row,col,1);
        while(n){
            if(n&1) ret=ret*x;
            x=x*x;
            n>>=1;
        }
        return ret;
    }
}A,B;
int eluer(int n)
{
    int ans=1,i;
    for(i=2; i*i<=n; i++)
    {
        if(n%i==0)
        {
            n/=i;
            ans*=(i-1);
            while(n%i==0)
            {
                n/=i;
                ans*=i;
            }
        }
    }
    if(n>1) ans*=(n-1);
    return ans;
}
LL quickmod(LL a,LL n,LL m)
{
    LL res=1,temp=a;
    while(n)
    {
        if(n&1) res = res*temp%m;
        temp = (temp*temp)%m;
        n>>=1;
    }
    return res%m;
}
int  main()
{
    int T,n,y,x,s;
    LL z;
    scanf("%d",&T);
    while(T--)
    {
        A.init(2,2);
        A.mat[0][0]=0;A.mat[0][1]=1;
        A.mat[1][0]=1;A.mat[1][1]=2;
        B.init(2,2);
        B.mat[0][0]=0;B.mat[0][1]=0;
        B.mat[1][0]=1;B.mat[1][1]=0;
        scanf("%d%d%d%d",&n,&y,&x,&s);
        mod = 1LL*2*eluer(s+1);
        z=n;
        z*=y;
        Matrix ans = A^z;
        ans = ans*B;
        z = (ans.mat[0][0]%mod*ans.mat[1][0]%mod)%mod+mod;
        z = (z%mod + mod)/2;
        printf("%I64d\n",quickmod(1LL*x,1LL*z,s+1));
    }
    return 0;
}