Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field【数论/组合数学】
1 second
256 megabytes
standard input
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.
The only line contains three integers n, m and k (1 ≤ n, m ≤ 1018, k is either 1 or -1).
Print a single number denoting the answer modulo 1000000007.
1 1 -1
1
1 3 1
1
3 3 -1
16
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【数论/组合数学】的更多相关文章
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field 数学
题目链接 题意:给你三个数n,m,k;让你构造出一个nm的矩阵,矩阵元素只有两个值(1,-1),且满足每行每列的乘积为k,问你多少个矩阵. 解法:首先,如果n,m奇偶不同,且k=-1时,必然无解: 设 ...
- 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 ...
- Codeforces Round #447 (Div. 2) 题解 【ABCDE】
BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imag ...
- Codeforces Round #447 (Div. 2)
我感觉这场CF还是比较毒的,虽然我上分了... Problem A QAQ 题目大意:给你一个由小写字母构成的字符串,问你里面有多少个QAQ. 思路:找字符串中的A然后找两边的Q即可,可以枚举找Q, ...
- 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能够找到的 ...
- 【Codeforces Round #447 (Div. 2) B】Ralph And His Magic Field
| [链接] 我是链接,点我呀:) [题意] 给你一个n*m矩阵,让你在里面填数字. 使得每一行的数字的乘积都为k; 且每一列的数字的乘积都为k; k只能为1或-1 [题解] 显然每个位置只能填1或- ...
- Codeforces Round #447 (Div. 2) 题解
A.很水的题目,3个for循环就可以了 #include <iostream> #include <cstdio> #include <cstring> using ...
- Codeforces Round #447 (Div. 2) C 构造
现在有一个长度为n的数列 n不超过4000 求出它的gcd生成set 生成方式是对<i,j> insert进去(a[i] ^ a[i+1] ... ^a[j]) i<=j 然而现在给 ...
- 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 ...
随机推荐
- react基本知识点合集
妹子UI里面有React的相关组件与用法:http://amazeui.org/react/components React官方网站:https://facebook.github.io/react/ ...
- 《Cracking the Coding Interview》——第17章:普通题——题目1
2014-04-28 21:45 题目:就地交换两个数,不使用额外的变量. 解法:没说是整数,我姑且先当整数处理吧.就地交换可以用加法.乘法.异或完成,其中乘法和加法都存在溢出问题.三种方法都不能处理 ...
- ajax向Asp.NET后端传递数组型数据
近日,在开发一个组件的过程中,需要通过Ajax对象向Asp.NET后端传递一个比较复杂的表单,表单中的一个字段是数组类型,我能想到的办法是用JSON.stringify将前端的数组对象序列化成字符串, ...
- UasyUi的各种方法整理
UasyUi的各种方法整理: 1.拖动 放置 droppable $('#dd').droppable({ }); 2.创建可变大小的窗口 resizable $('#rr').resizable({ ...
- FlexGrid布局
FlexGrid布局: Grid布局时网格大小是固定的,如果想网格大小不同的界面可以使用FlexGrid布局.FlexGrid是更加灵活的Grid布局.FlexGrid布局类是wx.FlexGridS ...
- 聊聊、Java SPI
SPI,Service Provider Interface,服务提供者接口. Animal 接口 package com.rockcode.www.spi; public interface Ani ...
- MVC从Controller到view进行传值的方法
这几天基本上都是交接的一些杂事,没有什么工作任务,就有空来回顾一下MVC.虽然工作中也用到了MVC,但已经被微软的架构师设计的找不到MVC的影子了,可能有别的考虑吧,至今还没研究出来.所以,今天就来回 ...
- POJ 3907 Build Your Home | 计算多边形面积
给个多边形 计算面积 输出要四舍五入 直接用向量叉乘就好 四舍五入可以+0.5向下取整 #include<cstdio> #include<algorithm> #includ ...
- 7月10日day2总结
今天学习过程和小结 上午: java项目建立方法,maven Tomcat的部署和MySQL的安装与权限. 分配时中datanote和namenote DataNode 作用 1Data Node以数 ...
- Linux命令之type,whatis,whereis,which,locate,find
第一个:type--查询一个命令的类型 -查询一个命令为内部或者外部命令的命令: -linux的众多命令中,有内部命令和外部命令,这时可以用type命令来查询一个命令到底是属于内部命令还是属于外部命令 ...