Addition (9.25ICPC Game 2 M)

Keywords: Algorithm

Title Description

Enter description

Output description

sample input

32
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

sample output

0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Tips


Special note: the output Vci can only be 0 / 1.

Main idea of the topic: given the symbol digit group sgn [], and the array VA [], VB [] represents each digit of the values a and b. now it is specified that the calculation method of the values a and b is the formula in the topic. The answer c obtained after the demand a + b is transformed through the formula to represent the Vc [] array of each digit.

It is not difficult to find that the power i of 2 in the formula of this problem reflects the weight of each bit corresponding to the binary. The result is that the high bit is on the right and the low bit is on the left. That is, for sequence 1110, the value of the corresponding last a is 0111, and sgn [] is the symbol controlling each bit.

Therefore, the calculation method of this problem can be transformed into (only when the symbol bits are + 1): add and carry the bits of the arrays Va and Vb from low to high, and the answer of each bit obtained after processing is the corresponding bit of Vc. From this example, we can get: 1 + 111 = 1000 (the leading 0 is omitted in the operation process), and then output 1000 in reverse order.

When the sign bit exists - 1, in order to easily identify the positive and negative of the corresponding bit in Vc, a negative sign can be added to the Vc [] array to distinguish it. For example (the array subscript specified here is for better demonstration example): ① when SGN [i] = - 1, VA [i] = 1, VB [i] = 0, Vc [i] = - 1; ② When SGN [i] = - 1, VA [i] = 1, VB [i] = 1, Vc [i] = 0, Vc [i + 1] due to carry = - 1;

However, since the output Vc can only be 0 / 1, it is necessary to transform the original Vc array with - 1 possibility. The following three cases are classified and discussed.




Among them, the difference between example 1 and 2 and 3 is: for the array c that has processed the carry but still has symbols, whether there is a situation opposite to the array sgn symbols (i.e., one is - 1 and the other is + 1). When this situation occurs, special processing is required, otherwise the absolute value of this bit can be taken directly.

In example 2, due to the carry of bit weight 2, the array c is opposite to the sign bit sgn in bit weight 4. In this example, since both a and b contain bit weight 2, the contribution to c should be (+ 2) + + 2), and (+ 4 should be taken after carry. However, since the sign bit sgn of bit weight 4 is - 1, the contribution to c here is (- 4). Since the value of the sign bit sgn has been determined, the contribution to c can only be made (+ 4) by means of matching, that is, the latter bit (bit weight 8) is added to obtain (+ 8) + (- 4) = (+ 4);

Similarly, in example 3, because the symbols at bit weight 8 are opposite, it is necessary to make (- 16) + + 8 = (- 8) for matching, that is, make the latter bit subtract one. Due to the chain reaction, bit weight 16 will carry - 2 until the operation has no effect on subsequent bits.

Therefore, the core of this problem is to judge whether array c [] is opposite to array sgn []. When sgn = 1 and c = - 1, the latter bit is processed with - 1; When sgn = - 1 and c = 1, the subsequent bits are processed with + 1, and the subsequent bits are traversed continuously to ensure their legitimacy.

Reference code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100;
int a[N],b[N],s[N],ans[N],c[N];

int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n;cin>>n;
	for(int i=0;i<n;i++)cin>>s[i];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];
	for(int i=0;i<n;i++){
		c[i]+=s[i]*a[i]+s[i]*b[i];
		if(c[i]>=2){
			c[i]=c[i]%2;
			c[i+1]++;
		}
		if(c[i]<=-2){
			c[i]=c[i]+2;
			c[i+1]--;
		}
	}
	for(int i=0;i<n;i++){
		if(s[i]==-1&&c[i]==-1)ans[i]=1;
		if(s[i]==1&&c[i]==1)ans[i]=1;
		if(c[i]==0)ans[i]=0;
		if(s[i]==-1&&c[i]==1){
			ans[i]=1;
			c[i+1]++;
			for(int j=1;j+i<n;j++){
				if(c[i+j]==2){
					c[i+j]=0;
					c[i+j+1]++;
				}else break;
			}
		}
		
		if(s[i]==1&&c[i]==-1){
			ans[i]=1;
			c[i+1]--;
			for(int j=1;j+i<n;j++){
				if(c[i+j]==-2){
					c[i+j]=0;
					c[i+j+1]--;
				}else break;
			}
			
		}
	}

	for(int i=0;i<n;i++){
		cout<<ans[i];
		if(i!=n-1)
			cout<<' ';
	}
	return 0;
} 

Posted by pelleas1022 on Mon, 27 Sep 2021 05:58:52 -0700