【CodeForces】233C - Cycles(贪心)

来源:互联网 发布:网络音乐在线听歌曲 编辑:程序博客网 时间:2024/06/16 16:03

题目链接:点击打开链接

C. Cycles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

John Doe started thinking about graphs. After some thought he decided that he wants to paint an undirected graph, containing exactly kcycles of length 3.

A cycle of length 3 is an unordered group of three distinct graph vertices ab and c, 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 exceed 100, or else John will have problems painting it.

Input

A single line contains an integer k (1 ≤ k ≤ 105) — the number of cycles of length 3 in the required graph.

Output

In the first line print integer n (3 ≤ n ≤ 100) — the number of vertices in the found graph. In each of next n lines print n characters "0" and "1": the i-th character of the j-th line should equal "0", if vertices i and j do not have an edge between them, otherwise it should equal "1". Note that as the required graph is undirected, the i-th character of the j-th line must equal the j-th character of the i-th line. The graph shouldn't contain self-loops, so the i-th character of the i-th line must equal "0" for all i.

Examples
input
1
output
3011101110
input
10
output
50111110111110111110111110




每添加一条线,贡献多少个环要算好,然后就不难了。

代码如下:
#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 longint 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;}


0 0
原创粉丝点击