1010. Radix (25)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number.
Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.
Input Specification:
Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number "radix" is the radix of N1 if "tag" is 1, or of N2 if "tag" is 2.
Output Specification:
For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print "Impossible". If the solution is not unique, output the smallest possible radix.
Sample Input 1:6 110 1 10Sample Output 1:
2Sample Input 2:
1 ab 1 2Sample Output 2:
Impossible
#include<iostream> #include<stdio.h> #include<string.h> #define ll long long int using namespace std; char A[11]; char B[11]; ll c,d; ll least,most; ll q; ll changeChar(char a)//换成数字 { if(a>='0'&&a<='9') return a-'0'; else return a-'a'+10; } ll changint(char a[] ,ll b)//得到一直到的值; { ll n1=strlen(a); ll k=1; ll sum=0; for(ll i=n1-1;i>=0;i--) { ll n=changeChar(a[i]); sum=n*k+sum; k=k*b; } return sum; } ll panduan(char a[])//判断至少是多少位; { ll n=strlen(a); ll low=0; for(ll i=n-1;i>=0;i--) { ll p=changeChar(a[i]); if(p>low) low=p; } return low+1; } int compare(char a[],ll w,ll q) { ll n1=strlen(a); ll k=1; ll sum=0; for(ll i=n1-1;i>=0;i--) { ll n=changeChar(a[i]); sum=n*k+sum; k=k*w; if(sum>q) return 1; } if(sum>q) return 1; if(sum<q) return -1; return 0; } ll erfen(char a[],ll l,ll r,ll q ) { while(l<r) { ll now =(l+r)/2; int flag=compare(a,now,q); if(flag==0) { return now; } else if(flag==1) { r=now; } else if(flag==-1) { l=now; } } return -1; } int main() { cin>>A; cin>>B; cin>>c; cin>>d; if(c==1) { q=changint(A,d); least=panduan(B); most=(q+1>least+1)?q+1:least+1; ll res = erfen(B,least,most,q); if(res==-1) cout<<"Impossible"<<endl; else cout<<res<<endl; } else { q=changint(B,d); least=panduan(A); most=(q+1>least+1)?q+1:least+1; ll res = erfen(A,least,most,q); if(res==-1) cout<<"Impossible"<<endl; else cout<<res<<endl; } return 0; }
给你 4个数==ABCD
A B 是2个数;
c是1 D就是a的进制;
2 就是 D 就是b的进制
求另一个进制