codeforces 894B Ralph And His Magic Field

http://www.elijahqi.win/archives/1606
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.

这题妙啊

题意:要求往m*n的格里填数 他会给你一个k 你填的数使得 每一行每一列的乘积都是k

k要么是1 要么是-1 那么我们就可以知道 往里面填的数一定是一或者-1

那么如何来确定 首先我们可以考虑 当m和n的奇偶性不同的时候 且k为-1时 无论如何也无法满足

因为我们假如把边数乘列数乘起来 那么最后结果一定是-1 但是 我们这样的话 其实一共是偶数个点 偶数个点我相当于着偶数个点相乘的平方最后答案应该是-1 显然是不存在的 所以输出0

其他情况 我们考虑只针对(m-1)行(n-1)列来看 是不是这m-1行n-1列无论填入什么我都是可以在最后一行和最后一列来构造满足条件的 且这样不会冲突 那么我不妨枚举m-1行n-1列的填数情况就是我最后的答案了

即剩余情况答案:2^((n-1)*(m-1)) 用快速幂计算答案即可

#include
#define mod 1000000007
long long n,m;int op;
int main(){
//  freopen("cf.in","r",stdin);
    scanf("%lld%lld%d",&n,&m,&op);
    if ((n%2)!=(m%2)&&op==-1) {printf("0");return 0;}long long ans=1,base=2;
    for (long long k=m-1;k;k>>=1,(base*=base)%=mod){
        if (k&1) (ans*=base)%=mod;
    }
    base=ans; ans=1;
    for (long long k=n-1;k;k>>=1,(base*=base)%=mod){
        if (k&1) (ans*=base)%=mod;
    }
    printf("%lld",ans);
    return 0;
}

你可能感兴趣的:(数学,快速幂)