1010. Radix (25)

1.此题较为重点

2.最小的radix为各位的数字最大值+1

3.采用二分法查找,l=radix,r=已知的数+1

4.要求的最大radix不一定是36


//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
int main(void) {

	string n1, n2;
	int tag;
	unsigned long long radix;
	cin >> n1 >> n2 >> tag >> radix;
	string s, t;
	if (tag == 1)
	{
		s = n1;
		t = n2;
	}
	else if (tag == 2)
	{
		s = n2;
		t = n1;
	}

	unsigned long long sInt = 0;
	for (int i = 0; i < s.size(); i++)
	{//把s转为十进制
		sInt = sInt*radix + c2int(s[i]);
	}

	unsigned long long minRadix = 2;
	for (int i = 0; i < t.size(); i++)
	{//求出最小的进制数
		minRadix = max(minRadix, c2int(t[i]) + 1);
	}
	bool flag = false;
	unsigned long long result = 0;
	unsigned long long r = sInt + 1;//必须+1
	unsigned long long l = minRadix;
	while (l <= r)//必须要有等号!!
	{//从最小的进制数开始遍历
		unsigned long long j = (l + r) / 2;//没说明j最大是36进制
		unsigned long long tInt = 0;
		for (int i = 0; i < t.size(); i++)
		{
			tInt = tInt*j + c2int(t[i]);
			if (tInt > sInt)
			{
				break;
			}
		}
		if (tInt == sInt)
		{
			flag = true;
			result = j;
			break;
		}
		else if (tInt > sInt)
		{
			r = j - 1;
			/*flag = false;
			break;*/
		}
		else l = j + 1;
	}
	if (flag) cout << result << endl;
	else cout << "Impossible" << endl;


	return 0;
}



posted @ 2015-11-04 12:33  siukwan  阅读(134)  评论(0)    收藏  举报