AcWing 470. 图书管理员
原题链接
简单
作者:
王小强
,
2021-02-11 12:36:53
,
所有人可见
,
阅读 649
简单题也要多思考
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, q, books[N];
int len, req; // req == requirements == 需求码
int find(int len, int req) {
// suffix == prefix 的反义词 (后缀)
// int suffix = pow(10, len); // 库函数的思维
int suffix = 1;
for (int k = 0; k < len; ++k) suffix *= 0b1010; // 数学的思维 (10的二进制就是1010)
for (int i = 1; i <= n; ++i)
if (books[i] % suffix == req) return books[i];
return -1;
}
int main(void) {
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> books[i];
sort(books + 1, books + 1 + n); // inclusive 的反义词 == exclusive
while (q--) {
scanf("%d %d", &len, &req);
printf("%d\n", find(len, req));
}
return 0;
}