Gym 101028C A or B Equals C

原题地址:http://codeforces.com/gym/101028/problem/C


Rami went back from school and he had an easy homework about bitwise operations (and,or,..) The homework was like this : You have an equation : " A | B = C " ( this bar '|' means OR ) you are given the value of A and B , you need to find the value of C ?? as we said before this is an easy question for Rami to solve ,but he wonderd if the equation was like this: " A | C = B " and he is given the value of A and B , how many value of C exists ?? Rami wants your help in this hard question ... He will help you as much as he can ,so he will convert the two decimal numbers(A and B) to binary representaion ( like this: if A=6 –> A=110 ) and this what he knows about OR operation and might be helpfull with your task :

Input

The input consists of several test cases. The first line of the input contains a single integer T, the number of the test cases. each test case consists of 3 lines : the 1st line is the length of the binary representaion of the 2 numbers.1<=n<=100 the 2nd line is the number A in base 2 .(a string consits of 0s and 1s only ) the 3rd line is the number B in base 2 .(a string consits of 0s and 1s only )

Output

for each test case,you need to print the number of possible values of C that make the eqaution correct in a seperate line .(read page 2 carefully)

Example
Input
3
2
10
11
3
110
110
4
1110
1011
Output
2
4
IMPOSSIBLE
Note

as the answer may be very very large , you should print the answer mod 1000000007. there might be no answer for the equation also , in this case you should print "IMPOSSIBLE" (without qoutes).


题意:

|表示或运算 给出A与B的长度 以及A B 

扫描二维码关注公众号,回复: 1685313 查看本文章

求 有多少个C能满足 A | C=B 

或运算:有真为真,全假为假 (即有1得1 全为0得0)

其他运算可以看 Forrest_Gao 这篇博客 :按位与、或、异或等运算方法

理解:

分三种情况讨论

1)同一位上 A和B都为1 那么C可以为0 也可以为1

2)同一位上 A为1 但B为0 则没有C符合条件

3)同一位上 A和B都为0 C只能为0 或是 A为0 B为1 则C只能为1 (即C只有一种情况)

将A和B的每一位一一对比讨论 每一位的情况进行相乘 得到所有解的数量

PS:因为最后的数可能会变得很大 所以在每一次相乘的时候 都要对原数进行取模操作


放代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;
char a[105],b[105];
const long long mm=1e9+7;
int main()
{
    int n,m;
    scanf("%d",&n);
    for (int i=0;i<n;i++)
    {
        int sum=1;
        scanf("%d",&m);
        scanf("%s%s",a,b);
        for (int j=0;j<m;j++)
        {
            if (a[j]=='1'&&b[j]=='0')
                sum=0;
            else if (a[j]=='1'&&b[j]=='1')
                sum*=2;
            else sum*=1;
            sum%=mm;
        }
        sum=sum%mm;
        if (sum!=0)
            printf("%d\n",sum);
        else printf("IMPOSSIBLE\n");
    }
    return 0;
}


继续加油哇~

阅读量猛增(ง •_•)ง  还有点小激动 谢谢大家的阅读

猜你喜欢

转载自blog.csdn.net/oneline_/article/details/80638420