Hack It
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 747 Accepted Submission(s): 247
Special Judge
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)
Source
2018 Multi-University Training Contest 2
题目大意:
让你打个n*n由0和1组成的表,其中表中任意取四个1不能组成一个矩形,且1不能少于85000个.
解题思路:
我们先找找规律,比如要打个9*9的表
则有
100 100 100
100 010 001 //根据大佬的观察,发现每3行都是有一定规律的,即为9的开方行
100 001 010 //如前三行来看 1每一行就进一位,在第一行进位的数为零开始计,如果超过3则跳回开头继续计(mod3).
010 010 010
010 001 100
010 100 001 //接下来每一组的规律都是一样的,但每一组1的起始位置都会进一位
001 001 001
001 100 010
001 010 100
于是大佬们就推出以下公式:
for(int i=0;i<p;i++)
for(int j=0;j<p;j++)
for(int k=0;k<p;k++)
mapp[j+i*p][k*p+(i+k*j)%p]=1; //p要为质数
官方解释:取质数 p 使得 n=p^2n=p2,考虑构造 p^2p2 个有比较多1的01序列使得没有任意两个有超过一个公共1
也不知道大佬们是怎么看出规律的,被安排的明明白白。
上个代码:
#include<bits/stdc++.h>
#define MAXN 3333
using namespace std;
bool mapp[MAXN][MAXN];
int main()
{
int p=47;
for(int i=0;i<p;i++){
for(int j=0;j<p;j++){
for(int k=0;k<p;k++){
mapp[j+i*p][k*p+(i+k*j)%p]=1;
}
}
}
puts("2000"); //为保证打够点,这里取2000
for(int i=0;i<2000;i++){
for(int j=0;j<2000;j++)
printf("%d",mapp[i][j]);
printf("\n");
}
}