A or B Equals C Gym - 101028C

版权声明:文章全是安利转载啦,自娱自乐喽, 可以随意操作哟! https://blog.csdn.net/ggqinglfu/article/details/82192000

 下满有详细解析

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 :

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

Input

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)

 ExampleInput

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

 我给作者 xx点了啦赞, 小心心送给他   (zhuanzia )

  1. 1.两个二进制数的数据范围是100位,用整型类型肯定存不下,用字符串

  2. 2.题目中要求取余,过程当中是乘法运算很可能会超出数据范围,所以在每次乘的时候取余

  3. int的取值范围: -2147483648~2147483647 10位

  4. unsigned int的取值范围:0~4294967295

  5. long long的取值范围:-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 19位

我给作者 OneLine_点了啦赞, 小心心送给他, 作为我借用学习他 的回报。(转载版权声明)

分三种情况讨论

1)同一位上 A和B都为1 那么C可以为0 也可以为1     * 2 是这么来的

2)同一位上 A为1 但B为0 则没有C符合条件               break, sum归零 1 是这么来的

3)同一位上 A和B都为0 C只能为0 或是 A为0 B为1 则C只能为1 (即C只有一种情况) 省略的 sum * 1 是这么来的

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

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

 我还是不太了解 按位或运算 , 虽然 哎呀 , 过段时间就理解了

啊, 交个代码 增加个ac数

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#define INF 0x3f3f3f3f
using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    int t, n, sum;     // 每次sum * 2 后, sum %MAXN 所以sum可以用int 型
    string a, b; // char a[1000100]; char b[1000100];
    cin >> t;
    while(t--)
    {
        cin >> n >> a >> b;
        sum = 1;
        int flag = 1;
        for (int i = 0; i < n; i++)
        {
            if(a[i] == '1' && b[i] == '1')
                sum *=2;
            else if(a[i] == '1' && b[i] == '0')
            {
                flag = 0;
                break;
            }
            sum %= 1000000007;
        }
        if(!flag)
            cout << "IMPOSSIBLE" << endl;
        else
            cout << sum << endl;
    }
    return 0;
}

 

猜你喜欢

转载自blog.csdn.net/ggqinglfu/article/details/82192000
今日推荐