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 ...
随机推荐
- Git上手:Git扫盲区
Git 自述Git 是由伟大的电脑程序员Linus Torvalds编写的一个开源的,分布式的版本控制系统软件. Git 核心原理Git 利用底层数据结构,通过指向索引对象的可变指针,保存文件快照. ...
- Oracle 学习----:Oracle删除表时报错:表或视图不存在
表明明存在,但是删除时却报错:表或视图不存在. 可能的原因之一是表名包含了小写,可以用双引号包含表名通过drop命令来删除, 如下所示: drop table "tmp_ST" ; ...
- mysql语法结构
环境:win7 64位.mysql 适合阅读者:对sql基本语法有一定了解 <建表语句>: create table <表名>( <列名> <类型> & ...
- shell之ip命令
转:出处我也不知道了,学习时候记下的笔记 1.作用 ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具,例如ifconfig.route等,使用权限为超级用户. ...
- hdu2010(dfs+剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- table纵横都需要下拉框
table 溢出,下拉框显示不全 <div class="table-scrollable"style="height: 500px; overflow-y: vi ...
- C#类和类成员初始化顺序
1.不带静态成员的普通类,首先通过构造函数初始化. 2.带静态属性的类,无论是普通类还是静态类,都会先初始化静态字段,再执行构造函数. 3.类初始化时,不会执行类中方法,无论是否是静态.若想执行方法, ...
- 详解Django-auth-ldap 配置方法
使用场景 公司内部使用Django作为后端服务框架的Web服务,当需要使用公司内部搭建的Ldap 或者 Windows 的AD服务器作为Web登录认证系统时,就需要这个Django-auth-ldap ...
- [poj] 1389 Area of Simple Polygons
原题 线段树+扫描线 对于这样一个不规则图形,我们要求他的面积有两种方法,割和补. 补显然不行,因为补完你需要求补上去的内部分不规则图形面积-- 那么怎么割呢? 像这样: 我们就转化成了无数个矩形的和 ...
- POJ 1523 SPF 求割点的好(板子)题!
题意: 给个无向图,问有多少个割点,对于每个割点求删除这个点之后会产生多少新的点双联通分量 题还是很果的 怎么求割点请参考tarjan无向图 关于能产生几个新的双联通分量,对于每个节点u来说,我们判断 ...