Poj Solution 2837

http://poj.org/problem?id=2837

#include <stdio.h>


int ans[512][512];

int main( ) {
    int i, j, k, n;
    ans[0][0] = 1;

    scanf( "%d", &n );

    for( i=1; i<(1<<n); i<<=1 ) {
        for( j=i; j<i*2; j++ )
        for( k=i; k<i*2; k++ )
            ans[j][k] = ans[j-i][k-i];
        for( j=0; j<i; j++ )
        for( k=i; k<i*2; k++ )
            ans[j][k] = ans[j][k-i] + i*2;
        for( j=i; j<i*2; j++ )
        for( k=0; k<i; k++ )
            ans[j][k] = ans[j-i][k] + i*2 + (j-i==k?-1:0);
    }
    
    for( i=0; i<(1<<n); i++ )
    for( j=0; j<(1<<n); j++ )
        printf( "%d%c", ans[i][j], j==(1<<n)-1?'n':' ' );

    return 0;
}
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *