Description
给出下面一个代码用来判断一个n×nn×n的0101矩阵中是否存在一个子矩形的四个角的值均为11
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
要求构造一个的0101矩形使得1≤n≤20001≤n≤2000且至少有8500085000个位置是11,使得其不存在四个角均为的子矩形但是上述代码的运行结果会判断为存在
Input
无
Output
输出nn以及这个的0101矩阵
Sample Input
无
Sample Output
无
Solution
显然只要一行有两个11且不存在四个角都为的矩形即为反例,问题转化为构造nn个序列使得任意两个序列不存在两个都为的位置
给定0≤a,b,c,d<p0≤a,b,c,d<p,其中pp为素数,假设对于两个不同的满足a+kb=c+kd mod pa+kb=c+kd mod p,则有a=c,b=da=c,b=d,故取不同的二元组(a,b)(a,b),通过a+kb mod pa+kb mod p可以构造出来p2p2个长度为pp的序列,且这些序列中任意两个序列不存在两个位置的值相同,那么我们取素数满足472>2000472>2000,把20002000分成若干长度为4747的段,第kk段的第个值为11,其余值为,则可以构造出满足条件的矩阵
Code
#include<cstdio>
using namespace std;
int main()
{
int n=2000,k=47;
printf("%d\n",n);
int num=0;
for(int i=0;i<k;i++)
for(int j=0;j<k;j++)
{
int m=i*k+j+1;
if(m<=n)
{
int res=0;
for(int x=0;x<43;x++)
{
int t=(i+x*j)%k+1;
for(int l=1;l<=k;l++)
{
res++;
if(l!=t)printf("0");
else printf("1");
if(res==n)
{
printf("\n");
break;
}
}
}
}
else break;
}
return 0;
}