1010 Radix (25 分)

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 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

Sample Output 2:

Impossible


题意:如果tag=1,则N1是radix进制数,若tag=2,则N2是radix数,问是否存在某进制数使得N2(N1)为N1(N2)的进制数。

分析:因为进制数越大所得到的值越大,比如110,若是二进制,值为6,若为10进制,值为110。
依据这点二分:
1、首先看N1、N2谁大,若N2大交换下N1、N2顺序。
2、二分下界为所出现数字中最大数字+1,上界为所给的radix,夹出对应进制数

 1 /**
 2 * Copyright(c)
 3 * All rights reserved.
 4 * Author : Mered1th
 5 * Date : 2019-02-26-14.28.46
 6 * Description : A1010
 7 */
 8 #include<cstdio>
 9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std;
19 
20 long long convert(string a,long long radix){ //转化为十进制
21     long long sum=0;
22     int temp=0,len=a.size();
23     for(int i=0;i<len;i++){
24         temp=isdigit(a[i])? a[i]-'0' :a[i]-'a'+10;
25         sum=sum*radix+temp;
26     }
27     return sum;
28 }
29 
30 long long find_radix(string a,long long num){
31     //二分法,进制数越大数值越大。先确定上下界:下界是最大值+1,比如987,最起码是一个10进制数。而上界是max(下界,N1的十进制)+1;
32     char it=*max_element(a.begin(),a.begin());
33     long long low=(isdigit(it)?it-'0':it-'a'+10)+1;
34     long long high=max(low,num);
35     while(low<=high){
36         long long mid=(low+high)/2;
37         long long t=convert(a,mid);
38         if(t<0||t>num) high=mid-1;
39         else if(t==num) return mid;
40         else low=mid+1;
41     }
42     return -1;
43 }
44 
45 int main(){
46 #ifdef ONLINE_JUDGE
47 #else
48     freopen("1.txt", "r", stdin);
49 #endif
50     string N1,N2;
51     int tag;
52     long long radix,ans;
53     cin>>N1>>N2>>tag>>radix;
54     if(tag==2){
55         swap(N1,N2);
56     }
57     ans=find_radix(N2,convert(N1,radix));
58     if(ans != -1) {
59         printf("%lld",ans);
60     }else{
61         printf("Impossible");
62     }
63 
64     return 0;
65 }

 

 

转载于:https://www.cnblogs.com/Mered1th/p/10437572.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值