A 1010 Radix pta

This question is too dog, and I thought it was innocent of the violence, the result of 24 points ...
had to learn in silence for two minutes, or 24 minutes ... because the sub is not subject to the data range, I think that up to 36 decimal, as death set up an MA,
this problem will not burst long long for the first number
to the number of the second-half when we could burst long long that Zezheng? Special judge! If we bombed (it just becomes a negative number, but can not become a natural number) at the time of the second number to decimal
time we put it into a number larger than the first number 3 just fine it
is mainly thought of the half, I do not know how to solve critical long long
good reflection
on the code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int r(char c)
{
	if(isalpha(c)) return  c-'a'+10;
	return c - '0';
}
int main()
{
	ll sum = 0,ok = 0;
	int jin,tag,w1[20],w2[20];
	string s1,s2,t;
	cin>>s1>>s2>>tag>>jin;
	if(tag == 2)
	{
		t = s2; s2 = s1; s1 =t;
	}//让s1 为已知进制
	int n1 = s1.size(),n2 = s2.size();
	//字母化数字好看 
	for (int i = 0; i < n1; i++)	w1[i] = r(s1[i]); 
	for (int i = 0; i < n2; i++)    w2[i] = r(s2[i]);
	//判断是否超进制如果超则不行 
	for	(int i = 0; i < n1; i++)  {
		if(w1[i] >= jin){
			sum = -1;break;
		}
		else{
			sum = sum*jin+w1[i];
		}
	}
	if(sum == -1) printf("Impossible\n");
	else{
		//确立一下 l 的 范围,至少要大于数字内的最大数 
		ll l = 0,r = sum+5; //r 等于sum+5 是因为下面二分不会取到r这个点 
		for (int i = 0; i < n2; i++)
		{
			if(l < w2[i]) l = w2[i];
		}
		l++;
		//纯纯的二分 
		while(l < r){
			ll mid = (r+l)/2;
			ll sum2 = 0;
			for (int i = 0; i < n2; i++) 
			{
				sum2 = sum2*mid + w2[i];
				if(sum2 < 0)
				{
					sum2 = sum+3; break;//溢出了 
				} //溢出的数肯定比sum大 
			}
			if(sum2 >= sum) 
			{
				r = mid; if(sum2 == sum) ok = 1;
			} 
			else l = mid+1; 
		}
		if(ok) printf("%lld\n",l);
		else   printf("Impossible\n");
		
	}
	return 0;
}
Published 55 original articles · won praise 1 · views 2639

Guess you like

Origin blog.csdn.net/qq_37548017/article/details/102821065