codeforces-894B Ralph And His Magic Field
2017-12-02 21:34
316 查看
B. Ralph And His Magic Field
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
题意:n行m列的矩阵只能由1和-1组成,每行每列的和为x(1或-1)的组合方式有多少种。
思路:当x=1的时候,每一行和每一列的组合都可以用最后一个1或者-1来让结果等于1。所以组合方式就是2的(n-1*m-1)次方。当x=-1的时候如果m%2和n%2不想等的话,每一行的和肯定不等于每一列的和。所以是0。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
#define ll long long
using namespace std;
ll mod=1000000007;
int main()
{
ll m,n,k;
cin>>m>>n>>k;
if(m%2!=n%2&&k==-1)
{
cout<<0<<endl;
return 0;
}
n--;m--;
n%=mod-1;
m%=mod-1;
//cout<<n<<m<<endl;
ll x=2,ans=1,b=n*m;
//cout<<b<<endl;
while(b)
{
if(b%2==1)
{
ans=ans*x%mod;
//ans%=mod;
}
x=x*x%mod;
b/=2;
}
ans%=mod;
cout<<ans<<endl;
}
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
题意:n行m列的矩阵只能由1和-1组成,每行每列的和为x(1或-1)的组合方式有多少种。
思路:当x=1的时候,每一行和每一列的组合都可以用最后一个1或者-1来让结果等于1。所以组合方式就是2的(n-1*m-1)次方。当x=-1的时候如果m%2和n%2不想等的话,每一行的和肯定不等于每一列的和。所以是0。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
#define ll long long
using namespace std;
ll mod=1000000007;
int main()
{
ll m,n,k;
cin>>m>>n>>k;
if(m%2!=n%2&&k==-1)
{
cout<<0<<endl;
return 0;
}
n--;m--;
n%=mod-1;
m%=mod-1;
//cout<<n<<m<<endl;
ll x=2,ans=1,b=n*m;
//cout<<b<<endl;
while(b)
{
if(b%2==1)
{
ans=ans*x%mod;
//ans%=mod;
}
x=x*x%mod;
b/=2;
}
ans%=mod;
cout<<ans<<endl;
}
相关文章推荐
- CodeForces 297C Splitting the Uniqueness (脑补构造题)
- Codeforces 558B Amr and The Large Array 数组美丽值
- Codeforces 201E Thoroughly Bureaucratic Organization (智商题。。)
- Codeforces-687A NP-Hard Problem(二分图染色)
- 【Codeforces Round #418 (Div. 2)】Codeforces 814E An unavoidable detour for home
- codeforces 602 D. Lipshitz Sequence
- Codeforces 106 C 多重背包
- Codeforces 164 E Compatible Numbers
- CodeForces 652D Nested Segments
- CodeForces-711D Directed Roads(拓扑排序+组合数)
- Codeforces 813E Army Creation 主席树(在线,求[l,r]内比x大的数的个数)
- CodeForces 883I
- codeforces 339A.Helpful Maths B.Xenia and Ringroad 两水题
- Codeforces 551E GukiZ and GukiZiana 分块
- CodeForces 645B Mischievous Mess Makers
- codeforces 675C Money Transfers(贪心)
- CodeForces - 660C Hard Process
- Codeforces 855 B Marvolo Gaunt's Ring(dp)
- codeforces 339C
- codeforces 554B Ohana Cleans Up