zoukankan      html  css  js  c++  java
• # PAT.Radix(史上最 NT 题， (其实是我脑瘫/ 哭唧唧))

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

### Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:





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
/*
本题大意：给出两个任意进制的数字，让你判断将他们都转换为十进制后是否相等。
注意其中一个数字和他的进制已经给出，另一个数只给出数某种进制下的编码，让
你求出这个数的最小进制数，使得这两个数字在十进制下相等。

思路：题目给出2 ~ 36 其实是一个坑，答案进制数有可能超大，但其范围在
(这个数中最大的数字 + 1，已知数字 + 1)这个范围内，知道范围，求最小值
我们直接二分答案就行了。
*/
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#define mid ((l + r) >> 1)
using namespace std;

typedef long long ll;

ll inf = 8e18;

string s1, s2;
ll n1, n2;

ll quickly_pow(ll a, int b) {
ll ans = 1;
while(b) {
if(b & 1) {
ans *= a;
}
a *= a;
b >>= 1;
}
return ans;
}

ll get_decimal(string s, ll radix) {
ll ans = 0;
int len = s.size();
for(int i = len - 1; i >= 0; i --) {
if(s[i] > '9') ans += (s[i] - 'a' + 10) * quickly_pow(radix, len - i - 1);
else ans += (s[i] - '0') * quickly_pow(radix, len - i - 1);
if(ans > n1 || ans < 0) return n1 + 1;
}
return ans;
}

int main() {
while(cin >> s1 >> s2 >> tag >> radix) {
n1 = inf;
if(tag == 2) swap(s1, s2);
char c = *max_element(s2.begin(), s2.end());
ll l = c < '9' ? (c - '0' + 1) : (c - 'a' + 11), r = n1 + 1, ans = inf, temp;
while(l <= r) {
temp = get_decimal(s2, mid);
if(temp > n1) r = mid - 1;
else if(temp < n1) l = mid + 1;
else {
ans = min(ans, mid);
r = mid - 1;
}
}
if(ans == inf) cout << "Impossible" << endl;
else cout << ans << endl;
}
return 0;
}

• 相关阅读:
Ubuntu18.04, WPS表格生成中文大写数字的script
Java实现的简单神经网络(基于Sigmoid激活函数)
Naive Bayes Classifier 朴素贝叶斯分类器
动态规划处理diff算法 Myers Diff (正向)
动态规划处理01背包问题
文本diff算法Patience Diff
API返回错误信息的最佳实践
CAP理论中, P(partition tolerance, 分区容错性)的合理解释
mysql: SELECT ... FOR UPDATE 对SELECT语句的阻塞实验
Probability&Statistics 概率论与数理统计(1)
• 原文地址：https://www.cnblogs.com/bianjunting/p/12502505.html