codeforces 894 B题 Ralph And His Magic Field(巧思)

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 == 1 || k == -1) ; 则这些 n*m个空格上只能放 1 || -1 ,

      每一个空格都可以放 1 || -1 而且在每一行或者每一列都可以 通过放下最后一个来改变这一行或者一列的乘积从而达到题目的要求。

思路:每一行和每一列的最后一个空格留下来 改变这一行或者这一列的状态,其他所有的空格都是可以随意放的 所以就有 2^((n-1)*(m-1))的情况

  另外注意 当 k == -1 时 假设所有的空格都放置 -1 如果 n 和 m 都是奇数 或者偶数 可以 通过把 一部分 -1 换成 1 来满足题目的要求 ,

       但是 当 n m 一个奇数 一个 偶数的时候 , 使一列有 奇数个 -1 则必回 时一行有 偶数个 -1 则无法满足需求 !!!

#include 
#include 
#include 

using namespace std ; 

#define LL long long
#define mod 1000000007
LL n , m , k ; 
// 快速幂  注意到  n m 可以取到 10^18 所以 分两次 快速幂 
LL pow_mod(LL a , LL b ){
    LL result = 1 ;
    a = a%mod ; 
    
    while(b){
        if(b%2 ==1 ){
            result = result*a%mod ; 
        }
        a = a*a%mod ; 
        b>>=1 ; 
    }
    
    return result ; 
}

int main(){
        
    while(~scanf("%lld %lld %lld" ,&n , &m , &k )){
        //  n  m 一奇 一偶 而且 k==-1 , 
        //无法在达到行乘积为 -1 的同时满足列乘积为 -1  
        if((n%2 != m%2) && k == -1 ) {
            printf("0\n") ; 
        } else {
            printf("%lld\n" , pow_mod(pow_mod(2 , n-1), m-1 ) ) ;
        }
        
    }
    
    return 0 ; 
}

 

 

转载于:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7868954.html

你可能感兴趣的:(codeforces 894 B题 Ralph And His Magic Field(巧思))