【HDU - 6313】Hack It

版权声明:本文为博主原创文章……懂吗?要尊重别人的劳动成果呐 https://blog.csdn.net/Tiw_Air_Op1721/article/details/82156963

@Hack It@


@题目描述 - English@

Tonyfang is a clever student. The teacher is teaching he and other students “bao’sou”.
The teacher drew an n*n matrix with zero or one filled in every grid, he wanted to judge if there is a rectangle with 1 filled in each of 4 corners.
He wrote the following pseudocode and claim it runs in O(n^2):

let count be a 2d array filled with 0s
iterate through all 1s in the matrix:
suppose this 1 lies in grid(x,y)
iterate every row r:
if grid(r,y)=1:
++count[min(r,x)][max(r,x)]
if count[min(r,x)][max(r,x)]>1:
claim there is a rectangle satisfying the condition
claim there isn’t any rectangle satisfying the condition

As a clever student, Tonyfang found the complexity is obviously wrong. But he is too lazy to generate datas, so now it’s your turn.
Please hack the above code with an n*n matrix filled with zero or one without any rectangle with 1 filled in all 4 corners.
Your constructed matrix should satisfy 1≤n≤2000 and number of 1s not less than 85000.

@大致题意@

省去对错误程序的分析(因为那不是重点……),这道题就是让你构造出n*n的一个01矩阵,使得矩阵中的每个子矩阵的四个角落不全为1,且1的数量不小于85000个。

@分析@

看着数据范围若有所思……
2000 44 ,而2000*44 = 88000……虽然差距还是有,但是从宏观上来看,这好像差得不是很远啊……
所以这道题一定要从 n 入手。

我们考虑将n*n的矩阵分为 n n 个矩阵块,每个块的大小都是 n n 的,每个块里每行每列都只有一个1。这样的话理想状况下就会有 n n n 个1了。

【9*9的矩阵分块举例:】

XXX | XXX | XXX
XXX | XXX | XXX
XXX | XXX | XXX
———————–
XXX | XXX | XXX
XXX | XXX | XXX
XXX | XXX | XXX
———————–
XXX | XXX | XXX
XXX | XXX | XXX
XXX | XXX | XXX

我们令最左边那一列上的块为第0列上的块,最右边那一列上的块为第 n 1 上的块。同理对行进行定义,则每一块就有一个坐标(x, y)。
定义每个块(x, y)的偏移量 p = x y mod n 。然后先令每一块的主对角线为1,再让所有数向右移动 p 步,如果超出块的右边界则回到块的左边界。实际实现可以用取模运算。

【还是拿9*9的矩阵举例子】

100 | 100 | 100
010 | 010 | 010
001 | 001 | 001
———————–
100 | 100 | 100
010 | 010 | 010
001 | 001 | 001
———————–
100 | 100 | 100
010 | 010 | 010
001 | 001 | 001

【上面是未偏移前的矩阵】

100 | 100 | 100
010 | 010 | 010
001 | 001 | 001
———————–
100 | 010 | 001
010 | 001 | 100
001 | 100 | 010
———————–
100 | 001 | 010
010 | 100 | 001
001 | 010 | 100

【上面是偏移后的矩阵,即我们的答案矩阵】
接下来我们将证明,当 n 为质数时,我们构造出来的矩阵总是合法。
令 m = n ,假设第(x1, y1)块内部的(i1, j1),第(x1, y2)块内部的(i1, j2),第(x2, y1)块内部的(i2, j1),第(x2, y2)块内部的(i2, j2)同时为1,那么:
i 1 + x 1 y 1 = j 1 mod m ( 1 )
i 1 + x 1 y 2 = j 2 mod m ( 2 )
i 2 + x 2 y 1 = j 1 mod m ( 3 )
i 2 + x 2 y 2 = j 2 mod m ( 4 )
可以发现(1) - (2) = (3) - (4),即:
x 1 y 1 x 1 y 2 = x 2 y 1 x 2 y 2 = j 1 j 2 mod m
通过移项可以得到:
( x 1 x 2 ) ( y 1 y 2 ) = 0 mod m
因为 m 是质数,所以要么x1 = x2,要么 y1 = y2,而两者都是不符合条件的。

注意最接近2000的平方根的数44不是一个质数,43所构造出来的矩阵1的数量太少,所以我们扩大一点矩阵使得 n = 47 ,然后只需要输出左上角那2000*2000的矩阵即可。

@代码 and 小剧场@

我:44为什么不是个质数啊啊啊啊啊啊
队友(一脸看智障的表情):???
我:如果44是个质数这道题就做出来啦!
队友:那你就假设44是……
我:去去去!
队友:考虑过43吗?
(我试了一下,发现1的数量不够)
我:不行
队友:那47也考虑一下啊
我and队友:好像47*47装不下啊……(woc完美错过正解)
队友:那……你自己处理吧
(队友 ran away )
(于是我在43*43剩下的边缘疯狂填补能填补的1……直到比赛结束也没弄出来85000)
(倒是隔壁队用随机化算法弄出一个有84539个1的矩阵……然后84539就成一个梗了2333)
【以上可以忽略】

#include<cmath>
#include<cstdio>
const int MAXM = 47;
const int MAXN = MAXM*MAXM;
int a[MAXN + 5][MAXN + 5];
int main() {
    int n = 2000;
    printf("%d\n", n);
    int m = MAXM;
    for(int i=0;i<m;i++)
        for(int j=0;j<m;j++) {
            int x = i*m, y = j*m;
            int p = i*j%m;
            for(int k=0;k<m;k++)
                a[x+k][y+(k+p)%m] = 1;
        }
    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++)
            printf("%d", a[i][j]);
        puts("");
    }
}

@END@

就是这样,新的一天里,也请多多关照哦(ノω<。)ノ))☆.。~

猜你喜欢

转载自blog.csdn.net/Tiw_Air_Op1721/article/details/82156963
hdu
今日推荐