题解:P17569 「FAOI-R15」XOR and Ugly Equation
Charged_Charge · · 题解
思路:
若 NO。由于 long long 的范围,用 __int128 即可。
AC 代码:
#include<bits/stdc++.h>
#define int long long
#define i128 __int128
using namespace std;
// ifstream fin("race.in");
// ofstream fout("race.out");
// #define cin fin
// #define cout fout
int t, a, k;
int n;
int lf(i128 a, int b){
return (a / b) % 2ll;
}
signed main(){
cin >> t;
while(t--){
cin >> k >> a;
if(a % 2 == 1){
cout << "NO\n";
continue;
}
int s1 = 0;
int s2 = 1;
for(int i = 1; i < k; i++){
if(lf((i128)s1 * s1, (1ll << i))){
if(!(a & (1ll << i))) s1 += (1ll << i);
}else{
if(a & (1ll << i)) s1 += (1ll << i);
}
}
for(int i = 1; i < k; i++){
if(lf((i128)s2 * s2, (1ll << i))){
if(!(a & (1ll << i))) s2 += (1ll << i);
}else{
if(a & (1ll << i)) s2 += (1ll << i);
}
}
cout << "YES\n";
cout << min(s1, s2) << '\n';
}
return 0;
}