2018 Multi-University Training Contest 2-----hdu6313Hack It【构造题】

                 Hack It

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 668    Accepted Submission(s): 216
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.

 题意:

这道题很有意思,是一道构造题,在我做不出其他题的时候全程都在推这道题的规律。

看了杜老师的构造真的觉得很牛逼!!QAQ

打印数字n和一个n*n的矩阵,里面只有1和0,里面的1不可以四角构成一个矩形。

而且1的个数要不少于85000个,n的范围为1到2000。

思路:

用分块的思想,分成m*m个正方形,子正方形的边长也为m,且m必须为质数。

例如以m为5作为例子

为了打出2000*2000的矩阵

因此推出最小且适合的质数为47。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int mapp[47*47+2][47*47+2];
int main() {
	memset(mapp, 0, sizeof(mapp));
	int n = 47;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			for(int k = 0; k < n; k++){
				mapp[i*n+j][k*n+((k*j)+i)%n] = 1;
			}
		}
	}
	printf("2000\n");
	for(int i = 0; i < 2000; i++){
		for(int j = 0; j < 2000; j++){
			printf("%d",mapp[i][j]);
		}
		printf("\n");
	}
	return 0;
}

 

全部评论

相关推荐

Hello_WordN:咱就是说,除了生命其他都是小事,希望面试官平安,希望各位平时也多注意安全
点赞 评论 收藏
分享
头像
11-09 12:17
清华大学 C++
out11Man:小丑罢了,不用理会
点赞 评论 收藏
分享
美团 后端开发 总包n(15%是股票)
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务