题目大意:
输入四个数字,a,b,c,d。
a和b是两个数字,c=1表示是第一个数字,c=2表示是第二个数字,d表示该数字是几进制
问:a和b是否能在某一进制下相等
思路:
傻逼二分...
一年没写过了,细节思考竟然差了这么多,尴尬了...
1 //看看会不会爆int!数组会不会少了一维! 2 //取物问题一定要小心先手胜利的条件 3 #include4 using namespace std; 5 #pragma comment(linker,"/STACK:102400000,102400000") 6 #define LL long long 7 #define ALL(a) a.begin(), a.end() 8 #define pb push_back 9 #define mk make_pair10 #define fi first11 #define se second12 #define haha printf("haha\n")13 const int maxn = 1e5 + 5;14 LL val;15 char ch1[maxn], ch2[maxn];16 17 LL get_int(char ch){18 if (ch >= '0' && ch <= '9') return 1LL * (ch - '0');19 return 1LL * (ch - 'a' + 10);20 }21 //c 1100 1 2622 bool flag;23 LL cal(LL x, int n){24 LL ans = 1;25 26 while (n){27 if (n & 1) ans *= x;28 x = x * x;29 if (x < 0 || ans < 0) flag = false;30 n /= 2;31 }32 return ans;33 }34 35 LL get_val(vector ve, int radix){36 LL sum = 0;37 int cnt = 0;38 for (int i = ve.size() - 1; i >= 0; i--){39 sum = sum + ve[i] * cal(1LL * radix, cnt);40 cnt++;41 }42 if (sum < 0) flag = false;43 return sum;44 }45 46 bool solve(vector b){47 LL lb = 0, rb = val + 1;48 for (int i = 0; i < b.size(); i++)49 lb = max(lb, b[i] + 1);50 while (lb < rb){51 LL mid = (lb + rb) / 2;52 flag = true;53 LL tmp = get_val(b, mid);54 if (!flag) {55 rb = mid - 1; continue;56 }57 if (val > tmp) lb = mid + 1;58 if (val < tmp) rb = mid - 1;59 if (val == tmp) rb = mid;60 }61 if (get_val(b, lb) == val) {62 printf("%lld\n", lb);63 return true;64 }65 return false;66 }67 68 int main(){69 scanf("%s%s", ch1, ch2);70 int ty, change;71 scanf("%d%d", &ty, &change);72 vector a, b;73 if (ty == 1){74 for (int i = 0; ch1[i] != '\0'; i++)75 a.push_back(get_int(ch1[i]));76 for (int i = 0; ch2[i] != '\0'; i++)77 b.push_back(get_int(ch2[i]));78 }79 else {80 for (int i = 0; ch2[i] != '\0'; i++)81 a.push_back(get_int(ch2[i]));82 for (int i = 0; ch1[i] != '\0'; i++)83 b.push_back(get_int(ch1[i]));84 }85 val = get_val(a, change);86 if(!solve(b)) puts("Impossible");87 return 0;88 }