POJ 2961 Sylvester construction 哈达玛矩阵

来源:互联网 发布:淘宝极有家和中国质造 编辑:程序博客网 时间:2024/04/25 04:13
点击打开链接

Sylvester construction
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1201 Accepted: 556

Description

A Hadamard matrix of order n is an n x n matrix containing only 1s and -1s, called Hn, such that HnHnT = nIn where In is the n x n identity matrix. An interesting property of Hadamard matrices is that they have the maximum possible determinant of any n x n matrix with elements in the range [-1, 1]. Hadamard matrices have applications in errorcorrecting codes and weighing design problems. 

The Sylvester construction is a way to create a Hadamard matrix of size 2n given Hn. H2n can be constructed as: 
 
for example: 
 
and so on. 

In this problem you are required to print a part of a Hadamard matrix constructed in the way described above.

Input

The first number in the input is the number of test cases to follow. For each test case there are five integers: n, x, y, w and h. n will be between 1 and 262 (inclusive) and will be a power of 2. x and y specify the upper left corner of the sub matrix to be printed, w and h specify the width and height respectively. Coordinates are zero based, so 0 ≤ x,y < n. You can assume that the sub matrix will fit entirely inside the whole matrix and that 0 < w,h ≤ 20. There will be no more than 1000 test cases.

Output

For each test case print the sub matrix followed by an empty line.

Sample Input

32 0 0 2 24 1 1 3 3268435456 12345 67890 11 12

Sample Output

1 11 -1-1 1 -11 -1 -1-1 -1 11 -1 -1 1 1 -1 -1 1 1 -1 -1-1 -1 1 1 -1 -1 1 1 -1 -1 11 1 1 -1 -1 -1 -1 1 1 1 1-1 1 -1 -1 1 -1 1 1 -1 1 -11 -1 -1 -1 -1 1 1 1 1 -1 -1-1 -1 1 -1 1 1 -1 1 -1 -1 1-1 -1 -1 -1 -1 -1 -1 1 1 1 11 -1 1 -1 1 -1 1 1 -1 1 -1-1 1 1 -1 -1 1 1 1 1 -1 -11 1 -1 -1 1 1 -1 1 -1 -1 1-1 -1 -1 1 1 1 1 1 1 1 11 -1 1 1 -1 1 -1 1 -1 1 -1

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2004


哈达玛(Hadamard)矩阵是由+1和-1元素构成的且满足Hn*Hn’=nI(这里Hn’为Hn的转置,I为单位方阵)n阶方阵。
本题要求打印构造出来的Hadamard矩阵的一部分。

//356K16MS#include<stdio.h>int H[2][2]={{1,1},{1,-1}};long long dfs(long long x,long long y){    if(x<2&&y<2)return H[x][y];    int i=0,j=0;    if(x>=2)    {        i=1;        while(i*2<=x)            i*=2;        x-=i;    }    if(y>=2)    {        j=1;        while(j*2<=y)            j*=2;        y-=j;    }    if(i==j)return -dfs(x,y);//位于4区    else if(i>j)return dfs(x,y+j);//位于3区    else return dfs(x+i,y);//位于2区}int main(){    int t;    scanf("%d",&t);    while(t--)    {        long long n,x,y,w,h,i,j;        scanf("%I64d%I64d%I64d%I64d%I64d",&n,&x,&y,&w,&h);        for(i=y;i<y+h;i++)        {            for(j=x;j<x+w-1;j++)                printf("%d ",dfs(i,j));            printf("%d\n",dfs(i,j));        }        printf("\n");    }    return 0;}



0 0
原创粉丝点击