A or B Equals C

博客围绕位运算方程展开,给出“A | B = C”和“A | C = B”两种方程。已知A和B的值,探讨在“A | C = B”方程中C的可能值数量。介绍了输入输出格式,输入包含测试用例数、二进制数长度及A、B的二进制表示,输出为C的可能值数量,结果需取模,无解时输出“IMPOSSIBLE”。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A or B Equals C

 Gym - 101028C 

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

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int t, n;
    char a[110], b[110];
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        scanf("%s%s", a, b);
        int i;
        long long int x = 1;
        for(i = 0; i < n; i++)
        {
            if(a[i] == '1' && b[i] == '1')
                x *= 2;
            if(a[i] == '1' && b[i] == '0')
                x *= 0;
            //if(a[i] == '0')
            //    x *= 1;    等于1 可以不判断
            if(x>1000000007)
                x = x % 1000000007;     //要边乘边判断
        }
        if(x == 0)
            printf("IMPOSSIBLE\n");
        else
            printf("%lld\n", x);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值