挺有意思的一个题。
思路
在下文中,记 。
因为颜色只有 ,那么 等于相邻两个位置 满足 的数量。
我们需要满足 ,即满足 。
考虑一个点 的 会做贡献的次数。注意到若 并且 ,贡献的次数为 ;其余位置的点贡献次数均为偶数。这意味着只有前者的颜色会对最后 的值有影响,其余位置的点都可以任选颜色。
记被钦定颜色的满足 并且 的点的数量为 ,它们的颜色和为 ,则:
- 若 :
- 若 ,答案为 (除了这 个点的颜色都可以任选)。
- 否则,答案为 。
- 否则,答案为 (当边缘的点只剩下一个没有被填颜色,那么这个点的颜色已经被确定,因为需要满足边缘点的颜色和为偶数)。
Code
#include <bits/stdc++.h>
#define re register
#define int long long
#define Mul(a,b) ((a) * (b) % mod)
#define chMul(a,b) (a = Mul(a,b))
using namespace std;
const int mod = 1e9 + 7;
int n,m,k;
inline int read(){
int r = 0,w = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
r = (r << 3) + (r << 1) + (c ^ 48);
c = getchar();
}
return r * w;
}
inline int qmi(int a,int b){
int res = 1;
while (b){
if (b & 1) chMul(res,a);
chMul(a,a); b >>= 1;
} return res;
}
inline void solve(){
int cnt = 0,sum = 0;
n = read(),m = read(),k = read();
for (re int i = 1,x,y,c;i <= k;i++){
x = read(),y = read(),c = read();
if ((x == 1) + (x == n) + (y == 1) + (y == m) == 1) cnt++,sum += c;
}
if (cnt == 2 * (n + m - 4)){
if (sum & 1) puts("0");
else printf("%lld\n",qmi(2,n * m - k));
}
else printf("%lld\n",qmi(2,n * m - k - 1));
}
signed main(){
int T; T = read();
while (T--) solve();
return 0;
}