[CSP-S Simulation Test]: bit arithmetic (math)

Portal Title (title 72 inside)


Input Format

  $ Bit.in $ input file
  for each input file contains $ T $ test cases. The first line of the input file integer $ T $, indicates the number of data sets. Then $ T $ rows, each row represents a set of test data comprising each test three numbers separated by spaces $ ResultAnd, ResultOr, ResultXor $, expressed sequence $ a \ & b, a | b, a \ text { b $} ^ limits of.
  If three numbers of an item is $ -1 $, then that this is not a limitation. Otherwise the number must be a non-negative integer, A represents $ $ $ b $ and results after this operation.


Output Format

  Bit.out input file $ $
  $ T $ lines, each an integer or a string $ "inf" $ (output without the quotes).
  Integer or string of $ I $ $ row indicates the answer to the first test data set I $.


Sample

Sample input:

10
20 1015 995
921 661 -1
-1 375 -1
30445634 30446311 30446245
11997588 11998143 555
-1 65535 -1
-1 465530605 312684161
2118209 930739953 928621744
69739040 402620388 332881348
4594346 533159678 528565332

Sample output:

128
0
2187
0
32
43046721
4096
32768
131072
4096


Data range and tips

Sample explained:

Swatch $ T = 10 $, $ I $ row and the first sample is a set of input data from the first $ (2 * i-1) $ test points

data range:

For all the test points: $ T \ leqslant 10, ResultAnd , ResultOr, ResultXor $ all $ [- 1, 10 ^ 9] integer in the range of $. On the same set of data, $ ResultAnd, ResultOr, ResultXor $ are not $ -1 $.
The first $ 1 $ and $ 5 $ test points: $ 0 \ leqslant ResultOr \ leqslant 1023 $
of $ 6 $ to $ 10 $ test points: $ ResultOr-ResultAnd \ leqslant 1000 , ResultOr> = 0, ResultAnd> = 0 $
of $ 11 $ to $ 16 $ test points: the answer does not appear $ INF $
of $ 11 $ and $ 12 $ test points further satisfies: $ ResultAnd = ResultXor = -1 $
of $ 17, $ test point satisfies: is not equal to $ INF $ of the answer will not exceed the range of $ int $ data type can represent the
first $ 19,20 $ test points: no special restrictions


answer

Vigorously Category talk to.

Bit operation must first be extracted for each treatment.

We may assume $ a [i], b [i] .c [i] $ denote $ \ &, |, \ text {^} $ each bit.

Followed by enumeration:

  . $ \ Alpha $ has a limit: $ ans = 2 ^ {\ sum \ limits_ {i = 1} ^ {30} a [i] \ text {^} b [i]} $.

  $\beta.\&$没有限制:$ans=2^{\sum \limits_{i=1}^{30}b[i]\&c[i]}$。

  $\gamma.|$没有限制:$ans=2^{\sum \limits_{i=1}^{30}c[i]}$。

  $\delta.\text{^}$没有限制:$ans=2^{\sum \limits_{i=1}^{30}a[i]\text{^}b[i]}$。

  $\epsilon.$只有$\&$有限制:$ans=inf$。

  $\zeta.$只有$|$有限制:$ans=3^{\sum \limits_{i=1}^{30}b[i]}$。

  $eta.$只有$\text{^}$有限制:$ans=inf$。

时间复杂度:$\Theta(30\times T)$。

期望得分:$100$分。

实际得分:$100$分。


代码时刻

#include<bits/stdc++.h>
using namespace std;
int AND,OR,XOR;
int a[50],b[50],c[50];
long long qpow(long long x,long long y)
{
	long long res=1;
	while(y)
	{	
		if(y&1)res=res*x;
		x=x*x;
		y>>=1;
	}
	return res;
}
int main()
{
	int T;scanf("%d",&T);
	while(T--)
	{
		int sum=0;
		scanf("%d%d%d",&AND,&OR,&XOR);
		if(AND!=-1&&OR==-1&&XOR==-1){puts("inf");continue;}
		if(AND==-1&&OR==-1&&XOR!=-1){puts("inf");continue;}
		if(AND!=-1&&OR!=-1&&XOR!=-1)
		{
			for(int i=0;i<=30;i++)
			{
				a[i]=(AND>>i)&1;
				b[i]=(OR>>i)&1;
				c[i]=(XOR>>i)&1;
				if(a[i]&&!b[i]){puts("0");goto nxt;}
				if(a[i]&&c[i]){puts("0");goto nxt;}
				if(!b[i]&&c[i]){puts("0");goto nxt;}
				sum+=a[i]^b[i];
			}
			printf("%lld\n",qpow(2,sum));
		}
		if(AND!=-1&&OR==-1&&XOR!=-1)
		{
			for(int i=0;i<=30;i++)
			{
				a[i]=(AND>>i)&1;
				c[i]=(XOR>>i)&1;
				if(a[i]&&c[i]){puts("0");goto nxt;}
				sum+=c[i];
			}
			printf("%lld\n",qpow(2,sum));
		}
		if(AND==-1&&OR!=-1&&XOR!=-1)
		{
			for(int i=0;i<=30;i++)
			{
				b[i]=(OR>>i)&1;
				c[i]=(XOR>>i)&1;
				if(!b[i]&&c[i]){puts("0");goto nxt;}
				sum+=b[i]&c[i];
			}
			printf("%lld\n",qpow(2,sum));
		}
		if(AND!=-1&&OR!=-1&&XOR==-1)
		{
			for(int i=0;i<=30;i++)
			{
				a[i]=(AND>>i)&1;
				b[i]=(OR>>i)&1;
				sum+=a[i]^b[i];
				if(a[i]&&!b[i]){puts("0");goto nxt;}
			}
			printf("%lld\n",qpow(2,sum));
		}
		if(AND==-1&&OR!=-1&&XOR==-1)
		{
			for(int i=0;i<=30;i++)
				sum+=(OR>>i)&1;
			printf("%lld\n",qpow(3,sum));
		}
		nxt:;
	}
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11695181.html