1010. Radix
来源:互联网 发布:蓝宝石 知乎 编辑:程序博客网 时间:2024/06/05 11:12
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<string>using namespace std;long long toRadix(string num, long long radix){long long res = 0;for(int i = 0; i < num.length(); i ++){int x = (num[i] >= '0' && num[i] <= '9') ? num[i]-'0' : num[i]-'a'+10;res = res*radix + x;}return res;}int main(){string num[2];int tag;long long radix;cin>>num[0]>>num[1]>>tag>>radix;long long base = toRadix(num[tag-1], radix);string number = num[2-tag];if(number.length() == 1){int x = (number[0] >= '0' && number[0] <= '9') ? number[0]-'0' : number[0]-'a'+10;if(x == base)cout<<x+1;elsecout<<"Impossible";return 0;}long long minRadix = 0, maxRadix = base;for(int i = 0; i < number.length(); i ++){int x = (number[0] >= '0' && number[0] <= '9') ? number[0]-'0' : number[0]-'a'+10;if(x+1 > minRadix)minRadix = x+1;}while(minRadix <= maxRadix){long long middle = (minRadix + maxRadix) / 2;long long x = toRadix(number, middle);if(x == base){cout<<middle;return 0;}else if(x > base || x < 0)maxRadix = middle - 1;elseminRadix = middle + 1;}cout<<"Impossible";return 0;}
- 1010.Radix
- 1010. Radix
- 1010. Radix
- 1010. Radix
- 1010. Radix
- 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25)-PAT
- 【PAT】1010. Radix (25)
- 1010. Radix (25)
- PAT 1010. Radix
- 1010. Radix (25)
- PAT 1010. Radix (25)
- 1010. Radix (25)
- PAT 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25)
- 1010. Radix (25) (JAVA)
- Excel操作库
- PHP 进阶
- JAVA多线程实现和应用总结
- PostgreSQL 9.5 new feature - BRIN (block range index) index
- LeetCode | Valid Parentheses
- 1010. Radix
- 天声人語 20151014 秋晴れと空の青さ
- XHTML和HTML的区别
- 直方图均衡化的作用是图像增强原理
- SQL语句执行顺序
- llvm之IR手册翻译(2)
- Hadoop
- 【软件工程】软件测试
- PostgreSQL zhparser & english parser, query, insert performance