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−1visgni2i, 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−1vaisgni2i.
The fourth line contains n integers, and the i-th of them represents vbi−1. The value of b is ∑i=0n−1vbisgni2i.
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−1vcisgni2i.
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;
}