[思维]2021icpc第二场网络选拔赛M Addition

The numbers in the computer are usually stored in binary form. But in Little A's computer, the numbers are stored in a different way.

The computer uses n bits to store a number. The value it stores is ∑i=0n−1​vi​sgni​2i, where v is an array of length n containing only 0 and 1, and sgn is a predefined array of length n containing only −1 and 1. It is not difficult to find that every expressible integer has a unique expression.

Little A gives you the binary representation of a and b in his computer, and you should report the binary representation of a+b in his computer. It is guaranteed that max{∣a∣,∣b∣}≤108, and all integers in [−109,109] can be expressed in his computer.

Input

The first line contains an integer n, which represents the number of bits used to store an integer.

The second line contains n integers, and the i-th of them represents sgni−1​.

The third line contains n integers, and the i-th of them represents vai−1​. The value of a is ∑i=0n−1​vai​sgni​2i.

The fourth line contains n integers, and the i-th of them represents vbi−1​. The value of b is ∑i=0n−1​vbi​sgni​2i.

It is guaranteed that 32≤n≤60,sgni​∈{−1,1},vai​,vbi​∈{0,1}, and max{∣a∣,∣b∣}≤108.

Output

Output one line containing n integers, separated by spaces. The i-th of them represents vci−1​∈{0,1}. There should be no extra spaces at the end of the line.

You should guarantee that a+b=∑i=0n−1​vci​sgni​2i.

Your output must be in a single line.

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

结尾无空行

题意: 给出n位数字每一位的权重(1或-1),以及这个权重下的两个数字a,b二进制形式,求在给定权重下a+b的二进制表示。

分析: 当时想多了,这题实际上只需要先把a和b的各位分别相加,之后逐位处理不合法数值即可,处理方法主要是改变后一位来等价替换当前位。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int n, a[70], b[70], c[70], ans[70];

signed main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
		cin >> c[i];
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	for(int i = 1; i <= n; i++)
		cin >> b[i];
	for(int i = 1; i <= n; i++)
		ans[i] = a[i]+b[i];
	for(int i = 1; i <= n; i++)
		if(ans[i] >= 2)
			if(c[i] == c[i+1])
				ans[i+1] += ans[i]/2, ans[i] %= 2;
			else
				ans[i+1] -= ans[i]/2, ans[i] %= 2;
		else if(ans[i] <= -1)
			if(c[i] == c[i+1])
				ans[i+1] -= (-ans[i]+1)/2, ans[i] = (ans[i]%2+2)%2;
			else
				ans[i+1] += (-ans[i]+1)/2, ans[i] = (ans[i]%2+2)%2;
	cout << ans[1];
	for(int i = 2; i <= n; i++)
		cout << ' ' << ans[i];
    return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值