Codeforces 894.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的方格,每个格子里只能填1或-1,要求最后每一列的数的乘积和每一行的数的乘积都等于k,问有多少种方案数.
分析:非常巧妙的思想.每放一个数都会对当前行和列造成影响,于是就要在当前行和当前列的其它位置放数来消除影响.因为最后的结果只有-1和1两种,所以为了消除影响只需要放一个数就行了,那么把消除影响的数放在最后一行或最后一列.那么前n-1行和前m-1列就可以随便放了,只需要在最后一行和最后一列调整到题目的要求就行了.但是这样会不会使得最后一行或最后一列不合法呢?答案是不会,分类讨论一下:如果k=1,有两个-1放在了不同行和不同列,那么不会造成不合法,因为同时补一个-1就能使得最后一行和最后一列的正负性相同.如果两个-1放在了同一行或同一列,那么最后一行和最后一列中一定有一个放了2个-1,另外一个放1,正负性还是不变,所以不会使得最后一行或最后一列不合法.但是有个特例:n+m %2 = 1并且k=-1是一定没有合法的方案的.因为每一行要放奇数个-1,总共有奇数行,那么要放奇数个-1,每一列要放奇数个-1,总共有偶数列,那么要放偶数个-1,矛盾了,所以没有合法的方案,特判一下就可以了.
打表也挺容易找到规律.get一个新技巧,以前处理这种行列有影响的题都是先处理行再来处理列,消除影响,这道题可以把行列的决定权交给最后的行列来处理.正负性往往和奇偶性有关,一定要注意检验是否每一种奇偶性都满足推导.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <algorithm> using namespace std; const int mod = 1e9 + ; typedef long long ll; ll n, m, k, ans; ll qpow(ll a, ll b)
{
ll res = ;
while (b)
{
if (b & )
res = (res * a) % mod;
a = (a * a) % mod;
b >>= ;
}
return res % mod;
} int main()
{
cin >> n >> m >> k;
if ((n + m) % == && k == -)
ans = ;
else
ans = qpow(, ((n - ) % (mod - )) * ((m - ) % (mod - )));
cout << ans % mod << endl; return ;
}
Codeforces 894.B Ralph And His Magic Field的更多相关文章
- 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 894B - Ralph And His Magic Field - [数学题]
题目链接:https://cn.vjudge.net/problem/CodeForces-894B Ralph has a magic field which is divided into n × ...
- Codeforces 894B - Ralph And His Magic Field
894B - Ralph And His Magic Field 思路: 当k为1时,如果n和m奇偶性不同,那么没有答案. 可以证明,在其他情况下有答案,且答案为2^(n-1)*(m-1),因为前n- ...
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field【数论/组合数学】
B. Ralph And His Magic Field time limit per test 1 second memory limit per test 256 megabytes input ...
- 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) B】Ralph And His Magic Field
| [链接] 我是链接,点我呀:) [题意] 给你一个n*m矩阵,让你在里面填数字. 使得每一行的数字的乘积都为k; 且每一列的数字的乘积都为k; k只能为1或-1 [题解] 显然每个位置只能填1或- ...
- [codeforces 894 E] Ralph and Mushrooms 解题报告 (SCC+拓扑排序+DP)
题目链接:http://codeforces.com/problemset/problem/894/E 题目大意: $n$个点$m$条边的有向图,每条边有一个权值,可以重复走. 第$i$次走过某条边权 ...
- CF894B Ralph And His Magic Field
题目链接:http://codeforces.com/contest/894/problem/B 题目大意: 往一个 \(n \times m\) 的网格中填数字 \((1 \le n,m \le 1 ...
- Codeforces 894.E Ralph and Mushrooms
E. Ralph and Mushrooms time limit per test 2.5 seconds memory limit per test 512 megabytes input sta ...
随机推荐
- Elasticsearch.Net 异常:[match] query doesn't support multiple fields, found [field] and [query]
用Elasticsearch.Net检索数据,报异常: )); ElasticLowLevelClient client = new ElasticLowLevelClient(settings); ...
- 天马行空-Ops平台建设概述
1 概述 什么是Ops平台,Ops平台的目标是什么,建设的考虑点有哪些?本章节以实际生活中医院的例子来进行各形象的阐述. 医院包含各种诊断治疗设备,病历库,医生.一个孕妇需要到医院 ...
- netty初认识
Netty是什么? 本质:JBoss做的一个Jar包 目的:快速开发高性能.高可靠性的网络服务器和客户端程序 优点:提供异步的.事件驱动的网络应用程序框架和工具 通俗的说:一个好使的处理Socket的 ...
- 3D打印产业链全景图
- Wormholes POJ 3259(SPFA判负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- scrum立会报告+燃尽图(第三周第三次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2286 项目地址:https://coding.net/u/wuyy694 ...
- POJ 3258(二分求最大化最小值)
题目链接:http://poj.org/problem?id=3258 题目大意是求删除哪M块石头之后似的石头之间的最短距离最大. 这道题目感觉大致代码写起来不算困难,难点在于边界处理上.我思考边界思 ...
- Ubuntu16.04安装json-c
1. 安装依赖 sudo apt-get install git gcc clang libtool autoconf automake doxygen valgrind 一些版本要求,如果版本过低可 ...
- 软工1816 · Alpha冲刺(9/10)
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 学习jQuery的AJAX部分的基础知识,对web端如何异步获取服务器信息有了 ...
- android入门 — 多线程(一)
android中的一些耗时操作,例如网络请求,如果不能及时响应,就会导致主线程被阻塞,出现ANR,非常影响用户体验,所以一些耗时的操作,我们会想办法放在子线程中去完成. android的UI操作并不是 ...