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 radixHere 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

Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 typedef unsigned long long LL;
 5
 6 char a[20], b[20], t[20];
 7
 8 LL Trans(char s[], int e)
 9 {
10     int len = strlen(s);
11     LL ans = 0;
12     for (int i = 0; i < len; i++)
13     {
14         ans *= e;
15         if (s[i] >= ‘0‘ && s[i] <= ‘9‘)
16             ans += s[i] - ‘0‘;
17         else
18             ans += s[i] - ‘a‘ + 10;
19     }
20     return ans;
21 }
22
23 int main()
24 {
25     int tag, rad;
26     scanf(" %s %s%d%d" , a , b , &tag , &rad);
27     if (tag == 2)
28     {
29         strcpy(t, a);
30         strcpy(a, b);
31         strcpy(b, t);
32     }
33     LL an = Trans(a, rad);
34     int bmax = 0;
35     int blen = strlen(b);
36     for (int i = 0; i < blen; i++)
37     {
38         if (b[i] >= ‘0‘ && b[i] <= ‘9‘)
39             bmax = max(bmax, b[i] - ‘0‘);
40         else
41             bmax = max(bmax, b[i] - ‘a‘ + 10);
42     }
43
44     LL l = bmax + 1, r = an;
45     while (l < r)
46     {
47         LL mid = l + r >> 1;
48         LL amid = Trans(b, mid);
49         if (amid == an)
50             l = r = mid;
51         else if (amid < an)
52             l = mid + 1;
53         else
54             r = mid - 1;
55     }
56
57     if (Trans(b , l) == an)
58         printf("%llu" , l);
59     else
60         printf("Impossible\n");
61     return 0;
62 }

时间: 2024-10-12 03:23:55

1010. Radix (25)(进制 + 二分 + 模拟)的相关文章

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 i

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

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,

4.25进制转换

016.4.25 第一章 进制转换 1.数制 逢N进一,N是每种进位计数制表示一位数所需要的符号数目为基数 二进制:逢二进一 八进制:逢八进一,借一当八 十六进制:逢十六进一,借一当十六 2.数制转换 十进制:10个基数啊:0至9 二进制:2个基数:0,1 八进制:8个基数:0至7 十六进制:16个基数:0至9,A ,B,C,D,E,F 3.十进制转二进制 除商取余,所得余数从右向左依次填写

Java 10进制转2、8、16进制转换 / 2、8、16进制转10进制转换

public static void main(String[] args) { int i = 10; System.out.println("***********10进制转换2进制.8进制.16进制************"); System.out.println(Integer.toBinaryString(i)); // 10转换2进制 System.out.println(Integer.toOctalString(i)); // 10转换8进制 System.out.p

printf用法之打印2进制,八进制,十进制,十六进制

printf是格式化输出函数,它可以直接打印十进制,八进制,十六进制,输出控制符分别为%d, %o, %x, 但是它不存在二进制,如果输出二进制,可以手写,但是也可以调用stdlib.h里面的itoa函数,他不是标准库里面的函数,但是大多数编译器里面都有这个函数,所以就介绍一下 itoa函数的原型为char* itoa(int value, char * string, int radix); int value 被转换的整数,char *string 转换后储存的字符数组int radix 转

洛谷——P1604 B进制星球

P1604 B进制星球 题目背景 进制题目,而且还是个计算器~~ 题目描述 话说有一天,小Z乘坐宇宙飞船,飞到一个美丽的星球.因为历史的原因,科技在这个美丽的星球上并不很发达,星球上人们普遍采用B(2<=B<=36)进制计数.星球上的人们用美味的食物招待了小Z,作为回报,小Z希望送一个能够完成B进制加法的计算器给他们. 现在小Z希望你可以帮助他,编写实现B进制加法的程序. 输入输出格式 输入格式: 共3行第1行:一个十进制的整数,表示进制B.第2-3行:每行一个B进制数正整数.数字的每一位属于

PAT 1015 Reversible Primes[求d进制下的逆][简单]

1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now

面试题:10进制转二十五进制

import re class DecimalToTwentyFiveHex: """ 面试题:十进制转换成25进制 """ def __init__(self, int_ten): self.stack = [] # 存储正整数部分转换值 self.flag = None self.num_integer = None # 存贮整数部分 self.num_fractional = None # 存贮小数部分 if isinstance(int_