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 ...
随机推荐
- hdu - 6276,2018CCPC湖南全国邀请赛A题,水题,二分
题意: 求H的最大值, H是指存在H篇论文,这H篇被引用的次数都大于等于H次. 思路:题意得, 最多只有N遍论文,所以H的最大值为N, 常识得知H的最小值为0. 所以H的答案在[0,N]之间,二分 ...
- Redis 指令
一个key可以存放将近40亿条数据 选择库 select 2 (代表选择第三个库) 增加key set db_number 11 删除key del key 获取值 get db_n ...
- VR产业链全景图
- OOP 1.4 内联函数和重载函数函数参数缺省值
1.内联函数 存在的背景:函数调用存在开销(调用时候参数压栈,返回地址压栈:返回时从栈取出返回地址,跳转到返回地址.总共需要几条指令的开销).如果函数指令较少,调用多次,函数调用的开销占比大. 内联函 ...
- Learn Docker(一)—软件安装与常规操作
一.安装Docker Windows平台 在Windows10 X64专业版上可以直接下载Docker原生应用进行安装,在控制面板的程序与功能里启用Hyper-v,之后就可以运行docker程序啦. ...
- lintcode-387-最小差
387-最小差 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回 ...
- Varnish是一款高性能的开源HTTP加速器
如何衡量缓存系统的优劣性 1:缓存命中率: 在memcached服务器中,get_hits的值表示缓存命中的次数,get_misses的值表示没有命中的次数,那么命中率的计算公式就是:命中率=get_ ...
- crontab部署定时任务
1.安装cron工具:apt-getinstall cron 2.开启定时任务:crontab –e 定时任务语句格式为:执行周期+命令. 周期有5个域,分别是分,时,日(day of month), ...
- 基于opencv的小波变换代码和图像结果
#include "stdafx.h" #include "WaveTransform.h" #include <math.h> #include ...
- 在Delphi中动态地使用SQL查询语句 Adoquery sql 参数 冒号
在Delphi中动态地使用SQL查询语句 在一般的数据库管理系统中,通常都需要应用SQL查询语句来提高程序的动态特性.下面介绍如何在Delphi中实现这种功能.在Delphi中,使用SQL查询语句的途 ...