您的位置:首页 > 其它

Codeforces 894 B. Ralph And His Magic Field

2017-11-21 00:57 323 查看
B. Ralph And His Magic Field

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
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.

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.

Examples

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.

First, it's obvious that the numbers put can be only 1 or -1.
If k equals to -1 and
the parity of n and m differ,
the answer is obviously 0.

Otherwise, for the first (n - 1) lines and the first (m - 1) columns,
we can put either 1 or -1 in
it, and there're pow(2, [(n - 1) * (m - 1)]) ways in total. Then it's obvious that the
remaining numbers are uniquely determined because the product of each row and each column is known already. So in this case the answer is pow(2, [(n - 1) * (m - 1)]) .

奇偶性不同不存在可以通过归纳法证明,(2^(m-1))^(n-1)可以这样想,Cn1+Cn3+...=2^(n-1), Cn0+Cn2+...=2^(n-1), 所以每一行的情况数就是2^(m-1), 然后有n行,但是后一行决定于前面的所有行,所以只能有(2^(m-1))^(n-1)种情况了

哎,智障了

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int mod=1000000007;
ll po(ll y, ll x){
ll now=y; ll ans=1;
for (ll j=x; j!=0; j=j/2){
if (j&1) {
ans=ans*now%mod;
}
now=now*now%mod;
}
return ans;
}
ll n, m, k;
int main(){
cin>>n>>m>>k;
if ((n+m)&1 && k==-1){
cout<<0<<endl;
return 0;
}
//cout<<po((n-1)*(m-1))<<endl;

cout<<po(po(2,n-1),m-1)<<endl;
}


还有,这题注意细节,m*n会爆long long,所以单独算(最后一行代码)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: