这个地方的地图可以被看作一个n维超立方体,有着恰好
2
n
2^n
2n个点。每个顶点编号从0到
2
n
−
1
2^n-1
2n−1。当且仅当两个顶点在二进制表示下仅有一位不同时这两个顶点相邻。
在战争中,每个点都被敌军或者将军占领。你注意到对于每一个顶点,和这个顶点相邻的点中同一颜色的点的个数不超过
⌈
n
⌉
\lceil \sqrt{n} \rceil
⌈n⌉。你可以构建一个超立方体满足以上要求吗?
输入这个n,请输出一个长为
2
n
2^n
2n的01串。
题意 :
- 定义n维超立方体有 2 n 2^n 2n个点,编号 [ 0 , 2 n − 1 ] [0, 2^n - 1] [0,2n−1],当且仅当点i和点j在二进制表示下仅有一位不同时点i和点j相邻。
- 给n,求构造长为 2 n 2^n 2n的01序列表示 2 n 2^n 2n个点,0表示敌军,1表示将军,满足每个点相邻的点中和这个点颜色相同的点的个数不超过 ⌈ n ⌉ \lceil \sqrt{n} \rceil ⌈n⌉个。
思路 :
- 但实际上就是一个二分图(因为只有01两种元素,且0元素之间没有边,1元素之间没有边,0和1元素之间有边),当二进制中1的个数为奇数和偶数的分别染不同颜色即可。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <map>
#define endl '\n'
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const double pi = acos(-1);
typedef long long ll;
typedef pair<int, int> PII;
// int calc(ll x)就可以了,因为int的第32位是符号位,所以x = 3时,(x >> 32 & 1)在这个循环里的结果会是1,会进入if语句块;(3 >> 32 & 1)则会是0,因为3在这里是unsigned int,不是int;而long long有64位,第64位是符号位
//int calc(int x)
//{
// int cnt = 0;
// for (int i = 0; i <= 32; i ++ )
// if (x >> i & 1) cnt ++ ;
// return cnt;
//}
int calc(int x)
{
int cnt = 0;
while (x)
{
if (x & 1) cnt ++ ;
x >>= 1;
}
return cnt;
}
int main()
{
IOS;
int n;
cin >> n;
for (int i = 0; i < 1 << n; i ++ )
{
if (calc(i) & 1) cout << 1;
else cout << 0;
}
cout << endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <ctime>
#define endl '\n'
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define lowbit(x) (x&-x)
using namespace std;
const double pi = acos(-1);
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
const int N = 1e6 + 10;
int main()
{
IOS;
int n;
cin >> n;
for (int i = 0; i < 1 << n; i ++ )
cout << (__builtin_popcount(i) & 1); // 函数返回输入数据二进制中1的个数 // 头文件<ctime>
cout << endl;
return 0;
}