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×mn\times m 的表格,現在向其中填數字(整數),保證每一行、每一列的積都是 kk 。其中 k{1,1}k\in\{1,-1\} 。問有多少填數字的方式。

解法:
由於 k{1,1}k\in\{1,-1\} ,因此填進去的數字只能是 111-1 。考慮每一行的數字,若 k=1k=1 則填入 1-1 的個數需爲偶數個,反之則爲奇數個。具體說來,若 k=1k=-1 ,每一行的填法共有 i=0m12Cm2i+1=2m2=2m1\sum_{i=0}^{\lfloor \frac{m-1}{2}\rfloor}C_{m}^{2i+1}=\frac{2^m}{2}=2^{m-1} 個。則 k=1k=1 的填法也是 2m12^{m-1} 個。

由於有 nn 行,當我們確定了前 n1n-1 行時,最後一行的每一列的填法也就確定了。因此共有 (2m1)n1(2^{m-1})^{n-1} 種填法。

當然,此處欠考慮,如果最後一行無法滿足這一行的積爲 kk 怎麼辦呢?
我們假設第 ii 列的前 n1n-1 行之積爲 aia_i ,最後一行的每一列爲 bib_i ,則滿足 aibi=ka_ib_i=ki=1mai=kn1\prod_{i=1}^{m} a_i=k^{n-1}i=1mbi=kmai=kmn+1\prod_{i=1}^{m} b_i=\frac{k^{m}}{\prod a_i}=k^{m-n+1}

因此,當 k=1k=1 時,最後一行一定滿足條件。
k=1k=-1 時,需要 mnm-n 爲偶數,否則不存在任何填法滿足題意。

Reference Code

#include <cstdio>
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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章