A+B Problem

话说我写了一个二进制的说

有一些细节 不过还好啦 $qwq$

$Upd:$我超!怎么又没A了$\ \ \ $但是懒得修锅了((

qwq
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define GMY (520&1314)
#define char_phi signed
#define re register int
#define IFNDEF__ios_FASTIO_ return
#define ENDIF__ios_FASTIO_ return
using namespace std;
bool flag, flager;
long long x, y, X, Y, final_ans;//难不成超long long了的说
bool s1[50], s2[50];
int ans[50];
long long res;
inline long long ksm(long long a, long long b){
	res = 1;
	while(b){
		if(b & 1) res *= a;
		b >>= 1;
		a *= a;
	}
	return res;
}
void debug(){
	cout << X << " " << Y << endl;
	for(re i = 20 ; i >= 1 ; -- i){
		cout << s1[i] << " ";
	}
	cout << endl;
	for(re i = 20 ; i >= 1 ; -- i){
		cout << s2[i] << " ";
	}
	cout << endl;
}
void Kawaii(){
	cout << "qwq" << endl;
}
void work(){
	// A+B problem,考虑用二进制表示模拟进位
	cin >> x >> y;
	if(x < 0){
		x *= -1;
		flag = true;
	}
	if(y < 0){
		y *= -1;
		flag = true;
	}
	if(x < 0 && y < 0){
		flager = true;//负负得正
	}
	int i(1);
	while(x != 0){
		if((x & 1) != 0) s1[i] = true, X = i;
		x >>= 1;
		++ i;
	}
	i = 1;
	while(y != 0){
		if((y & 1) != 0) s2[i] = true, Y = i;
		y >>= 1;
		++ i;
	}
	// Kawaii();
	// debug();
	if(X > Y){
		for(re i = 1 ; i <= X ; ++ i){
			if(s1[i] == true && s2[i] == true){
				// Kawaii();
				++ ans[i+1];//进一位
			}
			else if(s1[i] == true || s2[i] == true){
				++ ans[i];
			}
		}
		for(re i = 1 ; i <= X+1 ; ++ i){
			if(ans[i] != 0){
				final_ans += ksm(2, i-1);
			}
		}
	}
	else if(X < Y){
		// Kawaii();
		for(re i = 1 ; i <= Y ; ++ i){
			if(s1[i] == true && s2[i] == true){
				++ ans[i+1];//进一位
			}
			else if(s1[i] == true || s2[i] == true){
				// Kawaii();
				++ ans[i];
			}
			if(ans[i] == 2){
				ans[i] = 0;
				++ ans[i+1];
			}
		}
		for(re i = 1 ; i <= Y+1 ; ++ i){
			if(ans[i] != 0){
				final_ans += ksm(2, i-1);
			}
		}
	}
	else {
		for(re i = 1 ; i <= X ; ++ i){
			if(s1[i] == true && s2[i] == true){
				++ ans[i+1];//进一位
			}
			else if(s1[i] == true || s2[i] == true){
				++ ans[i];
			}
		}
		for(re i = 1 ; i <= X+1 ; ++ i){
			if(ans[i] != 0){
				final_ans += ksm(2, i-1);
			}
		}
	}
	// for(re i = 20 ; i >= 1 ; -- i){
	// 	cout << ans[i] << " ";
	// }
	// cout << endl;
	if(flag == true && flager == false){
		cout << -final_ans;
		return ;
	}
	cout << final_ans;
	return ;
}
inline void fastio_setup(){
	ios::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);
    double __FAS__INT128;
    IFNDEF__ios_FASTIO_;cin >> __FAS__INT128;
    cout << setprecision(3) << setiosflags(ios::fixed) << __FAS__INT128 << endl;
    cout << __FAS__INT128 << endl;
    cout.unsetf(ios::fixed);
    cout << fixed << setprecision(3) << __FAS__INT128 << endl;
    ENDIF__ios_FASTIO_;
}
char_phi main(){
	fastio_setup();
	work();
	return GMY;
}

 

posted @ 2022-06-29 17:03  char_phi  阅读(25)  评论(0编辑  收藏  举报