题意
给定一个数M,让你输出一个以邻接矩阵形式的图 ,使得图里的三元环的个数等于M.
分析
首先我们知道,完全图任意两个点都能互相到达,那么图里的三元环的个数就是 那么我们先预处理出组合数,然后先找到最大的 减去个数。
接下来我们就开始一个个加点,对于任意一个加的点,如果这个点与j个完全图里的点相连,那么这个加的点就会产生 的贡献,那么我们就可以不断最大的符合条件的j,并减去。
复杂度
由于第二列的组合数在100左右时上下差值还是100,也就是说我们找的次数不会超过10次,复杂度是常数级别。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #include<bits/stdc++.h> #define min(a,b) (a<b?a:b) #define max(a,b) (a>b?a:b) #define int long long using namespace std; const int L=1e6+233; char *p1,*p2,ppp[L]; inline char gc() { if(p1==p2) { p2=(p1=ppp)+fread(ppp,1,L,stdin); } return *p1++; } inline void read(int &x) { x=0;int pd=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')pd=-pd;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} x*=pd; } inline void write(const int &x) { int s=0;int tmp=x;if(tmp==0){putchar('0');return;} if(tmp<0)tmp=-tmp,putchar('-'); char ggg[20];while(tmp>0){ggg[s++]=tmp%10+'0',tmp/=10;} while(s>0)putchar(ggg[--s]); }
int c[1010][1010]; int now,n;
int cnt,mp[110][110];
signed main() { for(register int i=0;i<=100;++i) { c[i][0]=1; for(register int j=1;j<=3;++j) { c[i][j]=c[i-1][j]+c[i-1][j-1]; } } read(n); for(register int i=1;i<=100;++i) { if(c[i][3]>n) { now=i-1; break; } } n-=c[now][3]; cnt=now+1; for(register int i=1;i<=now;++i) { for(register int j=1;j<=now;++j) { if(i!=j) { mp[i][j]=1; } } } if(!n) { cout<<now<<endl; for(register int i=1;i<=now;++i) { for(register int j=1;j<=now;++j) { write(mp[i][j]); } puts(""); } return 0; } while(1) { for(register int i=1;i<=100;++i) { if(c[i][2]>n) { now=i-1; break; } } n-=c[now][2]; for(register int i=1;i<=now;++i) { mp[cnt][i]=mp[i][cnt]=1; } if(!n) { break; } else { ++cnt; } } cout<<cnt<<endl; for(register int i=1;i<=cnt;++i) { for(register int j=1;j<=cnt;++j) { write(mp[i][j]); } puts(""); } return 0; }
|