本题的思路在于将组成多边形的每一个最小的三角形都“浓缩”为一个点。然后将正放的小三角形所对应的坐标的y值设为偶数,倒放的为基数,再用手头已有的三角形去覆盖,按行覆盖即可
// C++Exercise.cpp : 定义控制台应用程序的入口点。
//
#include<iostream>
#include<string>
#include<cstring>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<sstream>
using namespace std;
int t, s, n;
bool area[110][110];
bool feasible(int x, int y, int length){
if (x + length - 1 > 2 * s || y + 2 * length - 2 > 4 * s) return false;
if (y % 2 == 1){//上三角
for (int i = 0; i < length; i++){
for (int j = 0; j < 2 * i + 1; j++){
if (area[x + i][y + j] == false) return false;
}
}
}
else{//下三角
for (int i = 0; i < length; i++){
for (int j = 2 * i; j < 2 * length - 1; j++){
if (area[x + i][y + j] == false) return false;
}
}
}
return true;
}
void change(int x, int y, int length){
if (y % 2 == 1){//上三角
for (int i = 0; i < length; i++){
for (int j = 0; j < 2 * i + 1; j++){
area[x + i][y + j] = false;
}
}
}
else{//下三角
for (int i = 0; i < length; i++){
for (int j = 2 * i; j < 2 * length - 1; j++){
area[x + i][y + j] = false;
}
}
}
}
void restore(int x, int y, int length){
if (y % 2 == 1){//上三角
for (int i = 0; i < length; i++){
for (int j = 0; j < 2 * i + 1; j++){
area[x + i][y + j] = true;
}
}
}
else{//下三角
for (int i = 0; i < length; i++){
for (int j = 2 * i; j < 2 * length - 1; j++){
area[x + i][y + j] = true;
}
}
}
}
bool dfs(int x, int y, vector<int>& small){
if (x > 2 * s) return true;
if (y > 4 * s) return dfs(x + 1, 1, small);
if (area[x][y] == false){
int j;
for (j = y + 1; j <= 4 * s; j++){
if (area[x][j] == true) break;
}
return dfs(x, j, small);
}
for (int i = 0; i < n; i++){
if (feasible(x, y, small[i])){
change(x, y, small[i]);
if (dfs(x, y + 1, small)) return true;
restore(x, y, small[i]);
}
else break;
}
return false;
}
int main(){
cin >> t;
while (t){
cin >> s >> n;
vector<int> small;
for (int i = 0; i < n; i++){
int t1;
cin >> t1;
small.push_back(t1);
}
bool success = false;;
sort(small.begin(), small.end());
for (int i = 0; i < n; i++){
if (small[i] > s){
n = i;
break;
}
}
for (int i = 0; i < n; i++){
if (s%small[i] == 0){
success = true;
break;
}
else{
for (int j = 0; j < i; j++){
if (small[i] % small[j] == 0){
for (int k = i + 1; k < n; k++) small[k - 1] = small[k];
n--;
i--;
break;
}
}
}
}
if (success){
cout << "YES" << endl;
t--;
continue;
}
memset(area, false, sizeof(area));
for (int i = 1; i <= s; i++){//上三角开始
for (int j = 1; j <= 2 * s + 2 * i - 1; j++){
area[i][j] = true;
}
}
for (int i = s + 1; i <= 2 * s; i++){//下三角开始
for (int j = 2 * i - 2 * s; j <= 4 * s; j++){
area[i][j] = true;
}
}
if (dfs(1, 1, small)) cout << "YES" << endl;
else cout << "NO" << endl;
t--;
}
return 0;
}