题目链接:点击打开链接
每添加一条线,贡献多少个环要算好,然后就不难了。
代码如下:
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int mapp[111][111];
int main()
{
int n;
scanf ("%d",&n);
int l = 2;
int ant = 0;
mapp[1][2] = mapp[2][1] = 1;
for (int i = 3 ; i <= 100 ; i++)
{
l = max(l,i);
mapp[i][1] = mapp[1][i] = 1;
for (int j = 2 ; j < i ; j++)
{
mapp[i][j] = mapp[j][i] = 1;
ant += j-1;
if (ant == n)
break;
else if (ant > n)
{
ant -= j-1;
mapp[i][j] = mapp[j][i] = 0;
break;
}
}
if (ant == n)
break;
}
printf ("%d\n",l);
for (int i = 1 ; i <= l ; i++)
{
for (int j = 1 ; j < l ; j++)
printf ("%d",mapp[i][j]);
printf ("%d\n",mapp[i][l]);
}
return 0;
}