观察可以发现,最终答案中连续的相同颜色的列的数量一定大于等于 $2$。按照行染色同理。设颜色 $a_i$ 能够染色的列数是 $b_i = \lfloor \frac{a_i}{m} \rfloor$,需要构造 $b_1 + b_2 + \ldots + b_j = n, b_i \ge 2$。
考虑分解的思想,如果 $n$ 是偶数,设 $b_1 + b_2 + \ldots + b_k = sum$,如果有解,那么要求 $sum \ge n$。如果 $sum$ 是偶数,这意味着,奇数 $b_j$ 的个数为偶数个,如果 $sum > n$,那么把偶数 $b_j$ 拆分成 $2$,奇数 $b_j$ 拆分成若干个 $2$ 和一个 $1$,最后 $1$ 的个数是偶数个,先去掉偶数个 $1$,然后再考虑去掉 $2$,直到 $sum = n$。
用类似的方法可以解决 $n$ 和 $sum$ 的奇偶性的其它三种情况,最终得到答案。这种奇偶性转换和分解的构造思想很常见。
// 2024/1/30
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7, MOD1 = 998'244'353;
const ll INFL = 0x3f3f3f3f'3f3f3f3f;
const double eps = 1e-8;
const int dir[8][2] = {
{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1},
};
const ull Pr = 131;
#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define For1(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i)
#define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i])
#define f1 first
#define f2 second
#define pb push_back
#define has(a, x) (a.find(x) != a.end())
#define nemp(a) (!a.empty())
#define all(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define NL cout << '\n';
template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template <typename t> istream &operator>>(istream &in, vector<t> &vec) {
for (t &x : vec)
in >> x;
return in;
}
template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) {
int n = SZ(vec);
For(i, 0, n) {
out << vec[i];
if (i < n - 1)
out << ' ';
}
return out;
}
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ", ";
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? ", " : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifdef _DEBUG
#define debug1(x) cout << #x " = " << x << endl;
#define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl;
#define debug3(x, y, z) \
cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl;
#define dbg(x...) \
cerr << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \
_print(x); \
cerr << endl;
#else
#define debug1
#define debug2
#define debug3
#define dbg(x...)
#endif
const int N = 100100;
ll n, m, a[N], k, b[N];
void solve() {
cin >> n >> m >> k;
For1(i, 1, k) { cin >> a[i]; }
sort(a + 1, a + 1 + k);
ll tot = n * m;
if (a[k] >= tot) {
cout << "Yes\n";
return;
}
auto check = [&](ll n, ll m) -> bool {
ll cnt = 0;
bool found = false;
For1(i, 1, k) {
b[i] = a[i] / n;
if (b[i] >= 2)
cnt += b[i];
if (b[i] >= 3)
found = true;
}
if (cnt < m)
return false;
if (m % 2 == 0)
return true;
return found;
};
if (check(n, m) || check(m, n)) {
cout << "Yes\n";
} else
cout << "No\n";
}
int main(void) {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}