https://www.patest.cn/contests/pat-a-practise/1010
题目大意:
输入四个数字,a,b,c,d。
a和b是两个数字,c=1表示是第一个数字,c=2表示是第二个数字,d表示该数字是几进制
问:a和b是否能在某一进制下相等
思路:
傻逼二分...
一年没写过了,细节思考竟然差了这么多,尴尬了...
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + ;
LL val;
char ch1[maxn], ch2[maxn]; LL get_int(char ch){
if (ch >= '' && ch <= '') return 1LL * (ch - '');
return 1LL * (ch - 'a' + );
}
//c 1100 1 26
bool flag;
LL cal(LL x, int n){
LL ans = ; while (n){
if (n & ) ans *= x;
x = x * x;
if (x < || ans < ) flag = false;
n /= ;
}
return ans;
} LL get_val(vector<LL> ve, int radix){
LL sum = ;
int cnt = ;
for (int i = ve.size() - ; i >= ; i--){
sum = sum + ve[i] * cal(1LL * radix, cnt);
cnt++;
}
if (sum < ) flag = false;
return sum;
} bool solve(vector<LL> b){
LL lb = , rb = val + ;
for (int i = ; i < b.size(); i++)
lb = max(lb, b[i] + );
while (lb < rb){
LL mid = (lb + rb) / ;
flag = true;
LL tmp = get_val(b, mid);
if (!flag) {
rb = mid - ; continue;
}
if (val > tmp) lb = mid + ;
if (val < tmp) rb = mid - ;
if (val == tmp) rb = mid;
}
if (get_val(b, lb) == val) {
printf("%lld\n", lb);
return true;
}
return false;
} int main(){
scanf("%s%s", ch1, ch2);
int ty, change;
scanf("%d%d", &ty, &change);
vector<LL> a, b;
if (ty == ){
for (int i = ; ch1[i] != '\0'; i++)
a.push_back(get_int(ch1[i]));
for (int i = ; ch2[i] != '\0'; i++)
b.push_back(get_int(ch2[i]));
}
else {
for (int i = ; ch2[i] != '\0'; i++)
a.push_back(get_int(ch2[i]));
for (int i = ; ch1[i] != '\0'; i++)
b.push_back(get_int(ch1[i]));
}
val = get_val(a, change);
if(!solve(b)) puts("Impossible");
return ;
}