Beispiel #1
0
int main(){

    freopen("in.txt","r",stdin);

    int p,q;
    int n;

    MATRIX initial;
    initial.resize(2);
    for (int i=0;i<initial.size();++i){
        initial[i].resize(2);
    }
    initial[1][0]=1;
    int kase;
    scanf("%d",&kase);

    for (int kk=1;kase--;++kk){

        scanf("%d %d %d",&p,&q,&n);
        printf("Case %d: ",kk);
        if ( n==0 ){
            printf("2\n");
        }else if ( n==1 ){
            printf("%d\n",p);
        }else if ( n==2 ){
            printf("%llu\n",LLL(p)*p-2*q);
        }
        else{
            initial[0][0]=p;
            initial[0][1]= -q;

            MATRIX result=powerMatrix( initial ,n-2 );

            LLL base1=p;
            LLL base2=LLL(p)*p-2*LLL(q);

            LLL rr=result[0][0]*(base2/2) +result[0][1]*(base1/2) ;
            rr+=result[0][0]*(base2/2) +result[0][1]*(base1/2) ;

            if ( base2 & 1 ){
                rr+=result[0][0];
            }
            if ( base1 & 1 ){
                rr+=result[0][1];
            }

            //printMatrix(result);
            printf("%llu\n",LLL(rr) );
        }
    }
    return 0;

}
Beispiel #2
0
int main(){

    freopen("in.txt","r",stdin);

    LLL n;

    MATRIX initial;

    initial.resize(3);
    for (int i=0;i<3;++i){
        initial[i].resize(3);
    }
    initial[0][0]=initial[0][1]=initial[0][2]=initial[1][0]=initial[2][2]=1;

    MATRIX save;
    save.resize(3);
    for (int i=0;i<save.size();++i){
        save[i].resize(1,1);
    }

    for (int kk=1;true;++kk){
        scanf("%lld %d",&n,&mod);

        if (!n && !mod)return 0;


        printf("Case %d: %lld %d ",kk,n,mod);

        if ( n==0 || n==1 ){
            printf("%d\n", 1%mod  );
            continue;
        }

        MATRIX result=powerMatrix( initial,n-1 );
        result=multiply( result,save );
        printf("%d\n",result[0][0]%mod  );
    }

}
Beispiel #3
0
MATRIX multiply(MATRIX &a, MATRIX &b) {

    MATRIX res;
    res.resize( a.size() );
    for (int i=0;i<res.size();++i){
        res[i].resize( b[i].size() );
    }

    for (int i = 0; i < a.size(); i++)
        for (int j = 0; j < b.size(); j++)
            for (int k = 0; k < a[i].size() ; k++) //col of A and row of B must be same which is equal to a[i].size()
                res[i][j] = ( res[i][j] + ( ( (a[i][k] % mod ) * (b[k][j] % mod))) % mod ) %mod;

    return res;

}
Beispiel #4
0
int main(){

    //freopen("in.txt","r",stdin);

    int n,m;

    MATRIX initial;
    initial.resize(DIM);
    for (int i=0;i<initial.size();++i){
        initial[i].resize(DIM);
    }

    for (int i=0;i<DIM;++i){
        for (int j=0;j<DIM;++j){
           initial[i][j]=1;
        }
    }
    initial[1][1]=0;

    int kase;
    LLL x,y;
    scanf("%d",&kase);
    for (int kk=1;kase--;++kk){

        scanf("%d %d %d %d",&x,&y,&n,&m);
        mod=LLL ( pow(10,m)+1e-6 );
        printf("Case %d: ",kk);
        if( n==0 ){
            printf("%d\n",x%mod);
            continue;
        }else if ( n==1 ){
            printf("%d\n",y%mod);
            continue;
        }

        MATRIX result=powerMatrix( initial , n-1 );

        printf("%d\n",( result[0][0]*y+result[0][1]*x )  %mod);

    }
    return 0;

}
Beispiel #5
0
int main(){
    freopen("in.txt","r",stdin);

    int kase;
    scanf("%d",&kase);
    LL n,b,d;
    MATRIX ini;
    ini.resize(DIM);
    for (int i=0;i<ini.size();++i){
        ini[i].resize(DIM);
    }


    ini[0][1]=1;
    ini[1][1]=1;
    ini[1][0]=0;

    for (int kk=1;kase--;++kk){
        scanf("%lld %lld %lld %lld",&n,&b,&d,&mod);
        printf("Case %d: ",kk);

        ini[0][0]=b;

        d%=mod;

        if ( n==1 ){
            printf("%lld\n",d);
        }else if (n==2 ){
            printf("%lld\n",(d*( (1+b)%mod ) ) %mod);
        }else{
            MATRIX rr=powerMatrix(ini,n-2);
            //printf("shaon %lld\n",rr[0][1]);
            printf("%lld\n", mult( d , mult( rr[0][0]%mod,(1+b)%mod ) %mod + ( rr[0][1]*1 )%mod )%mod    );
        }
    }
    return 0;
}