您的位置:首页 > 其它

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

2017-11-20 19:37 507 查看
B. Ralph And His Magic Field

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Ralph has a magic field which is divided into n × m blocks. That is to say, there are n rows
and m columns on the field. Ralph can put an integer in each block. However, the magic field doesn't always work properly. It works
only if the product of integers in each row and each column equals to k, where k is
either 1 or -1.

Now Ralph wants you to figure out the number of ways to put numbers in each block in such a way that the magic field works properly. Two ways are considered different if and only if there exists at least one block where the numbers in the first way and in the
second way are different. You are asked to output the answer modulo 1000000007 = 109 + 7.

Note that there is no range of the numbers to put in the blocks, but we can prove that the answer is not infinity.

Input

The only line contains three integers n, m and k (1 ≤ n, m ≤ 1018, k is
either 1 or -1).

Output

Print a single number denoting the answer modulo 1000000007.

Examples

input
1 1 -1


output
1


input
1 3 1


output
1


input
3 3 -1


output
16


Note

In the first example the only way is to put -1 into the only block.

In the second example the only way is to put 1 into every block.

题意:n×m的矩阵,每个格子里面填任意数,使得每行乘积都为k,每列乘积都为k,k取1或-1。问一共有多少种

填充方案

分析:先用一个3×3矩阵分析

每个格子数值用字母表示

abc

def

ghi

如果k取1,那么在成立的填充方法里面,每行乘起来,再乘以每列,结果为1a^2×b^2×c^2×...×i^2 = 11.每个值都是整数不是小数,只有每个平方取值都是1,乘积才是1。

所以每个值只能取1或-1才满足要求,根本不是题意说的取任意数。

2.其次,每个数都是平方,所以结果是大于等于0的,而取值又没有0,所以结果肯定是1只有偶数个-1乘积才是1,所以行数+列数=偶数,如果等于奇数,是不可能有填充方案的,

只有奇数+偶数才等于奇数,所以,行列如果是一奇一偶,填充方案数为0。

n,m范围超大,填充肯定是有规律的。由于乘积为1还是-1只由-1个数的奇偶性决定,所以先填充除最后一行,

最后一列的中间矩阵区域,最后一行,跟最后一列填充数值修正乘积最终值,每个格子有2种填法。

所以有2^((m-1)*(n-1))种方案。

如何保证最后一列跟最后一行的乘积同时满足都是1或都是-1呢?

以k=-1为例,再看3×3矩阵

ab c

de f

gh i

填充c,f后 

abc

def

乘起来为1,也就是一共有偶数个-1,

填充 g,h后

ab

de

gh

乘起来也是1,也是偶数个-1,

当这两块同时减去abde后,

如果减去奇数个-1,那么剩下的一行一列也都包含奇数个-1;

如果减去偶数个-1,剩下的一行一列也都包含偶数个-1。

所以行列里面-1的奇偶性是一致的,不论希望乘积是1还是-1,都可以通过选择f的值修正。

代码就简单了,快速幂:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define INF 0x3f3f3f3f
#define FI first
#define SE second
int mod = 1000000007;
const int N = 10010;
ll Pow(ll a, ll x)
{
ll ret = 1;
while (x)
{
if (x&1)
ret = (ret*a)%mod;
a = (a*a)%mod;
x >>= 1;
}
return ret;
}
int main()
{
ll n, m;
int k;
cin >> n >> m >> k;
if (k == -1 && ((n^m)&1))
puts("0");
else
cout<<Pow(Pow(2, n-1), m-1)<<endl;

return 0;
}


               
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: