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 nm and k (1?≤?n,?m?≤?1018k is either 1 or -1).

Output

Print a single number denoting the answer modulo 1000000007.

Example

Input

1 1 -1

Output

1

Input

1 3 1

Output

1

Input

3 3 -1

Output

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 = 1000000007;
ll n,m;int k;
ll fpow(ll a,ll b){//快速幂
    ll r=1,base=a%MOD;
    while(b){
        if(b&1) r*=base , r%=MOD;
        base*=base;
        base%=MOD;
        b>>=1;
    }
    return r;
}
int main()
{
    cin>>n>>m>>k;
    if(k==-1 && (n+m)%2==1)
    {
        printf("0\n");
        return 0;
    }

    ll ans=fpow(2,n-1);
    ans=fpow(ans,m-1);
    cout<<ans<<endl;
}

PS.显然最大10^18数量级的n和m直接乘起来肯定爆炸longlong,所以分两次快速幂即可。

PS2.此处#include<bits/stdc++.h>的话,因为包含进了pow()函数,这样我们就要给快速幂函数改个名字(比如fpow……),避免错误。

时间: 2024-10-24 07:19:26

codeforces 894B - Ralph And His Magic Field - [数学题]的相关文章

Codeforces 894 B Ralph And His Magic Field

Discription 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 p

Educational Codeforces Round 8(D. Magic Numbers(数位DP))

题目链接:点击打开链接 题意:给一个m一个d, 一个字符串a和b, 问在[a,b]范围内, 有多少个可以整除m的魔法数, 魔法数的定义是, 偶数位上都是d, 奇数位上都不是d. 思路:据说是典型的数位DP, 以前没做过数位DP, 感觉和DP差不多? 用d[i][j][p]表示当前到了第i位, 余数为j, p == 1表示目前和b串相等, p == 0表示已经比b串小了.  每次枚举第i位上放的数, 转移就行了. 区间也好弄, 我们可以先处理出小于等于b的所有情况ans1, 小于等于a的所有情况a

codeforces #369div2 B. Chris and Magic Square

题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 分析:题目不难,找到要填的那个数填进去,然后循环比较每行每列以及对角线的和是否相等,题目提交上去卡了几次要注意几点 注意:1.答案数据范围1<=x<=1e18,要用 long long 2.特殊情况,n==1时,由于一定有要填的数,所以一定有解 3.反正就是要注意看清楚题目和数据边界情况处理啦 1 #include<iostream> 2 #include<cstdio> 3 #includ

Codeforces Round #447 (Div. 2) 题解 【ABCDE】

BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imagine "Q" as eyes with tears and "A" as a mouth. Now Diamond has given Bort a string consisting of only uppercase English letters of leng

Codeforces Round #447

QAQ Ralph And His Magic Field Marco and GCD Sequence Ralph And His Tour in Binary Country Ralph and Mushrooms

Mach-O文件格式和程序从载入到运行过程

> 之前深入了解过.过去了一年多的时间.如今花些时间好好总结下,毕竟好记性不如烂笔头. 其次另一个目的,对于mach-o文件结构.关于动态载入信息那个数据区中,命令含义没有深刻掰扯清除,希望有同学能够指点下. 摘要:对于mach-o是Mac和iOS能够运行文件的格式.进程就是系统依据该格式将运行文件载入到内存后得到的结果.系统通过解析文件,建立依赖(动态库),初始化运行时环境,才干真正開始运行该App(进程) Start from Hello World 通过分析以下这个最熟悉的可运行文件.来好

Perl 内部结构详解

PerlGuts Illustrated Version 0.49, for perl 5.20 and older This document is meant to supplement the perlguts(1) manual page that comes with Perl. It contains commented illustrations of all major internal Perl data structures. Having this document han

Video for Linux Two API Specification Revision 2.6.32【转】

转自:https://www.linuxtv.org/downloads/legacy/video4linux/API/V4L2_API/spec-single/v4l2.html Video for Linux Two API Specification Revision 2.6.32 Michael H Schimek <[email protected]> Bill Dirks Original author of the V4L2 API and documentation. Hans

codeforces259B

Little Elephant and Magic Square CodeForces - 259B Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains some positive integer. At that the sums of integers in all rows, columns and diagonals of the table