Пример #1
0
    void processMatrix(MATRIX & mat, int match, FuncType &processor)
    {
        set<int> row;
        set<int> col;
        for (int i = 0; i < mat.size(); ++i)
        {
            for (int j = 0; j < mat[i].size(); ++j)
            {
                if (mat[i][j] == match)
                {
                    row.insert(i);
                    col.insert(j);
                }
            }
        }


        // Process matrix
        for (int i : row)
        {
            processor(mat, i, match, ROW);
        }

        for (int i : col)
        {
            processor(mat, i, match, COL);
        }
    }
Пример #2
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;

}
Пример #3
0
 /*
  * function members
  */
 explicit TSP(const MATRIX &_costs)
     : MATRIX(_costs),
     current_tour(_costs.size()),
     best_tour(_costs.size()),
     epsilon(0.000001),
     n(_costs.size()),
     updatecalls(0),
     swap_count(0),
     slide_count(0),
     reverse_count(0),
     improve_count(0) {
         pgassert(n == MATRIX::size());
         bestCost = MATRIX::tourCost(best_tour);
         current_cost = MATRIX::tourCost(current_tour);
         pgassert(bestCost == current_cost);
     }
Пример #4
0
void printMatrix(MATRIX mat){
    for (int i=0;i<mat.size();++i){
        for (int j=0;j<mat[i].size();++j){
            printf("%3lld",mat[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}
Пример #5
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;

}
Пример #6
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;

}
Пример #7
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  );
    }

}
Пример #8
0
    static void rotateNinety(MATRIX &matrix)
    {
        int N = matrix.size();
        int ring = ceil(N/2);
        for (int i = 0; i < ring; ++i)
        {
            int cap = N - i - 1;
            for (int j = i; j < cap; ++j)
            {
                int tmp1 = matrix[j][cap];
                matrix[j][cap] = matrix[i][j];

                int tmp2 = matrix[cap][cap - (j - i)]; // fix
                matrix[cap][cap - (j - i)] = tmp1;

                tmp1 = matrix[cap - (j - i)][i];
                matrix[cap - (j - i)][i] = tmp2;

                matrix[i][j] = tmp1;
            }
        }
    }
Пример #9
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;
}