John Doe started thinking about graphs. After some thought he decided that he wants to paint an undirected graph, containing exactlyk cycles of length 3.
A cycle of length 3 is an unordered group of three distinct graph verticesa, b andc, such that each pair of them is connected by a graph edge.
John has been painting for long, but he has not been a success. Help him find such graph. Note that the number of vertices there shouldn't exceed100, or else John will have problems painting it.
A single line contains an integer k (1 ≤ k ≤ 105) — the number of cycles of length3 in the required graph.
In the first line print integer n (3 ≤ n ≤ 100) — the number of vertices in the found graph. In each of nextn lines print n characters "0" and "1": thei-th character of the j-th line should equal "0", if verticesi and j do not have an edge between them, otherwise it should equal "1". Note that as the required graph is undirected, thei-th character of the j-th line must equal the j-th character of thei-th line. The graph shouldn't contain self-loops, so thei-th character of the i-th line must equal "0" for alli.
1
3 011 101 110
10
5 01111 10111 11011 11101 11110一幅图中的点,问能构成n个三元环的点的关系
依次模拟加入边的关系(即是否两点相连),判断加入此关系之后所增加的三元环数目是否合适,再决定是否加入
pos[i][j] = 1,代表i,j两点相连,即有边
判断方法是查找当前存在多少对与当前两顶点相连的边
如:要判断4与5是否相连,需统计1,4和1,5是否同时相连,若相连,连接4,5后,三元环数目+1,
再统计2,4和2,5……
……
类推即可
显然如果有n对与当前两顶点(即当前边)同时相连的两边对数,则当前两顶点相连可以多形成n个三元环
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<functional>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CL(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int maxn = 1e3+10;
const int MOD = 1e9+7;
int pos[105][105];
int main(){
int n, t, i, kcase = 1;
scanf("%d", &n);
pos[1][2] = pos[2][1] = 1;
for(i = 3; i <= 100; i++){
for(int j = 1; j < i; j++){
int c = 0;
for(int k = 1; k < j; k++){//c统计与当前边同时相连的两边对数,即三元环数目
if(pos[k][j] && pos[k][i]) c++;
}
if(c <= n){//若添加此边后,增加的三元环不超过要求,更新
n -= c;
pos[i][j] = pos[j][i] = 1;
}
if(!n) break;
}
if(!n) break;
}
printf("%d\n", i);
for(int m = 1; m <= i; m++){
for(int n = 1; n <= i; n++) printf("%d", pos[m][n]);
printf("\n");
}
return 0;
}