http://codeforces.com/contest/233/problem/C
題意:求一個具有k個三元環的無向圖。
分析:先一個i個節點的無向完全圖,其中C(i,3)<=k,剩下k-C(i,3)個三元環未構成,再加j條邊(C(j,2)<=未構成的環)直到滿足條件。
View Code
1 /* 2 Author:Zhaofa Fang 3 Lang:C++ 4 */ 5 #include <cstdio> 6 #include <cstdlib> 7 #include <iostream> 8 #include <cmath> 9 #include <cstring>10 #include <algorithm>11 #include <string>12 #include <utility>13 #include <vector>14 #include <queue>15 #include <stack>16 #include <map>17 #include <set>18 using namespace std;19 typedef long long ll;20 #define pii pair<int,int>21 #define pb push_back22 #define mp make_pair23 #define fi first24 #define se second25 #define lowbit(x) (x&(-x))26 #define INF (1<<30)27 28 int maz[105][105];29 int main()30 {31 #ifndef ONLINE_JUDGE32 freopen("in","r",stdin);33 #endif34 int K;35 while(cin>>K)36 {37 memset(maz,0,sizeof(maz));38 int i;39 for(i=3;i*(i-1)*(i-2)/6<=K;i++);40 i--;41 K -= i*(i-1)*(i-2)/6;42 for(int j=0;j<i;j++)43 for(int k=0;k<i;k++)44 maz[j][k] = (j != k);45 int n=i;46 while(K)47 {48 int j;49 for(j=2;j*(j-1)/2<=K;j++);50 j--;51 for(int k=0;k<j;k++)maz[n][k] = maz[k][n] = 1;52 K -= (j-1)*j/2;53 n++;54 }55 cout<<n<<endl;56 for(int x=0;x<n;x++)57 {58 for(int y=0;y<n;y++)59 printf("%d",maz[x][y]);60 puts("");61 }62 }63 return 0;64 }