【Codeforces Round #447 (Div. 2) B】Ralph And His Magic Field

| 【链接】 我是链接,点我呀:)
【题意】

给你一个n*m矩阵,让你在里面填数字. 使得每一行的数字的乘积都为k; 且每一列的数字的乘积都为k; k只能为1或-1

【题解】

显然每个位置只能填1或-1 如果只考虑前n-1行和前m-1列。 那么我们对这(n-1)*(m-1)的范围。 先任意填入数字; 则一共有$2^{(n-1)*(m-1)}$种方法。 然后把最后一行的前m-1列填一下。 使得前m-1列满足,每一列的乘积为k 然后把最后一列的前n-1行填一下使前n-1行每一行的乘积都为k 最后填最右下角那个格子。 会发现,行数和列数如果奇偶不同的话,且k=-1的话,那个格子是没办法满足每行每列的要求的 除了这种情况之外,都能满足要求,即填一个唯一的数字,使得最后一行,最后一列的乘积都为k (根据整个矩形的-1的个数的奇偶性来判断)

【代码】

/*
  	1.Shoud it use long long ?
  	2.Have you ever test several sample(at least therr) yourself?
  	3.Can you promise that the solution is right? At least,the main ideal
  	4.use the puts("") or putchar() or printf and such things?
  	5.init the used array or any value?
  	6.use error MAX_VALUE?
  	7.use scanf instead of cin/cout?
*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long

LL n,m;
int k;

const LL MOD = 1e9 + 7; // ģ��
LL Pow(LL x,LL y){ //��x^y
    LL a = 1;x%=MOD;
    while (y){
        if (y&1) a = (a*x)%MOD;
        x=(x*x)%MOD;
        y>>=1;
    }
    return a;
}

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("F:\c++source\rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
	cin >> n >> m >> k;
	if (k==-1 && ((n&1)!=(m&1))){
	    cout << 0 << endl;
	}else{
	    cout << Pow(Pow(2,n-1),m-1) << endl;
	}	
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7889621.html