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\n",ksm(ksm(2,m-1),n-1));
    return 0;
}

原文地址:https://www.cnblogs.com/JYYHH/p/8341402.html

时间: 2024-10-24 07:19:25

Codeforces 894 B Ralph And His Magic Field的相关文章

codeforces 894B - Ralph And His Magic Field - [数学题]

题目链接:https://cn.vjudge.net/problem/CodeForces-894B 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 alway

【Codeforces】894E.Ralph and Mushrooms Tarjan缩点+DP

题意 给定$n$个点$m$条边有向图及边权$w$,第$i$次经过一条边边权为$w-1-2.-..-i$,$w\ge 0$给定起点$s$问从起点出发最多能够得到权和,某条边可重复经过 有向图能够重复经过的边当且仅当成环,所以tarjan缩点成DAG,缩点后每个点内的权值可以通过二分算出,假设最大的$n$使得$w-\frac{n(n+1)}{2}\ge 0$,那么该点值为$(n+1)w-\frac{n(n+1)(n+2)}{6}$,通过对DAG进行dp算出最长路就是答案 代码 #include <b

【Codeforces Global Round 1 E】Magic Stones

[链接] 我是链接,点我呀:) [题意] 你可以把c[i]改成c[i+1]+c[i-1]-c[i] (2<=i<=n-1) 问你能不能把每一个c[i]都换成对应的t[i]; [题解] d[i] = c[i+1]-c[i]; (1<=i<=n-1) change c[i] c[i]' = c[i+1]+c[i-1]-c[i]; d[i-1] = c[i]'-c[i-1]; = c[i+1]+c[i-1]-c[i]-c[i-1] == c[i+1]-c[i] = d[i]; d[i]

Codeforces Round #447 (Div. 2) 题解 【ABCDE】

BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imagine "Q" as eyes with tears and "A" as a mouth. Now Diamond has given Bort a string consisting of only uppercase English letters of leng

Codeforces Round #447

QAQ Ralph And His Magic Field Marco and GCD Sequence Ralph And His Tour in Binary Country Ralph and Mushrooms

Mach-O文件格式和程序从载入到运行过程

> 之前深入了解过.过去了一年多的时间.如今花些时间好好总结下,毕竟好记性不如烂笔头. 其次另一个目的,对于mach-o文件结构.关于动态载入信息那个数据区中,命令含义没有深刻掰扯清除,希望有同学能够指点下. 摘要:对于mach-o是Mac和iOS能够运行文件的格式.进程就是系统依据该格式将运行文件载入到内存后得到的结果.系统通过解析文件,建立依赖(动态库),初始化运行时环境,才干真正開始运行该App(进程) Start from Hello World 通过分析以下这个最熟悉的可运行文件.来好

Perl 内部结构详解

PerlGuts Illustrated Version 0.49, for perl 5.20 and older This document is meant to supplement the perlguts(1) manual page that comes with Perl. It contains commented illustrations of all major internal Perl data structures. Having this document han

Video for Linux Two API Specification Revision 2.6.32【转】

转自:https://www.linuxtv.org/downloads/legacy/video4linux/API/V4L2_API/spec-single/v4l2.html Video for Linux Two API Specification Revision 2.6.32 Michael H Schimek <[email protected]> Bill Dirks Original author of the V4L2 API and documentation. Hans

codeforces 492E. Vanya and Field(exgcd求逆元)

题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走了n步后,x从0~n-1,y从0~n-1都访问过,但x,y不相同. 所以,x肯定要经过0点,所以我只需要求y点就可以了. i,j为每颗苹果树的位置,设在经过了a步后,i到达了0,j到达了M. 则有 1----------------------(i + b * dx) % n = 0 2------