PAT 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
 1 #include <iostream>
 2 #include <string>
 3
 4 using namespace std;
 5
 6
 7 long long ToRadix(string num, long long radix)
 8 {
 9     long long res = 0;
10     for (int i = 0; i < num.size(); i++)
11     {
12         int x = (num[i] >= ‘0‘&&num[i] <= ‘9‘) ? num[i] - ‘0‘ : num[i] - ‘a‘ + 10;
13         res = res*radix + x;
14     }
15
16     return res;
17 }
18
19 int main()
20 {
21     string num[2];
22     int tag;
23     long long radix;
24
25     cin >> num[0] >> num[1] >> tag >> radix;
26     long long base = ToRadix(num[tag - 1], radix);
27
28     string number = num[2 - tag];
29     if (number.size() == 1)
30     {
31         int x = (number[0] >= ‘0‘&&number[0] <= ‘9‘) ? number[0] - ‘0‘ : number[0] - ‘a‘ + 10;
32         if (x == base)
33             cout << x + 1;
34         else
35             cout << "Impossible";
36
37         return 0;
38     }
39
40     long long minRadix = 0, maxRadix = base;
41     for (int i = 0; i < number.size(); i++)
42     {
43         int x = (number[i] >= ‘0‘&&number[i] <= ‘9‘) ? number[i] - ‘0‘ : number[i] - ‘a‘ + 10;
44         if (x + 1 > minRadix)
45             minRadix = x + 1;
46     }
47
48     while (minRadix <= maxRadix)
49     {
50         long long middle = (minRadix + maxRadix) / 2;
51         long long x = ToRadix(number, middle);
52         if (x == base)
53         {
54             cout << middle;
55             return 0;
56         }
57         else if (x > base || x<0)
58             maxRadix = middle - 1;
59         else
60             minRadix = middle + 1;
61     }
62
63     cout << "Impossible";
64 }
				
时间: 2024-10-11 16:20:49

PAT 1010. Radix (25)的相关文章

1010. Radix (25)——PAT (Advanced Level) Practise

题目信息: 1010. Radix (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 b

PAT 1010 Radix

过了一天感觉没干什么,想刷一发题弥补一下,正在考虑去做哪道,室友说去试试PAT 1010,果然当年自己直接跳过了这题,一看这通过率(0.07)有点夸张了.题目是已知一个数,求另外一个数的基数,使得这两个数数值上相等.很自然的考虑到使用二分搜索来确定这个基数,数字表示使用[0-9a-z],这tmd的让人很容易的想到基数的范围就在1~36之间了,艹,基数是可以超过这个范围的,如果没有考虑到这一点,可以得到的一个典型分值就是19分.不过基数在[1, 36]之间的话这个搜索范围太小了,直接暴力遍历也可以

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 rad

1010. Radix (25)

简直是哭着做出来的,起码提交了22遍.创纪录了... 首先不能像上次那样枚举进制数,那样输入987654321 10 1 10 的话十分钟都算不完,题中只有一个超时很照顾人了... 考虑近似计算,利用y=a*x^n+b 算出n,进制数如果有,也不会超过n,因此考虑由n向下寻找. 注意,进制数不能小于数中最大的数字. 如果将两个数字作为字符串输入,注意排除字符串前面的0. 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN,

PAT 1010 Radix 进制转换+二分法

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

PAT 1010 Radix (二分)

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 o

PAT 1010

1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

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 N?1?? and N?2??, your task is

PAT 1070. Mooncake (25)

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival.  Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture.  Now given the inventory amounts and the prices of al