codeforces 894B - Ralph And His Magic Field - [数学题]
题目链接:https://cn.vjudge.net/problem/CodeForces-894B
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 n, m and k (1 ≤ n, m ≤ 1018, k is either 1 or -1).
Output
Print a single number denoting the answer modulo 1000000007.
Example
1 1 -1
1
1 3 1
1
3 3 -1
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=-1或1,在所有方格里面填上-1或1,使得每行每列的乘积都为k,则算作一种方案,求总共有多少种不同方案。
题解:
①当n+m为奇数,k=-1时,方案数=0;
因为这时,n和m必然为一奇一偶,不妨设n为奇数,m为偶数;
则在每一行上必然要放奇数个-1,那么这样可以知道-1的总个数是偶数(奇数行,每行奇数个-1);
但是,同时每一列上也要放奇数个-1,那么-1的总个数是奇数(偶数列,每列奇数个-1);
互相矛盾,所以不存在这样的方案。
②其他情况下,存在至少一种方案,此时我们设有矩阵A[n][m]:
a[1][1] …………………… a[1][m-1] a[1][m]
……………………………………………………
……………………………………………………
a[n-1][1] ……………… a[n-1][m-1] a[n-1][m]
a[n][1] …………………… a[n][m-1] a[n][m]
此时矩阵A[n-1][m-1]里面可以随意填入1或者-1,则对应的 a[n][1] ~ a[n-1][m] 和 a[1][m] ~ a[n-1][m] 需要取-1或者1来使得行列为k;
例如:,因为
,所以
和
,所以a[n][m]存在,所以方案存在。
因此我们不能难算出方案数为;
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = ;
ll n,m;int k;
ll fpow(ll a,ll b){//快速幂
ll r=,base=a%MOD;
while(b){
if(b&) r*=base , r%=MOD;
base*=base;
base%=MOD;
b>>=;
}
return r;
}
int main()
{
cin>>n>>m>>k;
if(k==- && (n+m)%==)
{
printf("0\n");
return ;
} ll ans=fpow(,n-);
ans=fpow(ans,m-);
cout<<ans<<endl;
}
PS.显然最大10^18数量级的n和m直接乘起来肯定爆炸longlong,所以分两次快速幂即可。
PS2.此处#include<bits/stdc++.h>的话,因为包含进了pow()函数,这样我们就要给快速幂函数改个名字(比如fpow……),避免错误。
codeforces 894B - Ralph And His Magic Field - [数学题]的更多相关文章
- Codeforces 894B - Ralph And His Magic Field
894B - Ralph And His Magic Field 思路: 当k为1时,如果n和m奇偶性不同,那么没有答案. 可以证明,在其他情况下有答案,且答案为2^(n-1)*(m-1),因为前n- ...
- 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 894.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【数论/组合数学】
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或- ...
- CF894B Ralph And His Magic Field
题目链接:http://codeforces.com/contest/894/problem/B 题目大意: 往一个 \(n \times m\) 的网格中填数字 \((1 \le n,m \le 1 ...
- codeforces #369div2 B. Chris and Magic Square
题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...
- codeforces 711B B. Chris and Magic Square(水题)
题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...
随机推荐
- IOS 基于APNS消息推送原理与实现(JAVA后台)--转
Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...
- IOS 应用官方接口地址
地址: http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-a ...
- Linux wget 命令下载文件
wget是Linux系统中用来下载文件的工具,其功能还是比较多的,能够下载单个文件,也可以分段下载,下面小编将针对wget命令的用法给大家做个实例介绍. 实例1 :下载单个文件 # wget http ...
- SpringBoot(四)-- 整合Servlet、Filter、Listener
SpringBoot中有两种方式可以添加 Servlet.Filter.Listener. 1.代码注册 通过ServletRegistrationBean. FilterRegistrationBe ...
- nessus 激活码
nessus激活码的申请 nessus屏蔽了中国的激活码申请,中国IP申请的时候会直接跳转到购买商业版的页面. 解决方法: 使用IE代理或者VPN,用美国的IP最好,然后访问网址: http://ww ...
- Selenium 查找节点
Selenium 可以驱动浏览器完成各种操作,比如填充表单.模拟点击等.比如,我们想要完成向某个输入框输入文字的操作,总需要知道这个输入框在哪里吧?而 Selenium 提供了一系列查找节点的方法,我 ...
- Ubuntu12.04编译Android2.3.4
Ubuntu12.04编译Android2.3.4 1.下载Ubuntuubuntu-12.04-dvd-i386.iso2.使用U盘安装,启动盘制作用unetbootin-windows-568工具 ...
- [Windows] 解决 VLC Media Player 的 Crash Reporting 消息弹窗
运行环境:Windows 8.1 (64bits), VLC Media Player 2.1.3 异常描述:首次启动VLC播放影音文件时,一切正常.此后每次启动VLC都弹出"VLC Cra ...
- STL——序列式容器
一.容器概述与分类 1. STL容器即是将运用最广的一些数据结构实现出来.常用的数据结构有array, list, tree, stack, queue, hash table, set, map…… ...
- codeforces水题100道 第十三题 Codeforces Round #166 (Div. 2) A. Beautiful Year (brute force)
题目链接:http://www.codeforces.com/problemset/problem/271/A题意:给你一个四位数,求比这个数大的最小的满足四个位的数字不同的四位数.C++代码: #i ...