A1010 Radix [二分/******]

在这里插入图片描述
我就不该做 难得要死

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<math.h>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
using namespace std;
typedef long long LL;
LL Map[100];
LL inf = (1LL << 63) - 1;

void init()
{
	for (char c = '0'; c <= '9'; c++)
	{
		Map[c] = c - '0';
	}
	for (char c = 'a'; c <= 'z'; c++)
	{
		Map[c] = c - 'a' + 10;
	}
}

LL convertNum10(char a[], LL radix, LL t)
{
	LL ans = 0;
	int len = strlen(a);
	for (int i = 0; i < len; i++)
	{
		ans = ans * radix + Map[a[i]];
		if (ans<0 || ans>t) return -1;
	}
	return ans;
}

int cmp(char N2[], LL radix, LL t)
{
	int len = strlen(N2);
	int num = convertNum10(N2, radix, t);
	if (num < 0) return 1;
	if (t > num) return -1;
	else if (t == num) return 0;
	else return 1;
}

LL binarysearch(char N2[], LL left, LL right,LL t)
{
	LL mid;
	while (left <= right)
	{
		mid = (left + right) / 2;
		int flag = cmp(N2, mid, t);
		if (flag == 0) return mid;
		else if (flag == -1) left = mid + 1;
		else right = mid - 1;
	}
	return -1;
}

int findLargestDigit(char N2[])
{
	int ans = -1, len = strlen(N2);
	for (int i = 0; i < len; i++)
	{
		if (Map[N2[i]] > ans)
		{
			ans = Map[N2[i]];
		}
	}
	return ans + 1;
}

char N1[20], N2[20], temp[20];

int tag, radix;
int main()
{
	init();
	cin >> N1 >> N2 >> tag >> radix;
	if (tag == 2)
	{
		swap(N1, N2);
	}
	LL t = convertNum10(N1, radix, inf);
	LL low = findLargestDigit(N2);
	LL high = max(low, t) + 1;
	LL ans = binarysearch(N2, low, high, t);
	if (ans == -1) cout << "Impossible" << endl;
	else cout << ans << endl;
	return 0;
}
posted @ 2020-08-06 17:35  _Hsiung  阅读(56)  评论(0编辑  收藏  举报