zoukankan      html  css  js  c++  java
  • Codeforces 894 B Ralph And His Magic Field

    Discription

    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 modulo1000000007 = 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 nm and k (1 ≤ n, m ≤ 1018k is either 1 or-1).

    Output

    Print a single number denoting the answer modulo 1000000007.

    Example

    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.

    /*
      1:当k==1的情况:
        这样需要保证每一行和每一列的乘积都是1,也就是
        每一行和每一列的-1的个数都是偶数。
        
        我们可以填前n-1行的时候只需要保证行的限制,先
        不管列的限制。
        这样的方案数是 2^((n-1)*(m-1))  (杨辉三角同一行
        的奇数项和=偶数项和)
        然后最后一行再填坑,对于每一列,如果前n-1行的乘积
        是-1,那么填-1;否则填1。
        发现这样的方案是唯一的,而且是一定可行的。
        证明可行的话,因为前n-1行的乘积是1,而这一行填完之后
        每一列的乘积也是1,所以除一下之后,这一行的乘积也是1了。
        
      2:当k==-1的情况:
        当n,m奇偶性不同的时候无解,因为如果行和列其中一个是奇数的
        时候,可以推出所有元素乘积是-1;而偶数则可以推出所有元素乘积是1,
        矛盾。
        
        当n,m奇偶性相同的时候,答案和1一样,证明略。
        (也是前n-1行只需满足单行限制的填数,最后一行填坑,
        仍可以证明方案唯一且可行) 
    */
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #define ll long long
    #define ha 1000000007
    using namespace std;
    ll n,m;
    int k;
    
    inline ll ksm(ll x,ll y){
        ll an=1;
        for(;y;y>>=1,x=x*x%ha) if(y&1) an=an*x%ha;
        return an;
    } 
    
    int main(){
        scanf("%lld%lld%d",&n,&m,&k);
        if(k==-1&&((m^n)&1)) puts("0");
        else printf("%lld
    ",ksm(ksm(2,m-1),n-1));
        return 0;
    }
  • 相关阅读:
    54. 八皇后问题[eight queens puzzle]
    53. 特殊的O(n)时间排序[sort ages with hashtable]
    52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
    C++基础知识面试精选100题系列(11-20题)[C++ basics]
    C++基础知识面试精选100题系列(1-10题)[C++ basics]
    洛谷 P1479 宿舍里的故事之五子棋
    洛谷 P2084 进制转换
    codevs 1700 施工方案第二季
    POJ 3278 Catch That Cow(求助大佬)
    POJ 2251 Dungeon Master
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8341402.html
Copyright © 2011-2022 走看看