Description:
John Doe started thinking about graphs. After some thought he decided that he wants to paint an undirected graph, containing exactly k cycles of length 3.
A cycle of length 3 is an unordered group of three distinct graph vertices a, band 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
1Output
3 011 101 110Input
10Output
5 01111 10111 11011 11101 11110
这道题题意比较难理解,就是1代表有边,0代表没有边,三条边组成一个三元环,问最少需要多大的矩阵可以组成N个三元环。
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<vector>
#include<math.h>
const int INF = 0x3f3f3f3f;
using namespace std;
typedef long long ll;
typedef double ld;
int i,j,k,l;
const int maxn = 1e3+10;
const int MOD = 1e9+7;
int a[105][105];
int n;
int main()
{
scanf("%d",&n);
a[1][2]=a[2][1]=1;
for(i=3;i<=100;i++)
{
for(j=1;j<i;j++)
{
int cnt=0;
for(k=1; k<j; k++)
{//cnt统计与当前边同时相连的两边对数,即三元环数目
if(a[k][j]&&a[k][i])
cnt++;
}
if(cnt<=n)
{//若添加此边后,增加的三元环不超过要求,更新
n-=cnt;
a[i][j]=a[j][i]=1;
}
if(!n)
break;
}
if(!n)
break;
}
printf("%d\n",i);
for(j=1;j<=i;j++)
{
for(k=1;k<=i;k++)
printf("%d", a[j][k]);
printf("\n");
}
return 0;
}
大佬的代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<vector>
#include<math.h>
const int INF = 0x3f3f3f3f;
using namespace std;
typedef long long ll;
typedef double ld;
int i,j,k,l;
const int maxn=100;
int mp[maxn][maxn];
int main()
{
scanf("%d",&k);
for(i=0; i<maxn; i++)
{
int cnt=0;
for(int j=0; j<i&&cnt<=k; j++)
{
k-=s;
cnt++;
mp[i][j]=mp[j][i]=1;
}
}
printf("%d\n",maxn);
for(i=0; i<maxn; i++)
{
for(j=0; j<maxn; j++)
{
printf("%d",mp[i][j]);
}
printf("\n");
}
return 0;
}