1010. Radix (25) pat
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 题意很好理解,给出两个数,给出其中一个的radix,求是否存在适合的radix让两个数相等,
最初还在想radix的范围,就想肯定不能是1,最小是2,题中说最大为z(35),应该是2-36,肯定不对,没好好考虑,
要确定范围,下边界肯定不能从2开始,应该找出最大的数字,对他加一,才是下边界,因为题目说A digit is less than its radix,
然后上边界应该是下边界和给出radix的那个数的当中最大的那个,这个比较好理解,上边界肯定不能比下边界小,如果给出的N比下边界大,那么分两种情况,
未知radix的数如果是一位数,那么就不用管上边界了,假如有合适的肯定是下边界,如果不是单位数,肯定大于等于10,以N为进制的10正好等于N,所以上边界不能超过N了。
再就是如果排着尝试,必定会超时,可以用二分法,人家没说数的范围,封顶是64位长整型,计算的过程中容易溢出,变为负数要做特殊处理。 代码:
#include <iostream>
using namespace std;
int main()
{
string a,b;
long long tag,radix;
long long l,r;
int flag = ;
cin>>a>>b>>tag>>radix;
long long aa = ,bb = ;
if(tag == )swap(a,b);///这样做可以少写点代码,不用分情况了 for(int i = ;i < a.size();i ++)///先计算已知radix的数
{
if(a[i] >= '' && a[i] <= '')
{
aa = aa * radix + a[i] - '';
}
else if(a[i] >= 'a' && a[i] <= 'z')
{
aa = aa * radix + a[i] - 'a' + ;
}
}for(int i = ;i < b.size();i ++)
{
if(b[i] >= 'a' && b[i] <= 'z' && b[i] - 'a' + >= l)l = b[i] - 'a' + ;///下边界这里直接加一了,本来是加十,所以加十一
else if(b[i] >= '' && b[i] <= '' && b[i] - '' >= l)l = b[i] - '' + 1;
}
r = aa > l?aa:l;///确定上边界
long long mid = (l + r) / ;
while(l <= r)
{
bb = ;
for(int j = ;j < b.size();j ++)
{
if(b[j] >= '' && b[j] <= '')
{
bb = bb * mid + b[j] - '';
}
else if(b[j] >= 'a' && b[j] <= 'z')
{
bb = bb * mid + b[j] - 'a' + ;
}
if(bb < || bb > aa)break;///溢出的特殊处理
}
if(aa == bb)
{
cout<<mid;
flag = ;
break;
}
if(bb < ||bb > aa)r = mid - 1;///溢出的特殊处理else l = mid + ;
mid = (l + r) / ;
}
if(flag == )cout<<"Impossible";
}
1010. Radix (25) pat的更多相关文章
- PAT 解题报告 1010. Radix (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)
1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- 已经菜到不行了 PAT 1010. Radix (25)
https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...
- PAT (Advanced Level) 1010. Radix (25)
撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...
- PAT Advanced 1010 Radix(25) [⼆分法]
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- PAT 1010 Radix (25分) radix取值无限制,二分法提高效率
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- PAT甲题题解-1010. Radix (25)-二分搜索
题意:给出n1和n2,以及其中一个数的进制,问另一个数是多少进制的情况下,才会是两个数相等.不存在的话,则输出Impossible 这题思路很简单,但是要考虑的比较多,在简单题里面算是比较好的. 有两 ...
- 【PAT甲级】1010 Radix (25 分)(二分)
题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...
随机推荐
- 2018年全国多校算法寒假训练营练习比赛(第一场)G 圆圈
https://www.nowcoder.com/acm/contest/67/G 思路: 分形. 记录中间左边点的坐标,然后推出另外3个点的坐标,递归到最简单的情况. 代码: #include< ...
- win7创建 VirtualBox COM 对象失败。 应用程序现在将终止。 Callee RC: E_NOINTERFACE (0x80004002)
win7创建 VirtualBox COM 对象失败. 应用程序现在将终止. Callee RC: E_NOINTERFACE (0x80004002) 启动VirtualBox提示这个错误, ...
- vs2010打包安装
[WinForm] VS2010发布.打包安装程序(超全超详细) 2017年02月17日 21:47:09 y13156556538 阅读数:16487更多 个人分类: C#winform 1. ...
- zzuli1985(dp/水dfs郑轻比赛)
再一次感受到dp的威力 1985: 即将到来的新生赛 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 112 Solved: 28 SubmitStat ...
- UVA-1663 Purifying Machine (最大匹配数)
题目大意:每一个01串中最多含有一个‘*’,‘*’既可表示0也可表示1,给出一些等长的这样的01串,问最少能用多少个这样的串表示出这些串.如:000.010.0*1表示000.010.001.011, ...
- Oracle12c中性能优化&功能增强新特性之重大突破——内存列存储新特性
内存列存储(IM column store) 是Oracle12.1.0.2版本的主要特点.该特点允许列,表,分区和物化视图在内存中以列格式存储,而不是通常的行格式.数据存在内存中的好处显而易见,而列 ...
- C++技能重拾2
13.类成员函数重载:局部同名函数将隐藏而不是重载全局声明,不引入父类名字空间时子类的同名函数不会和父类的构成重载,静态成员函数可以和非静态成员函数构成重载.本质是重载函数的定义是在相同的声明域里!! ...
- UVALive 5107 dfs暴力搜索
题目链接:A hard Aoshu Problem DES:给三个字符串,包含的字符是A-E范围内的.长度都不超过8.每个字符可以而且只可以匹配一个数字.两个字符不能匹配相同的数字.前两个式子之间可以 ...
- Openwrt VLAN Configure(2)
1 Scope of Document This document describes vlan design on nodewrt2p 2 Requiremen 2.1 ...
- Gradle 完整指南(Android)
Gradle 的编译周期 在解析 Gradle 的编译过程之前我们需要理解在 Gradle 中非常重要的两个对象.Project和Task. 每个项目的编译至少有一个 Project,一个 build ...