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 nm 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或-1。现在拉尔夫想让你弄清楚在每个区块中放置数字的方法数量,以使魔法区域正常工作。(那么只能放1 or -1)两种方式被认为是不同的,当且仅当至少存在一个块,其中第一种方式和第二种方式中的数字是不同的。你被要求输出答案%1e9+7。

【分析】:当(n%2) != (m%2)并且k == -1是时输出0, 因为k==-1乘积次数为偶数时得到1,奇数时为-1.

else,试想一行的情况,不管前m-1怎么选择,最后一个都能通过选择(-1 or 1)得到k,同理列的情况也一样,所以最后(n-1)*(m-1)的方格都可以随意选择,然后通过剩下的一行和一列选择就可以得到k。因为只能选择(-1 or 1),答案是 2^((n-1)*(m-1))。

//我们可以将1或-1 放在它里面,总共有pow(2,[(n-1)*(m-1)])方式。 那么很明显,剩下的数字是唯一确定的,因为每行和每列的乘积已经是已知的。

因为n和m都可以到1e18,不能直接2^((n-1)*(m-1))进行快速幂操作。可以进行两次快速幂 或者 指数%(MOD-1)

【代码】:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const LL mod = 1e9+;
inline LL pows(LL x,LL n)//注意函数名不能为pow(不然一直wa7)因为pow为库函数名而且不是LL的
{
LL res=;
while(n)
{
if(n&)//
res = res * x % mod;
n>>=;
x = x * x % mod;
}
return res;
}
int main()
{
LL n,m,k; while(~scanf("%lld%lld%lld",&n,&m,&k))
{
if(n%!=m% && k==-)
{
printf("0\n");
return ;
}
else
printf("%lld\n",pows(pows(, n-),m-));
}
return ;
}

Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field【数论/组合数学】的更多相关文章

  1. Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field 数学

    题目链接 题意:给你三个数n,m,k;让你构造出一个nm的矩阵,矩阵元素只有两个值(1,-1),且满足每行每列的乘积为k,问你多少个矩阵. 解法:首先,如果n,m奇偶不同,且k=-1时,必然无解: 设 ...

  2. Codeforces Round #447 (Div. 2)E. Ralph and Mushrooms

    Ralph is going to collect mushrooms in the Mushroom Forest. There are m directed paths connecting n  ...

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

    BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imag ...

  4. Codeforces Round #447 (Div. 2)

    我感觉这场CF还是比较毒的,虽然我上分了... Problem A  QAQ 题目大意:给你一个由小写字母构成的字符串,问你里面有多少个QAQ. 思路:找字符串中的A然后找两边的Q即可,可以枚举找Q, ...

  5. codeforces #447 894A QAQ 894B Ralph And His Magic Field 894C Marco and GCD Sequence

    A.QAQ 题目大意:从给定的字符串中找出QAQ的个数,三个字母的位置可以不连续 思路:暴力求解,先找到A的位置,往前扫,往后扫寻找Q的个数q1,q2,然 后相乘得到q1*q2,这就是这个A能够找到的 ...

  6. 【Codeforces Round #447 (Div. 2) B】Ralph And His Magic Field

    | [链接] 我是链接,点我呀:) [题意] 给你一个n*m矩阵,让你在里面填数字. 使得每一行的数字的乘积都为k; 且每一列的数字的乘积都为k; k只能为1或-1 [题解] 显然每个位置只能填1或- ...

  7. Codeforces Round #447 (Div. 2) 题解

    A.很水的题目,3个for循环就可以了 #include <iostream> #include <cstdio> #include <cstring> using ...

  8. Codeforces Round #447 (Div. 2) C 构造

    现在有一个长度为n的数列 n不超过4000 求出它的gcd生成set 生成方式是对<i,j> insert进去(a[i] ^ a[i+1] ... ^a[j]) i<=j 然而现在给 ...

  9. Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence【构造/GCD】

    C. Marco and GCD Sequence time limit per test 1 second memory limit per test 256 megabytes input sta ...

随机推荐

  1. Singleton patterns 单件(创建型模式)

    1.模式分类 1.1  从目的来看: •      – 创建型(Creational)模式:负责对象创建. •      – 结构型(Structural)模式:处理类与对象间的组合. •      ...

  2. Java 打印* 三角形

    package anli1; public class sanjiaoxing { public static void main(String[] agrs){ System.out.println ...

  3. jsp处理

    jsp处理步骤: 浏览器发送一个HTTP请求给服务器. Web服务器识别出这是一个对JSP网页的请求,并且将该请求传递给JSP引擎.通过使用URL或者.jsp文件来完成. JSP引擎从磁盘中载入JSP ...

  4. c++ 2.1 编译器何时创建默认构造函数

    我们通常会说当生命一个 class 时,如果我们不为该 class 指定一个 constructor,那么编译器会替我们实现一个 connstructor,那么这种说法一定对吗? 事实上,这是不对的. ...

  5. struct&&class 空的大小

    #include using namespace std; class ClassA { }; class ClassB { private: int b; }; class ClassC : pub ...

  6. 01、JAVA开发准备

    一.首先要认识几个名词: 1. JRE(Java Runtime Environment ,JAVA运行环境):它包含Java虚拟机(JVM,Java Virtual Machine)和Java程序所 ...

  7. J2EE的十三个技术——JSP

    简介 JSP,Java Server  Page,Java服务器页面.它是在传统的网页HTML文件中插入Java程序段(Scriptlet)和JSP标记,从而形成JSP文件,后缀名为(*.jsp). ...

  8. [洛谷P4925][1007]Scarlet的字符串不可能这么可爱

    题目大意:问字符集大小为$k$,长度为$L$的字符串,且没有长度超过$1$的回文段的个数.规定第$s(若为0则无限制)$位为$w$. 题解:懒得写了,根据是否有限制分类讨论 卡点:中途有个地方忘记取模 ...

  9. Codeforces755D PolandBall and Polygan

    题目戳这里 我们只需要计算每增加一条线后穿过了几条已有的线即可.为了方便,我们令\(K \le N/2\),并且给每条线一个方向,即\(x\)到\((x+K) \; mod \; N\).然后我们假设 ...

  10. NFS排错案例

    1.检验rpcinfo从客户端 # rpcinfo -p nfsserverip ,可以看到服务器端开的tcp/udp端口.默认都是打开的,客户端可以自己选择使用TCP/UDP program ver ...