送你一个集合
题目背景:
分析:二进制
第一次做通信题。
最朴素的想法,直接把x传回去,这样显然不够优秀,考虑优化一下,显然x,y至少存在一位2进制位是不同的,那么我们只要传第一个不同的位置,然后再传x在当前位是0/1,这样可以在20以内解决,还是不够优秀,考虑如何优化掉返回0/1,我们发现,如果满足两个数中二进制位为1的个数是相同的,那么如果两个数不同,一定存在一位是x为1,y为0,那么我们发现C(12, 6) = 924 > 920(组合数)那么我们直接将1 ~ 920映射到所有有六个二进制位为1的数然后返回第一个x为1,y为0的位置就可以了。
Source:
/*
created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>
inline char read() {
static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}
///*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}
inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}
/*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
if (c == '-') iosig = true;
for (x = 0; isdigit(c); c = getchar())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
int type, n;
int cnt[1 << 12 | 1];
std::vector<int> code;
inline void init() {
for (int i = 0, s = (1 << 12) - 1; i < s; ++i) {
cnt[i] = cnt[i >> 1] + (i & 1);
if (cnt[i] == 6) code.push_back(i);
}
}
int encode(int x, int y) {
int pos = ((code[x - 1] ^ code[y - 1]) & code[x - 1]);
for (int i = 0; i < 12; ++i)
if (pos & (1 << i)) return i + 1;
}
bool decode(int q, int h) {
return (code[q - 1] >> h - 1) & 1;
}
int main() {
// freopen("xmasset.in", "r", stdin);
// freopen("xmasset.out", "w", stdout);
int t;
R(type), R(n), R(t);
init();
while (t--) {
int x, y;
R(x), R(y);
if (type == 1) W(encode(x, y)), write_char('\n');
else puts(decode(x, y) ? "yes" : "no");
}
flush();
return 0;
}