Problem Description
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(n2):
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.
Input
Nothing.
Output
The first line should be one positive integer n where 1≤n≤2000.
n lines following, each line contains only a string of length n consisted of zero and one.
Sample Input
(nothing here)
Sample Output
3
010
000
000
(obviously it’s not a correct output, it’s just used for showing output format
以前没遇到过这种题目,做了好久还是做不出来,其实答案的两个想法我都想到了,但是没有把他们结合到一起去
来自https://blog.csdn.net/axuhongbo/article/details/81226670
我的理解是把他们分成若干个小块,每个块里面的1的位置都要往后推,这样到最后一个块的时候正好就是所有状态都遍历一遍
然后下一行的最前面一个往后推1,后面所有的都是上一行对应状态-1 的格子,因为47^2>2000所以在重复行出现之前就已经推完了
唉,有点难理解,自己去别的博客看一看吧
#include<bits/stdc++.h>
using namespace std;
int num[2009][2009]={0};
int main()
{
printf("2000\n");
for(int i=0;i<2000;i++)
{
int k = 1;
for(int j=i/47;j<2000;j+=47)
{
num[i][j] = 1;
j+=i%47;
if(j>=(k*47))j-=47;
k++;
}
}
for(int i=0;i<2000;i++)
{
for(int j=0;j<2000;j++)
{
printf("%d",num[i][j]);
}
printf("\n");
}
}