1010. Radix (25)
来源:互联网 发布:pstn网络 编辑:程序博客网 时间:2024/05/14 13:19
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 <cstring>using namespace std;int getnum(char p){if(p<='9' & p>='0')return p-'0' ;else return p-'a'+10;}long long str2num(char *p, long long rad){long long num=0;for(int i=0;i<strlen(p);i++){num=num*rad+getnum(p[i]);if (num<0) return -1;}return num;}int main(){char a[15],b[15];int tag;long long radix;cin>>a>>b>>tag>>radix;if(tag==2){char temp[15];strcpy(temp,a);strcpy(a,b);strcpy(b,temp);}long long n=0;n=str2num(a,radix);long long threshold[2]={2,n+1};for(int i=0;i<strlen(b);i++)threshold[0]=threshold[0]<getnum(b[i])?getnum(b[i])+1:threshold[0];long long targetradix,t;while(threshold[1]>=threshold[0]){targetradix=(threshold[0]+threshold[1])/2;t=str2num(b,targetradix);if(n==t){cout<<targetradix<<endl;return 0;}else if (-1==t||n<t)threshold[1]=targetradix-1;else threshold[0]=targetradix+1;}cout<< "Impossible"<<endl;return 0;}
- 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25)-PAT
- 【PAT】1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25)
- PAT 1010. Radix (25)
- 1010. Radix (25)
- PAT 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25) (JAVA)
- PAT 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25)
- PAT 1010. Radix (25)
- 评教系统——学生评教提交
- codeforces 417D Cunning Gena
- android拍照压缩及添加水印3
- oracle 数据类型详解---日期型
- CF31D 分治
- 1010. Radix (25)
- Flatten Binary Tree to Linked List
- Swift中的!和?
- 1011. World Cup Betting (20)
- SQL Server不区分大小写的问题
- 图像处理+机器学习相关资源整合
- PHP关闭notice级别的错误提示
- 1012. The Best Rank (25)
- Handler消息传递机制(三)管理MessageQueue的Looper