1. 程式人生 > >HDU 6313: Hack it

HDU 6313: Hack it

time his eache length 。。 eve con ide nsis

Hack It

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 548 Accepted Submission(s): 170
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(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 \leq n \leq 2000$ and number of 1s not less than 85000.

Input Nothing.

Output The first line should be one positive integer n where $1 \leq n \leq 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 分析:昨天聽了dls的一頓操作後,頓時覺得有點東西。不過不知道為什麽會WA。。。 比如這個矩陣:

10000 10000 10000 10000 10000
10000 01000 00100 00010 00001
10000 00100 00001 01000 00010
10000 00010 01000 00001 00100
10000 00001 00010 00100 01000
11000 11000 11000 11000 11000
01000 00100 00010 00001 10000
01000 00010 10000 00100 00001
01000 00001 00100 10000 00010
01000 10000 00001 00010 00100
01100 01100 01100 01100 01100

其實就是某種意義上的+1,+2,。。。

技術分享圖片
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#define range(i,a,b) for(auto i=a;i<=b;++i)
#define LL long long
#define itrange(i,a,b) for(auto i=a;i!=b;++i)
#define rerange(i,a,b) for(auto i=a;i>=b;--i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
int grid[3005][3005],p=47;
void init(){
    range(i,0,p)range(j,0,p)range(k,0,p)grid[i*p+j][(j*k+i)%p+k*p]=1;
}
void solve(){
    puts("2000");
    range(i,0,1999) {
        range(j,0,1999){
            putchar(grid[i][j]+48);
            if(!((j+1)%5))putchar( );
        }
        putchar(\n);
    }
}
int main() {
    init();
    solve();
    return 0;
}
View Code

面向題解編程後,勉強理解了公式。。。這裏直接放標程了。。

技術分享圖片
#include <stdio.h>
int P=47,f[233333],an=0,gg[2333][2333];
int main()
{
    for(int i=0;i<P;i++)
    {
        for(int r=0;r<P;++r)
        {
            ++an;
            for(int j=i,k=0;k<P;k++,j=(j+r)%P)
                f[j*P+k]=an;
            for(int j=0;j<P*P;++j)
                if(f[j]==an) gg[i*P+r][j]=1;
        }
    }
    printf("%d\n",1999);
    for(int i=0;i<1999;++i,puts(""))
        for(int j=0;j<1999;++j)
            putchar(gg[i+2][j+2]+48);
}
View Code

HDU 6313: Hack it