CDMA(牛客第八场构造题)

链接:https://ac.nowcoder.com/acm/contest/888/C
来源:牛客网

Gromah and LZR have entered the third level. There is a blank grid of size m\times mm×m, and above the grid is a word “CDMA”.

In CDMA Technology, a Technology about computer network, every network node should be appointed a unique binary sequence with a fixed and the same length. Moreover, if we regard 0_{}0

in the sequence as -1_{}−1

, and regard 1_{}1

as +1_{}+1

, then these sequences should satisfy that for any two different sequences s,t_{}s,t

, the inner product of s,t_{}s,t

should be exactly 0_{}0

.

The inner product of two sequences s,t_{}s,t

with the same length n_{}n

equals to \sum_{i=1}^{n} s_it_i∑

So, the key to the next level is to construct a grid of size m\times mm×m, whose elements are all -1_{}−1

or 1_{}1

, and for any two different rows, the inner product of them should be exactly 0_{}0

.

In this problem, it is guaranteed that m_{}m

is a positive integer power of 2_{}2

and there exists some solutions for input m_{}m

. If there are multiple solutions, you may print any of them.
输入描述:
Only one positive integer m_{}m

in one line.

m \in {2^k ; | ;k = 1, 2, \cdots, 10}m∈{2
k
∣k=1,2,⋯,10}
输出描述:
Print m_{}m

lines, each contains a sequence with length m_{}m

, whose elements should be all -1_{}−1

or 1_{}1

satisfying that for any two different rows, the inner product of them equals 0_{}0

.

You may print multiple blank spaces between two numbers or at the end of each line, and you may print any number of blank characters at the end of whole output file.
示例1
输入
复制
2
输出
复制
1 1
1 -1
说明
The inner product of the two rows is 1\times(-1) + 1\times 1 = 01×(−1)+1×1=0.
构造1,-1矩阵使得每一行对应位置的数乘积加起来都为零。
典型的构造题,不禁想起来课本上的循环赛程表,根据小的去构造大的,通过给出的m=2的去构造m=4的,然后找出规律后构造出m=8的发现也对,就写了。代码中解释
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
 
int a[1030][1030];
int n;
 
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        a[1][1]=1;a[1][2]=1;
        a[2][1]=-1;a[2][2]=1;
        if(n==2)
        {
            for(int i=1;i<=2;i++)
            {
                for(int j=1;j<=2;j++)
                    printf("%d ",a[i][j]);
                printf("\n");
            }
        }
        else
        {
            int y=4;
            while(y<=n)
            {
                for(int i=y/2+1;i<=y;i++)//左下方的矩阵就等于左上方的矩阵的相反数
                {
                    for(int j=1;j<=y/2;j++)
                    a[i][j]=-a[i-y/2][j];
                }
                for(int i=1;i<=y/4;i++)//右上方的矩阵前两行左下方的矩阵前两行
                {
                    for(int j=y/2+1;j<=y;j++)
                    {
                        a[i][j]=a[y/2+i-1][j-y/2];
                    }
                }
                for(int i=y/4+1;i<=y/2;i++)//右上方的矩阵后两行等于左下方的矩阵后两行的相反数
                {
                    for(int j=y/2+1;j<=y;j++)
                    {
                        a[i][j]=-a[y/2+i-1][j-y/2];
                    }
                }
                for(int i=y/2+1;i<=y;i++)//右下方的矩阵就等于右上方的矩阵
                {
                    for(int j=y/2+1;j<=y;j++)
                    a[i][j]=a[i-y/2][j];
                }
                y*=2;
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    printf("%d ",a[i][j]);
                }
                printf("\n");
            }
        }
    }
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值