CodeForces - 894B. Ralph And His Magic Field

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 = 10^9 + 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 ≤ 10^18, 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.

Tips

题意:
一个 n × m n\times m n×m 的表格,现在向其中填数字(整数),保证每一行、每一列的积都是 k k k 。其中 k ∈ { 1 , − 1 } k\in\{1,-1\} k{1,1} 。问有多少填数字的方式。

解法:
由于 k ∈ { 1 , − 1 } k\in\{1,-1\} k{1,1} ,因此填进去的数字只能是 1 1 1 − 1 -1 1 。考虑每一行的数字,若 k = 1 k=1 k=1 则填入 − 1 -1 1 的个数需为偶数个,反之则为奇数个。具体说来,若 k = − 1 k=-1 k=1 ,每一行的填法共有 ∑ i = 0 ⌊ m − 1 2 ⌋ C m 2 i + 1 = 2 m 2 = 2 m − 1 \sum_{i=0}^{\lfloor \frac{m-1}{2}\rfloor}C_{m}^{2i+1}=\frac{2^m}{2}=2^{m-1} i=02m1Cm2i+1=22m=2m1 个。则 k = 1 k=1 k=1 的填法也是 2 m − 1 2^{m-1} 2m1 个。

由于有 n n n 行,当我们确定了前 n − 1 n-1 n1 行时,最后一行的每一列的填法也就确定了。因此共有 ( 2 m − 1 ) n − 1 (2^{m-1})^{n-1} (2m1)n1 种填法。

当然,此处欠考虑,如果最后一行无法满足这一行的积为 k k k 怎么办呢?
我们假设第 i i i 列的前 n − 1 n-1 n1 行之积为 a i a_i ai ,最后一行的每一列为 b i b_i bi ,则满足 a i b i = k a_ib_i=k aibi=k ∏ i = 1 m a i = k n − 1 \prod_{i=1}^{m} a_i=k^{n-1} i=1mai=kn1 ∏ i = 1 m b i = k m ∏ a i = k m − n + 1 \prod_{i=1}^{m} b_i=\frac{k^{m}}{\prod a_i}=k^{m-n+1} i=1mbi=aikm=kmn+1

因此,当 k = 1 k=1 k=1 时,最后一行一定满足条件。
k = − 1 k=-1 k=1 时,需要 m − n m-n mn 为偶数,否则不存在任何填法满足题意。

Reference Code

#include 
const int MOD=1e9+7;
typedef long long ll;
int qp(ll n,ll k){
    ll res=1;
    while (k){
        if (k&1) res=res*n%MOD;
        n=n*n%MOD;
        k>>=1;
    }
    return (int)res;
}
ll n,m;
int k;
int solve(){
    if (k==-1&&((n-m)&1))
        return 0;
    int y=qp(2,m-1);
    return qp(y,n-1);
}
int main(){
//    freopen("in.txt","r",stdin);
    scanf("%lld%lld%d",&n,&m,&k);
    printf("%d\n",solve());
    return 0;
}

你可能感兴趣的:(ACM练习,ACM,C/C++)